-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRXButtonBase.cpp
180 lines (156 loc) · 5.67 KB
/
TRXButtonBase.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
#include "stdafx.h"
#include "TRXButtonBase.h"
#include "TRXTools.h"
#include "TRXColors.h"
#include "TRXGlobal.h"
#include "TRXGDI.h"
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CTRXButtonBase, CButton)
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
CTRXButtonBase::CTRXButtonBase(void)
{
m_IconResource = 0;
}
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
CTRXButtonBase::~CTRXButtonBase(void)
{
}
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CTRXButtonBase::SetIconResource ( UINT resource )
{
m_IconResource = resource;
}
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CTRXButtonBase, CButton)
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
ON_WM_RBUTTONUP()
END_MESSAGE_MAP()
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
HBRUSH CTRXButtonBase::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
//
HBRUSH hBrush = CTRXColors::OnCtlColor ( pDC, pWnd, nCtlColor );
if ( hBrush != NULL )
{
return hBrush;
}
HBRUSH hbr = CButton::OnCtlColor(pDC, pWnd, nCtlColor);
//
return hbr;
}
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
BOOL CTRXButtonBase::OnEraseBkgnd(CDC* pDC)
{
//
if ( CTRXColors::OnEraseBkgnd(pDC, this ) )
{
return TRUE;
}
return CButton::OnEraseBkgnd(pDC);
}
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CTRXButtonBase::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
UINT uStyle = DFCS_BUTTONPUSH;
static char szText [ MAX_PATH ];
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if ( pDC )
{
// This code only works with buttons.
ASSERT ( lpDrawItemStruct->CtlType == ODT_BUTTON );
// If drawing selected, add the pushed style to DrawFrameControl.
if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
uStyle |= DFCS_PUSHED;
}
// Draw the button frame.
// pDC->DrawEdge ( &lpDrawItemStruct->rcItem, EDGE_ETCHED, BF_ADJUST );
pDC->DrawFrameControl ( &lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle );
if ( CTRXColors::m_iDarkTheme != 0 )
{
RECT rect = lpDrawItemStruct->rcItem;
rect.top = rect.top + 1;
rect.left = rect.left + 1;
rect.bottom = rect.bottom - 2;
rect.right = rect.right - 2;
pDC->FillRect ( &rect, CTRXColors::GetBKNormalCBrush( CTRXColors::m_iDarkTheme != 0 ) );
}
// Get the button's text.
GetWindowText ( szText, sizeof(szText) );
// Draw the button text using the text color white.
COLORREF crOldColor;
if ( ( lpDrawItemStruct->itemState & ODS_DISABLED ) != 0 )
{
crOldColor = pDC->SetTextColor ( CTRXColors::GetFGDisabledCR( CTRXColors::m_iDarkTheme != 0 ) );
}
else if ( ( lpDrawItemStruct->itemState & ODS_FOCUS ) != 0 )
{
crOldColor = pDC->SetTextColor ( CTRXColors::GetFGSelectedCR( CTRXColors::m_iDarkTheme != 0 ) );
}
else
{
crOldColor = pDC->SetTextColor ( CTRXColors::GetFGNormalCR( CTRXColors::m_iDarkTheme != 0 ) );
}
if ( m_IconResource != 0 && ( lpDrawItemStruct->itemState & ODS_DISABLED ) == 0 )
{
int xIconSmall = GetSystemMetrics(SM_CXSMICON);
int yIconSmall = GetSystemMetrics(SM_CYSMICON);
int xMargin = ( ( lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left ) - xIconSmall ) / 2;
int yMargin = ( ( lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top ) - yIconSmall ) / 2;
if ( xMargin < 0 )
{
xMargin = 0;
}
if ( yMargin < 0 )
{
yMargin = 0;
}
HICON hIcon = AfxGetApp()->LoadIcon ( m_IconResource );
DrawIconEx ( pDC->m_hDC, lpDrawItemStruct->rcItem.left + xMargin, lpDrawItemStruct->rcItem.top + yMargin,
hIcon, xIconSmall, yIconSmall, 0, NULL, DI_NORMAL );
}
else
{
pDC->DrawText ( szText, &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER );
}
pDC->SetTextColor( crOldColor);
}
}
//
/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
void CTRXButtonBase::OnRButtonUp(UINT nFlags, CPoint point)
{
//
NMHDR hdr;
hdr.code = NM_RCLICK;
hdr.hwndFrom = this->GetSafeHwnd();
hdr.idFrom = GetDlgCtrlID();
this->GetParent()->SendMessage(WM_NOTIFY, (WPARAM)hdr.idFrom, (LPARAM)&hdr );
}