简体   繁体   中英

How to check and uncheck and enable and disable a check Box control in MFC

What is the source code to do the standard checkbox actions with a Visual C++ MFC checkbox control?

  • set a check in the displayed checkbox control
  • clear a check in the displayed checkbox control
  • enable the displayed checkbox control for user input
  • disable the displayed checkbox control for user input

Controlling Checkboxes in MFC

Here's how to check, uncheck, enable, and disable a checkbox in MFC:

    CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->SetCheck(0);// uncheck it
      CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->SetCheck(1);// check it
      CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->EnableWindow(0);// disable it
      CButton* pBtn = (CButton*) GetDlgItem(IDC_SETUP_AM);
      pBtn->EnableWindow(1);// enable it
      bool isRemoveChecked = IsDlgButtonChecked(IDC_removeProf);

Alternatively, you won't need to retrieve a pointer to the button (checkbox) if you use CWnd::CheckDlgButton to check/un-check the button, for example:

BOOL isChecked = ...
CheckDlgButton(IDC_SOME_ID, isChecked);

And, enabling/disabling can be simplified to:

BOOL isEnabled = ...
GetDlgItem(IDC_SOME_ID)->EnableWindow(isEnabled);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM