MFC中radio button的选中

我们首先创建两个RADIO控件:IDC_RADIO_FIRST和IDC_RADIO_LAST,我们的目的是默认选中IDC_RADIO_FIRST

方法一:CheckRadioButton();

在MSDN上的函数说明为:

BOOL CheckRadioButton(

  HWND hDlg, // handle to dialog box 

  int nIDFirstButton// identifier of first button in group 

  int nIDLastButton, // identifier of last button in group 

  int nIDCheckButton // identifier of button to select

);

其作用是将一组radio button中的一个设置为被选中状态,其余全部设置为不选中状态,参数中的nIDFirstButton就是组中的第一个按钮,nIDLastButton就是一组中的最后一个按钮。


对于我们创建的两个按钮来说,就是

 CheckRadioButton(IDC_RADIO_FIRST, IDC_RADIO_LAST, IDC_RADIO_FIRST);


方法二:

IDC_RADIO_FIRST设置为有Group属性,作用是将连续的radio button设置为一组(同一个对话框上的radio button可以划分为多组,每个具有group属性的都是一组的第一个),IDC_RADIO_LAST不需要设置;

CTRL + 双击 IDC_RADIO_FIRST按钮,为其添加一个成员变量m_Radio

在初始化RADIO时添加如下代码:

m_Radio=0;

UpdateData(FALSE);


方法三:

在初始化函数里添加:

CButton* radio=(CButton*)GetDlgItem(IDC_RADIO_FIRST);

radio->SetCheck(1);

这种方法最为简便,也用的最多,在多个radio button中有多个需要被设置为默认选中时,也可以用这种方法,只需要将需要被选中的SetCheck为1,不选中的可以不管,若是想要设置为不被选中,SetCheck为0就可以了。

猜你喜欢

转载自blog.csdn.net/ll596214569/article/details/79285886
今日推荐