15,980
社区成员




//**************************************************
//头文件的定义部分如下:
class CCreateButton : public CWnd
{
public:
//……
CButton *m_BnRadio1;
CButton *m_BnRadio2;
CButton *m_BnRadio3;
CButton *m_BnRadio4;
//单击Radio的响应函数
afx_msg void OnBnRadioClked(UINT m_BnRaID);
//……
}
//*******************************************************
//*******************************************************
//.cpp文件定义如下:
//定义控件ID
#define IDB_RABUTTON1 310
#define IDB_RABUTTON2 311
#define IDB_RABUTTON3 312
#define IDB_RABUTTON4 313
BEGIN_MESSAGE_MAP(CCreateButton,CWnd)
//……
ON_CONTROL_RANGE(BN_CLICKED,IDB_RABUTTON1,IDB_RABUTTON4,&CCreateButton::OnBnRadioClked)
//……
END_MESSAGE_MAP()
CCreateButton::CCreateButton(CWnd *pParent /* = NULL */)
{
//……
//初始化指针为空
m_BnRadio1 = NULL;
m_BnRadio2 = NULL;
m_BnRadio3 = NULL;
m_BnRadio4 = NULL;
}
void CCreateButton::CreateButton1()
{
//……
//新建4个单选控件
m_BnRadio1 = new CButton;
m_BnRadio1->Create(_T("图1"),WS_GROUP | WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
CRect(0,370,80,395),m_WndCreate,IDB_RABUTTON1);
m_BnRadio2 = new CButton;
m_BnRadio2->Create(_T("图2"),WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
CRect(100,370,180,395),m_WndCreate,IDB_RABUTTON2);
m_BnRadio3 = new CButton;
m_BnRadio3->Create(_T("图3"),WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
CRect(200,370,280,395),m_WndCreate,IDB_RABUTTON3);
m_BnRadio4 = new CButton;
m_BnRadio4->Create(_T("图4"),WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
CRect(300,370,380,395),m_WndCreate,IDB_RABUTTON4);
//预置第一Radio为选中
m_BnRadio1->SetCheck(true);
m_BnRadio2->SetCheck(false);
m_BnRadio3->SetCheck(false);
m_BnRadio4->SetCheck(false);
//顺利执行,没有发生断言
//……
}
void CCreateButton::OnBnRadioClked(UINT m_BnRaID) //响应4个Radio控件的单击事件
{
switch(m_BnRaID)
{
case IDB_RABUTTON1:
{
/*m_BnRadio1->SetCheck(true);
m_BnRadio2->SetCheck(false);
m_BnRadio3->SetCheck(false);
m_BnRadio4->SetCheck(false);*/
//触发断言如下:***************************************
//_AFXWIN_INLINE void CButton::SetCheck(int nCheck)
//{ ASSERT(::IsWindow(m_hWnd)); ::SendMessage(m_hWnd, BM_SETCHECK, nCheck, 0); }
//**********************************************************
//下面的可以执行
((CButton*)GetDlgItem(IDB_RABUTTON1))->SetCheck(true);
((CButton*)GetDlgItem(IDB_RABUTTON2))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON3))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON4))->SetCheck(false);
//……
break;
}
case IDB_RABUTTON2:
{
/*m_BnRadio1->SetCheck(false);
m_BnRadio2->SetCheck(true);
m_BnRadio3->SetCheck(false);
m_BnRadio4->SetCheck(false);*/
//激发断言,同上
//下面的可以执行
((CButton*)GetDlgItem(IDB_RABUTTON1))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON2))->SetCheck(true);
((CButton*)GetDlgItem(IDB_RABUTTON3))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON4))->SetCheck(false);
//……
break;
}
case IDB_RABUTTON3:
{
/*m_BnRadio1->SetCheck(false);
m_BnRadio2->SetCheck(false);
m_BnRadio3->SetCheck(true);
m_BnRadio4->SetCheck(false);*/
//激发断言,同1
//下面的可以执行
((CButton*)GetDlgItem(IDB_RABUTTON1))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON2))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON3))->SetCheck(true);
((CButton*)GetDlgItem(IDB_RABUTTON4))->SetCheck(false);
//……
break;
}
case IDB_RABUTTON4:
{
/*m_BnRadio1->SetCheck(false);
m_BnRadio2->SetCheck(false);
m_BnRadio3->SetCheck(false);
m_BnRadio4->SetCheck(true);*/
//激发断言,同1
//下面的可以执行
((CButton*)GetDlgItem(IDB_RABUTTON1))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON2))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON3))->SetCheck(false);
((CButton*)GetDlgItem(IDB_RABUTTON4))->SetCheck(true);
//……
break;
}
}
}
void CCreateButton::OnClose()
{
//……
//释放内存
if(m_BnRadio1)
delete m_BnRadio1;
if(m_BnRadio2)
delete m_BnRadio2;
if(m_BnRadio3)
delete m_BnRadio3;
if(m_BnRadio4)
delete m_BnRadio4;
}
//*******************************************************
//自己定义的文件,创建按钮和相应函数
#include "stdafx.h"
#include "CMyHeadFile.h"
#include "Resource.h"
#define WNDCHILD1 2010
#define IDB_RABUTTON1 310
#define IDB_RABUTTON2 311
#define IDB_RABUTTON3 312
#define IDB_RABUTTON4 313
CButton *m_BnRadio1;
CButton *m_BnRadio2;
CButton *m_BnRadio3;
CButton *m_BnRadio4;
CCreateButton::CCreateButton(CWnd *pParent /* = NULL */)
{
m_WndCreate = NULL;
m_BnRadio1 = NULL;
m_BnRadio2 = NULL;
m_BnRadio3 = NULL;
m_BnRadio4 = NULL;
}