-
Notifications
You must be signed in to change notification settings - Fork 3
/
LedButton.cpp
executable file
·180 lines (148 loc) · 4.51 KB
/
LedButton.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
// LedButton.cpp : implementation file
//
#include "stdafx.h"
#include "LedButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*
Copyright:
Completely Free (as long as this header remains like this) !
Please visit http://welcome.to/softbird for updates, other sources and more !
(c)Benjamin Mayrargue 2000
*/
/////////////////////////////////////////////////////////////////////////////
// CLedButton
void CLedButton::Init( BOOL bReadOnly/*=TRUE*/, BOOL bDepressed/*=FALSE*/, BOOL bCenterAlign/*=TRUE*/ )
{
m_bDepressed = bDepressed;
m_bCenterAlign = bCenterAlign;
m_nMargin = DEFAULT_MARGIN;
m_bReadOnly = bReadOnly;
}
BEGIN_MESSAGE_MAP(CLedButton, CButton)
//{{AFX_MSG_MAP(CLedButton)
//}}AFX_MSG_MAP
//ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLedButton message handlers
void CLedButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
// make sure we are an owner draw button
ModifyStyle(0, BS_OWNERDRAW);
CRect rect;
GetClientRect(rect);
// setup the button metrics
LOGFONT lf;
GetFont()->GetLogFont(&lf);
m_nRadius = lf.lfHeight;
if( m_nRadius == 0 )
m_nRadius = 15;
if( m_nRadius < 0 )
m_nRadius = (-1)*m_nRadius;
m_nRadius = (int)(rect.bottom*0.5)-5;
// don't let the button get above this maximum radius
// user can reset m_nRadius later, if desired
if( m_nRadius > 6 )
m_nRadius = 6;
m_ptCenter.x = rect.left+m_nRadius+1;
if( m_bCenterAlign )
m_ptCenter.y = rect.top+(int)(rect.Height()*0.5+0.5);
else
m_ptCenter.y = rect.top+m_nRadius+1;
}
void CLedButton::SetImage(UINT idBmp, UINT nWidthOfOneImage)
{
m_imgList.Create( idBmp, nWidthOfOneImage );
m_rcImage = m_imgList.GetImageDimension();
m_ptCenter.x = 1; //m_ptCenter.x - rcImage.cx/2;
m_ptCenter.y = m_ptCenter.y - m_rcImage.cy/2;
if( m_nMargin<m_rcImage.cx )
m_nMargin = 5 + m_rcImage.cx;
}
// draw the button and text
void CLedButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct != NULL);
if( lpDrawItemStruct->itemAction != ODA_DRAWENTIRE )
return;
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
int nSavedDC = pDC->SaveDC();
pDC->SelectStockObject(NULL_BRUSH);
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE));
//if( GetStyle()&BS_AUTOCHECKBOX && lpDrawItemStruct->itemState&ODS_SELECTED)
// SetCheck(GetCheck()?0:1);
//if( !m_bReadOnly )
//m_bDepressed = lpDrawItemStruct->itemState&ODS_SELECTED;
//m_bDepressed = GetCheck();
//Does not work as an auto checkbox !!! Why ???
if( m_bDepressed ) //Button down
m_imgList.Draw(pDC,1,m_ptCenter);
else //Button up
m_imgList.Draw(pDC,0,m_ptCenter);
// draw the text if there is any
CString strText;
GetWindowText(strText);
CSize Extent;
CPoint pt;
CRect textRect, clientRect;
GetClientRect( clientRect );
textRect = clientRect;
textRect.left += m_nMargin;
textRect.right -= 2;
if (!strText.IsEmpty())
{
pDC->SetBkMode(TRANSPARENT);
/*if (state & ODS_DISABLED)
{
Extent = pDC->GetTextExtent(strText);
pt.x = rect.left + m_nMargin;
pt.y = (int)((rect.Height() - Extent.cy)*0.5);
pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
}
else*/
{
pDC->DrawText( strText, textRect, DT_LEFT|DT_NOPREFIX|DT_WORDBREAK|DT_CALCRECT );
int h = textRect.Height();
textRect.top = clientRect.top+(int)((clientRect.Height()-textRect.Height())*0.5+0.5);
textRect.bottom = textRect.top + h;
pDC->DrawText( strText, textRect, DT_LEFT|DT_NOPREFIX|DT_WORDBREAK );
m_textRect = textRect;
}
}
textRect.right += 2;
pDC->RestoreDC(nSavedDC);
}
// return the button's depression state
BOOL CLedButton::IsDepressed()
{
if( !m_bReadOnly )
m_bDepressed = GetCheck();
return m_bDepressed;
}
// set the button's depression state
BOOL CLedButton::Depress(BOOL bDown)
{
//if( !m_bReadOnly )
// m_bDepressed = GetCheck();
if( bDown != m_bDepressed )
{
m_bDepressed = bDown;
//SetCheck(bDown);
//PostMessage(BM_SETCHECK, bDown, 0);
RedrawWindow();
}
return m_bDepressed;
}
/*
BOOL CLedButton::OnClicked()
{
//Depress(!GetCheck()); //Does not work. It seems that SetCheck can not be called from inside a handler.
return FALSE; //Continue routing
}
*/