简体   繁体   English

以编程方式选择MFC单选按钮

[英]Programmatically select an MFC radio button

When I'm initializing a dialog, I'd like to select one of the radio buttons on the form. 当我初始化对话框时,我想选择表单上的一个单选按钮。 I don't see a way to associate a Control variable using the Class Wizard, like you would typically do with CButtons, CComboBoxes, etc... 我没有看到使用类向导关联Control变量的方法,就像你通常使用CButtons,CComboBoxes等一样...

Further, it doesn't like a CRadioButton class even exists. 此外,它不喜欢CRadioButton类甚至存在。

How can I select one of the several radio buttons? 如何选择几个单选按钮中的一个?

Use CWnd::CheckRadioButton to set select one button in a group and CWnd::GetCheckedRadioButton to retrieve the ID of the selected button. 使用CWnd :: CheckRadioButton设置组中的一个按钮和CWnd :: GetCheckedRadioButton以检索所选按钮的ID。 Be sure to call these methods on your dialog object, and not any of the radio button objects. 务必在对话框对象上调用这些方法,而不是任何单选按钮对象。

Radio buttons and check buttons are just buttons. 单选按钮和检查按钮只是按钮。 Use a CButton control and use GetCheck / SetCheck . 使用CButton控件并使用GetCheck / SetCheck

Going on what mos said, the following worked did the trick: 继续说mos,以下的工作做了诀窍:

CButton* pButton = (CButton*)GetDlgItem(IDC_RADIOBUTTON);
pButton->SetCheck(true);

你可以使用这个单行:

::SendMessage(GetDlgItem(IDC_RADIO1)->m_hWnd, BM_SETCHECK, BST_CHECKED, NULL);
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
  ...
  DDX_Radio(pDX, IDC_RADIO1, m_Radio);
  ...
}

but it is the same thing Wizard generates 但它与Wizard生成的一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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