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

浪子不回头ぞ 提交于 2019-12-21 16:54:22

问题


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


来源:https://stackoverflow.com/questions/30350537/how-to-check-and-uncheck-and-enable-and-disable-a-check-box-control-in-mfc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!