Introduction
This class is derived from the MFC CButton
. It supports the following features:
- Showing of On/Off LEDs to indicate state of the check box
- Showing an icon to indicate state of the check box. Icon can be specified using a resource ID, filename or
HICON
.
- Left/Center/Right aligned texts
- Text on the left or right of the LED
- Enabled/disabled check boxes
- Tool tips
This article demonstrates how to extend MFC to subclass common controls and to apply owner drawing to give them any desired look.
Using the code
To use the class follow the steps:
- Add the files CCheckSK.h and CCheckSK.cpp to your project.
- Create check boxes on the dialog.
- Open the class wizard and create control variables for the check boxes. Choose "Control" for the "Category" and "CButton" for the "Variable Type".
- Include CCheckSK.h in the .h file for your dialog class (in the demo the file is checkDlg.h)
- If you had named the check-box variable
m_chk
then in the header file for the dialog, there will be a line CButton m_chk;
Replace the CButton
with CCheckSK
- At the end of
OnInitDialog
of your dialog class, add calls to the appropriate methods in CCheckSK
BOOL CCheckDlg::OnInitDialog()
{
...
m_chk1.SetCheck(TRUE);
m_chk1.SetLedColor(RGB(255, 0, 0), RGB(128, 0, 0));
m_chk1.SetToolTip("Click on this to change the state");
m_chk5.SetIcon(IDI_ON, IDI_OFF);
...
}
- If you want to change the look of the check boxes later, you can call any of these methods from your code at run time.
How the code works
We start by deriving the CCheckSk
class from the CButton
MFC.
class CCheckSK : public CButton
{
....
}
To make the control owner drawn, the BS_OWNERDRAW
style needs to be added to the window style. For this subclassing is used. To do this the PreSubclassWindow
method is overridden as follows.
void CCheckSK::PreSubclassWindow()
{
UINT nBS = GetButtonStyle();
ASSERT((nBS & SS_TYPEMASK) != BS_OWNERDRAW);
ASSERT(nBS & BS_CHECKBOX);
ModifyStyle(SS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
m_nStyle = GetWindowLong(GetSafeHwnd(), GWL_STYLE);
CButton::PreSubclassWindow();
}
Once the owner-draw style has been set, the framework will call the DrawItem
method each time it requires the control to be redrawn. So this method has to be implemented and all code to draw the LED or display the icon has to be added to it. The implementation of this method is a bit long and the following snippet just gives the flow
void CCheckSK::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
ASSERT (lpDIS->CtlType == ODT_BUTTON);
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
}
Methods
The following methods are present in CDialogSK
class:
DWORD SetIcon(int nIconOn, int nIconOff);
Sets the icon based on resource ID
DWORD SetIcon(HICON hIconOn, HICON hIconOff);
Sets the icon based on handle to a icon
DWORD SetIcon(LPCTSTR lpszFileNameIn, LPCTSTR lpszFileNameOut);
Sets the icon based on the name of an icon file
BOOL SetLedColor(COLORREF colLedOn, COLORREF colLedOff);
If any icons are not used then this method is used to set the color of the on/off LED
BOOL SetLedSize (int nSize);
The size of the LED is changed with this method.
void SetToolTip (LPCTSTR lpszText);
The tool tip for the check box can be changed using this method
History
- This is the initial version.