StaticEx.cpp
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* CC/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "StaticEx.h"
CStaticEx::CStaticEx()
{
}
CStaticEx::~CStaticEx()
{
}
void CStaticEx::PreSubclassWindow()
{
// 修改风格为自绘
ModifyStyle(0, BS_OWNERDRAW);
CStatic::PreSubclassWindow();
}
BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
END_MESSAGE_MAP()
BOOL CStaticEx::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
void CStaticEx::OnPaint()
{
CPaintDC dc(this);
m_font.CreatePointFont(m_Fontsize, m_FontFacename); // 创建自定义的文字
CFont *pOldFont = dc.SelectObject(&m_font);
CSize sz = dc.GetTextExtent(m_WindowText); // 获得文字的长度以便改变Static控件的大小
m_brush.CreateSolidBrush(m_bkColor); // 创建背景画刷
CRect rect;
rect.left = m_nptx;
rect.top = m_npty;
rect.right = rect.left + m_nLen;
rect.bottom = rect.top + sz.cy;
MoveWindow(&rect);
GetClientRect(&rect);
CBrush * pOldBrush = dc.SelectObject(&m_brush);
dc.FillRect(&rect,&m_brush);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(m_textColor);
int n = m_nLen - sz.cx;
n >>= 1;
dc.TextOut(rect.left + n, rect.top, m_WindowText); // 文字显示
dc.SelectObject(pOldFont); // 还原DC为默认状态
dc.SelectObject(pOldBrush);
m_brush.DeleteObject(); // 删除资源对象
m_font.DeleteObject();
}
void CStaticEx::SetFont(int nSize, const char *strFaceName)
{
// 设置字体
m_Fontsize = nSize;
m_FontFacename = strFaceName;
}
void CStaticEx::SetColor(COLORREF bkColor, COLORREF textColor)
{
// 设置文字的背景色和前景色
m_bkColor = bkColor;
m_textColor = textColor;
}
void CStaticEx::SetWindowpos(int nx, int ny)
{
m_nptx = nx;
m_npty = ny;
}
void CStaticEx::SetStaticexText(const char *strtext)
{
m_WindowText = strtext;
}
CString &CStaticEx::GetStaticexText()
{
return m_WindowText;
}
void CStaticEx::SetWidth(int len)
{
m_nLen = len;
}