Checkboxes can be created with the following style
BS_CHECKBOX – Creates a small, empty check box with text. By default, the text is displayed to the right of the check box.The CButton member functions GetState() and GetState() can be used to check/set the state of a button and ON_BN_CLICKED macro maps the button click function to the button click event.
//Checkbutton control example
#include <afxwin.h>
#define ID_REDBUTTON 1000
#define ID_GREENBUTTON 1001
#define ID_BLUEBUTTON 1002
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
afx_msg void wRedButtonOnClick( );
afx_msg void wGreenButtonOnClick( );
afx_msg void wBlueButtonOnClick( );
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
//instantiate button class;
CButton wRedButton;
CButton wGreenButton;
CButton wBlueButton;
COLORREF wcolor;
int red;
int green;
int blue;
HBRUSH OnCtlColor(CDC *,CWnd *,UINT );
bool OnEraseBkgnd(CDC *);
};
BOOL CSimpleApp::InitInstance()
{
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}
CMainFrame::CMainFrame()
{
red=0;
green=0;
blue=0;
Create(NULL, TEXT("MFC check box example"), WS_OVERLAPPEDWINDOW ,CRect(25,25,410,200));
wRedButton.Create(TEXT("Red"),BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_BORDER , CRect(70,50,125,75), this, ID_REDBUTTON);
wGreenButton.Create(TEXT("Green"),BS_AUTOCHECKBOX |WS_CHILD | WS_VISIBLE | WS_BORDER , CRect(135,50,210,75), this, ID_GREENBUTTON);
wBlueButton.Create(TEXT("Blue"),BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_BORDER , CRect(220,50,275,75), this, ID_BLUEBUTTON);
}
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
//message map
ON_BN_CLICKED(ID_REDBUTTON,wRedButtonOnClick)
ON_BN_CLICKED(ID_GREENBUTTON,wGreenButtonOnClick)
ON_BN_CLICKED(ID_BLUEBUTTON,wBlueButtonOnClick)
ON_WM_PAINT()
END_MESSAGE_MAP()
CSimpleApp MFCApp1;
afx_msg void CMainFrame::wRedButtonOnClick()
{
//red button click function
int cbvalue=wRedButton.GetState()-8;
red=cbvalue*255;
Invalidate();
}
//green button click function
afx_msg void CMainFrame::wGreenButtonOnClick()
{
int cbvalue=wGreenButton.GetState()-8;
green=cbvalue*255;
Invalidate();
}
//blue button click function
afx_msg void CMainFrame::wBlueButtonOnClick()
{
int cbvalue=wBlueButton.GetState()-8;
blue=cbvalue*255;
Invalidate();
}
afx_msg void CMainFrame::OnPaint()
{
CPaintDC paintDC(this);
CRect rect;
GetClientRect(&rect);
wcolor=RGB(red,green,blue);
paintDC.FillSolidRect(&rect,wcolor);
}