MFC - Checkboxes

A checkbox is a Windows control that allows the user to set or change the value of an item as true or false.
Create
Creates the Windows button control and attaches it to the CButton object.
DrawItem
Override to draw an owner-drawn CButton object.
GetBitmap
Retrieves the handle of the bitmap previously set with SetBitmap.
GetButtonStyle
Retrieves information about the button control style.
GetCheck
Retrieves the check state of a button control.
GetCursor
Retrieves the handle of the cursor image previously set with SetCursor.
GetIcon
Retrieves the handle of the icon previously set with SetIcon.
GetIdealSize
Retrieves the ideal size of the button control.
GetImageList
Retrieves the image list of the button control.
GetNote
Retrieves the note component of the current command link control.
GetNoteLength
Retrieves the length of the note text for the current command link control.
GetSplitGlyph
Retrieves the glyph associated with the current split button control.
GetSplitImageList
Retrieves the image list for the current split button control.
GetSplitInfo
Retrieves information that defines the current split button control.
GetSplitSize
Retrieves the bounding rectangle of the drop-down component of the current split button control.
GetSplitStyle
Retrieves the split button styles that define the current split button control.
GetState
Retrieves the check state, highlight state, and focus state of a button control.
GetTextMargin
Retrieves the text margin of the button control.
SetBitmap
Specifies a bitmap to be displayed on the button.
SetButtonStyle
Changes the style of a button.
SetCheck
Sets the check state of a button control.
SetCursor
Specifies a cursor image to be displayed on the button.
SetDropDownState
Sets the drop-down state of the current split button control.
SetIcon
Specifies an icon to be displayed on the button.
SetImageList
Sets the image list of the button control.
SetNote
Sets the note on the current command link control.
SetSplitGlyph
Associates a specified glyph with the current split button control.
SetSplitImageList
Associates an image list with the current split button control.
SetSplitInfo
Specifies information that defines the current split button control.
SetSplitSize
Sets the bounding rectangle of the drop-down component of the current split button control.
SetSplitStyle
Sets the style of the current split button control.
SetState
Sets the highlighting state of a button control.
SetTextMargin
Sets the text margin of the button control.
Let us create a new MFC dialog based project.
Once the project is created, you will see the following dialog box in designer window.
Step 1 − Delete the TODO line and drag one checkbox and one Edit control as shown in the following snapshot. Also change the caption of checkbox to Enable Control.
Step 2 − Right-click on the checkbox and select Add Variable.
Step 3 − You can select different options on this dialog box. For checkbox, the CButton variable type is selected by default.
Step 4 − Similarly, the control ID is also selected by default. We now need to select Control in the Category combo box, and type m_enableDisableCheck in the Variable Name edit box and click Finish.
Step 5 − Add Control Variable of Edit control with the settings as shown in the following snapshot.
Step 6 − Observe the header file of the dialog class. You can see that these two variables have been added now.
CButton m_enableDisableCheck;
CEdit m_myEditControl;
Step 7 − Right-click on the checkbox and select Add Variable.
Step 8 − Click Finish to continue.
Step 9 − Add value Variable for Edit control with the settings as shown in the following snapshot.
Step 10 − Observe the header file. You can see that the new variables have been added now.
bool m_enableDisableVal;
CString m_editControlVal;
Step 11 − Now we will add event handler for checkbox.
Step 12 − Right-click the control for which you want to handle the notification event.
Step 13 − Select the event in the Message type box to add to the class selected in the Class list box.
Step 14 − Accept the default name in the Function handler name box, or provide a name of your choice.
Step 15 − Click Add and Edit to add the event handler.
Step 16 − You can now see the following event added at the end of CMFCControlManagementDlg.cpp file.
void CMFCControlManagementDlg::OnBnClickedCheck1() {
   // TODO: Add your control notification handler code here
}
Step 17 − This enables/disables the edit control when the checkbox is checked/unchecked.
Step 18 − We have now added the checkbox click event handler. Here is the implementation of event handler for checkbox.
void CMFCControlManagementDlg::OnBnClickedCheck1() {
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);
   if (m_enableDisableVal)
      m_myEditControl.EnableWindow(TRUE);
   else
      m_myEditControl.EnableWindow(FALSE);
}
Step 19 − We need to add the following code to CMFCControlManagementDlg::OnInitDialog(). When the dialog is created, it will manage these controls.
UpdateData(TRUE);
if (m_enableDisableVal)
   m_myEditControl.EnableWindow(TRUE);
