/
Label.cpp
213 lines (166 loc) · 3.7 KB
/
Label.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Label.cpp : implementation file
//
#include "stdafx.h"
#include "Resource.h"
#include "Label.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLabel
CLabel::CLabel()
{
m_crText = GetSysColor(COLOR_WINDOWTEXT);
m_hBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
m_font.CreateFontIndirect(&m_lf);
m_bTimer = FALSE;
m_bState = FALSE;
m_bLink = TRUE;
m_hCursor = NULL;
m_Type = None;
m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
}
CLabel::~CLabel()
{
m_font.DeleteObject();
::DeleteObject(m_hBrush);
}
CLabel& CLabel::SetText(const CString& strText)
{
SetWindowText(strText);
return *this;
}
CLabel& CLabel::SetTextColor(COLORREF crText)
{
m_crText = crText;
RedrawWindow();
return *this;
}
CLabel& CLabel::SetFontBold(BOOL bBold)
{
m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetFontUnderline(BOOL bSet)
{
m_lf.lfUnderline = bSet;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetFontItalic(BOOL bSet)
{
m_lf.lfItalic = bSet;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetFontSize(int nSize)
{
nSize*=-1;
m_lf.lfHeight = nSize;
ReconstructFont();
RedrawWindow();
return *this;
}
CLabel& CLabel::SetBkColor(COLORREF crBkgnd)
{
if (m_hBrush)
::DeleteObject(m_hBrush);
m_hBrush = ::CreateSolidBrush(crBkgnd);
return *this;
}
CLabel& CLabel::SetFontName(const CString& strFont)
{
//strcpy_s(m_lf.lfFaceName, 32, strFont);
strcpy(m_lf.lfFaceName, strFont);
ReconstructFont();
RedrawWindow();
return *this;
}
BEGIN_MESSAGE_MAP(CLabel, CStatic)
//{{AFX_MSG_MAP(CLabel)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLabel message handlers
HBRUSH CLabel::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
// TODO: Return a non-NULL brush if the parent's handler should not be called
if (CTLCOLOR_STATIC == nCtlColor)
{
pDC->SelectObject(&m_font);
pDC->SetTextColor(m_crText);
pDC->SetBkMode(TRANSPARENT);
}
if (m_Type == Background)
{
if (!m_bState)
return m_hwndBrush;
}
return m_hBrush;
}
void CLabel::ReconstructFont()
{
m_font.DeleteObject();
BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
ASSERT(bCreated);
}
void CLabel::OnTimer(UINT nIDEvent)
{
m_bState = !m_bState;
switch (m_Type)
{
case Text:
if (m_bState)
SetWindowText("");
else
SetWindowText(m_strText);
break;
case Background:
InvalidateRect(NULL,FALSE);
UpdateWindow();
break;
}
CStatic::OnTimer(nIDEvent);
}
CLabel& CLabel::SetLink(BOOL bLink)
{
m_bLink = bLink;
if (bLink)
ModifyStyle(0,SS_NOTIFY);
else
ModifyStyle(SS_NOTIFY,0);
return *this;
}
void CLabel::OnLButtonDown(UINT nFlags, CPoint point)
{
CString strLink;
GetWindowText(strLink);
ShellExecute(NULL,"open",strLink,NULL,NULL,SW_SHOWNORMAL);
CStatic::OnLButtonDown(nFlags, point);
}
BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_hCursor)
{
::SetCursor(m_hCursor);
return TRUE;
}
return CStatic::OnSetCursor(pWnd, nHitTest, message);
}
CLabel& CLabel::SetLinkCursor(HCURSOR hCursor)
{
m_hCursor = hCursor;
return *this;
}