MFC入门示例之单选框、复选框

时间:2024-04-03 09:15:36

MFC入门示例之单选框、复选框

设置默认选中一个单选按钮,OnInitDialog()函数中添加:

CheckRadioButton(IDC_RADIO1, IDC_RADIO2, IDC_RADIO2);

按钮事件处理

 1 void CMFCApplication3Dlg::OnBnClickedChkBlue()
 2 {
 3     // 选中蓝色让对话框失效,触发清除背景
 4     Invalidate();
 5 }
 6 
 7 void CMFCApplication3Dlg::OnBnClickedChkRed()
 8 {
 9     OnBnClickedChkBlue();
10 }
11 
12 void CMFCApplication3Dlg::OnBnClickedChkGreen()
13 {
14     OnBnClickedChkBlue();
15 }
16 
17 void CMFCApplication3Dlg::OnBnClickedRadio1()
18 {
19     OnBnClickedChkBlue();
20 }
21 
22 //清除背景时画图
23 BOOL CMFCApplication3Dlg::OnEraseBkgnd(CDC* pDC)
24 {
25     // TODO: 在此添加消息处理程序代码和/或调用默认值
26     BOOL bRet = CDialogEx::OnEraseBkgnd(pDC);
27     int nRed, nGreen, nBlue;
28     nRed = m_chk_red.GetCheck() ? 255 : 0;
29     nGreen = m_chk_green.GetCheck() ? 255 : 0;
30     nBlue = m_chk_blue.GetCheck() ? 255 : 0;
31 
32     COLORREF crFore = RGB(nRed, nGreen, nBlue);
33     CBrush brush;
34     brush.CreateSolidBrush(crFore);
35     CBrush *pOldBrush = pDC->SelectObject(&brush);
36 
37     RECT rc = { 50,20,150,120 };
38     if (m_rd_rect.GetCheck()) {
39         pDC->Rectangle(&rc);
40     }
41     else {
42         pDC->Ellipse(&rc);
43     }
44     pDC->SelectObject(pOldBrush);
45 
46     return bRet;
47 }