-
Notifications
You must be signed in to change notification settings - Fork 3
/
LedButton.h
executable file
·155 lines (123 loc) · 4.47 KB
/
LedButton.h
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
#pragma once
// LedButton.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CLedButton window
#define DEFAULT_MARGIN 20
/*
class CLedButton : public CButton (version 1.0)
Description:
class that displays a LED that has 2 states: ON or OFF.
The LED can have some text on its right.
The LED is read-only (by user) and can not have focus (you should disable "tab stop" in the resource),
The disabled state is not implemented: it is like the enable state.
How to use:
To your dialog add either a button, a checkbox or a radio button,
bind it to a member variable (of type Control) using ClassWizard,
then change in the header file the class type to CLedButton.
In InitDialog bind your bitmap resource to the button:
m_greenLed.SetImage( IDB_LEDBUTTON_GREEN, 15 ); //15 is the width of one image in the bitmap
//You can set or retreive the state of the LED (ON or OFF) using
m_greenLed.Depress(TRUE); //ON (second image in bitmap)
m_greenLed.Depress(FALSE); //OFF (first image in bitmap)
These bitmaps are given with this source code:
IDB_LEDBUTTON_GREEN.bmp
IDB_LEDBUTTON_RED.bmp
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
Example of use:
in OnInitDialog():
//Set images
m_greenLed.SetImage( IDB_GREENBUTTON, 15 );
m_redLed.SetImage( IDB_REDBUTTON, 15 );
m_redLed2.SetImage( IDB_REDBUTTON, 15 );
// set the initial state of buttons
m_greenLed.Depress(true);
m_redLed.Depress(true);
//Add a handler to a switch button (for example)
void CExampleDlg::OnClikSwitchRedLed()
{
m_redLed.Depress(!m_redLed.IsDepressed());
}
*/
class CRGBImageList
{
public:
CRGBImageList()
{
m_dcMem.CreateCompatibleDC(NULL);
m_old = NULL;
}
virtual ~CRGBImageList()
{
if( m_hImage )
{
m_dcMem.SelectObject(m_old);
m_old = NULL;
m_bmp.Detach();
::DeleteObject(m_hImage);
}
m_dcMem.DeleteDC();
}
BOOL Create( UINT idBitmap, UINT nWidth )
{
m_hImage = (HBITMAP)::LoadImage( AfxGetInstanceHandle(), MAKEINTRESOURCE(idBitmap), IMAGE_BITMAP, 0, 0, LR_LOADTRANSPARENT|LR_LOADMAP3DCOLORS );
if( !m_hImage ) return FALSE;
m_bmp.Attach(m_hImage);
m_width = nWidth;
//CSize size = m_bmp.GetBitmapDimension(); //Must be set by SetBitmapDimension: unusable here.
m_bmp.GetObject(sizeof(BITMAP),&m_bmpInfo);
m_height = m_bmpInfo.bmHeight;
m_old = m_dcMem.SelectObject(&m_bmp);
return TRUE;
}
BOOL Draw(CDC* pDC, int nImage, POINT pt)
{
return pDC->BitBlt( pt.x, pt.y, m_width, m_height, &m_dcMem, nImage*m_width, 0, SRCCOPY );
}
CSize GetImageDimension() { CSize s(m_width,m_bmpInfo.bmHeight); return s; }
protected:
CDC m_dcMem;
HBITMAP m_hImage;
CBitmap m_bmp;
UINT m_width, m_height;
CGdiObject *m_old;
BITMAP m_bmpInfo;
};
class CLedButton : public CButton
{
// Construction
public:
//ReadOnly does not work: it is ALWAYS read-only.
CLedButton( BOOL bReadOnly=TRUE, BOOL bDepressed=FALSE, BOOL bCenterAlign=TRUE ) { Init(bReadOnly,bDepressed,bCenterAlign); }
virtual ~CLedButton() {}
void Init( BOOL bReadOnly=TRUE, BOOL bDepressed=FALSE, BOOL bCenterAlign=TRUE );
//Call "SetImage" once before displaying the control.
void SetImage(UINT idBmp, UINT nWidthOfOneImage);
//Does not work// void SetReadOnly(BOOL bReadOnly=TRUE) { m_bReadOnly = bReadOnly; }
BOOL Depress( BOOL Down ); //Set button state (TRUE=pushed, FALSE=depressed)
BOOL IsDepressed(); //Get button state
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CLedButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Generated message map functions
protected:
int m_nMargin;
int m_nRadius;
CPoint m_ptCenter;
CRect m_textRect;
BOOL m_bDepressed, m_bCenterAlign, m_bReadOnly; //m_bReadOnly does not work
CRGBImageList m_imgList;
CSize m_rcImage;
//{{AFX_MSG(CLedButton)
//}}AFX_MSG
//afx_msg BOOL OnClicked();
DECLARE_MESSAGE_MAP()
};