How to check and uncheck and enable and disable a

2020-07-13 07:46发布

问题:

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

回答1:

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);


回答2:

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);