else
   m_myEditControl.EnableWindow(FALSE);
Step 20 − Here is the complete implementation of CMFCControlManagementDlg.cpp file.
// MFCControlManagementDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCControlManagement.h"
#include "MFCControlManagementDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx {
   public:
      CAboutDlg();

   // Dialog Data
   #ifdef AFX_DESIGN_TIME
      enum { IDD = IDD_ABOUTBOX };
   #endif

   protected:
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
	
   // Implementation
   protected:
      DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) {

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX) {
   CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
CMFCControlManagementDlg::CMFCControlManagementDlg(CWnd* pParent /* = NULL*/)
   : CDialogEx(IDD_MFCCONTROLMANAGEMENT_DIALOG, pParent), 
   m_enableDisableVal(FALSE), m_editControlVal(_T("")) {
  
   m_hIcon = AfxGetApp()→LoadIcon(IDR_MAINFRAME);
}
void CMFCControlManagementDlg::DoDataExchange(CDataExchange* pDX) {
   CDialogEx::DoDataExchange(pDX);
   DDX_Control(pDX, IDC_CHECK1, m_enableDisableCheck);
   DDX_Control(pDX, IDC_EDIT1, m_myEditControl);
   DDX_Check(pDX, IDC_CHECK1, m_enableDisableVal);
   DDX_Text(pDX, IDC_EDIT1, m_editControlVal);
}

BEGIN_MESSAGE_MAP(CMFCControlManagementDlg, CDialogEx)
   ON_WM_SYSCOMMAND()
   ON_WM_PAINT()
   ON_WM_QUERYDRAGICON()
   ON_BN_CLICKED(IDC_CHECK1, &CMFCControlManagementDlg::OnBnClickedCheck1)
END_MESSAGE_MAP()


// CMFCControlManagementDlg message handlers

BOOL CMFCControlManagementDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();
	
   // Add "About..." menu item to system menu.
	
   // IDM_ABOUTBOX must be in the system command range.
   ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
   ASSERT(IDM_ABOUTBOX < 0xF000);
	
   CMenu* pSysMenu = GetSystemMenu(FALSE);
   if (pSysMenu != NULL) {
      BOOL bNameValid;
      CString strAboutMenu;
      bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
      ASSERT(bNameValid);
      if (!strAboutMenu.IsEmpty()) {
         pSysMenu->AppendMenu(MF_SEPARATOR);
         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }
   }

   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);      // Set big icon
   SetIcon(m_hIcon, FALSE);     // Set small icon

   // TODO: Add extra initialization here
   UpdateData(TRUE);
   if (m_enableDisableVal)
      m_myEditControl.EnableWindow(TRUE);
   else
      m_myEditControl.EnableWindow(FALSE);
   return TRUE; // return TRUE unless you set the focus to a control
}

void CMFCControlManagementDlg::OnSysCommand(UINT nID, LPARAM lParam) {
   if ((nID & 0xFFF0) == IDM_ABOUTBOX) {
      CAboutDlg dlgAbout;
      dlgAbout.DoModal(); 
   }else {
      CDialogEx::OnSysCommand(nID, lParam);
   }
}
  
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CMFCControlManagementDlg::OnPaint() {
   if (IsIconic()) {
      CPaintDC dc(this); // device context for painting

      SendMessage(WM_ICONERASEBKGND,
         reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
			
      // Center icon in client rectangle
      int cxIcon = GetSystemMetrics(SM_CXICON);
      int cyIcon = GetSystemMetrics(SM_CYICON);
      CRect rect;
      GetClientRect(&rect);
      int x = (rect.Width() - cxIcon + 1) / 2;
      int y = (rect.Height() - cyIcon + 1) / 2;

      // Draw the icon
      dc.DrawIcon(x, y, m_hIcon);
   }else{
      CDialogEx::OnPaint();
   }
}

// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCControlManagementDlg::OnQueryDragIcon() {
   return static_cast<HCURSOR>(m_hIcon);
}
void CMFCControlManagementDlg::OnBnClickedCheck1(){
   // TODO: Add your control notification handler code here
   UpdateData(TRUE);
   if (m_enableDisableVal)
      m_myEditControl.EnableWindow(TRUE);
   else
      m_myEditControl.EnableWindow(FALSE);
}
Step 21 − When the above code is compiled and executed, you will see the following output. You can now see the checkbox is unchecked by default. This disables the edit control.
Step 22 − Now when you check the checkbox, the edit control is enabled.
 Print
 Add Notes
 Bookmark this page