分类: C/C++
2009-05-31 18:18:30
CButton *p_MyBut = new CButton(); |
BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ); |
CButton* CTextEditorView::NewMyButton(int nID,CRect rect,int nStyle) { CString m_Caption; m_Caption.LoadString( nID ); //取按钮标题 CButton *p_Button = new CButton(); ASSERT_VALID(p_Button); p_Button->Create( m_Caption, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | nStyle, rect, this, nID ); //创建按钮 return p_Button; } |
CButton *p_MyBut[3]; p_MyBut[0] = NewMyButton( ID_MYBUT1, CRect(10,20,50,35), BS_DEFPUSHBUTTON ); p_MyBut[1] = NewMyButton( ID_MYBUT2, CRect(55,20,95,35), 0 ); p_MyBut[2] = NewMyButton( ID_MYBUT3, CRect(100,20,140,35), 0 ); |
BEGIN_MESSAGE_MAP(CTextEditorView, CFormView) //{{AFX_MSG_MAP(CTextEditorView) ON_BN_CLICKED(IDC_ICONBUT0, OnIconbut0) //}}AFX_MSG_MAP END_MESSAGE_MAP() |
BEGIN_MESSAGE_MAP(CTextEditorView, CFormView) //{{AFX_MSG_MAP(CTextEditorView) ON_BN_CLICKED(IDC_ICONBUT0, OnIconbut0) //}}AFX_MSG_MAP ON_BN_CLICKED(ID_MYBUT1, OnMybut1) ON_BN_CLICKED(ID_MYBUT2, OnMybut2) ON_BN_CLICKED(ID_MYBUT3, OnMybut3) END_MESSAGE_MAP() |
protected: //{{AFX_MSG(CTextEditorView) afx_msg void OnIconbut0(); //}}AFX_MSG DECLARE_MESSAGE_MAP() |
protected: //{{AFX_MSG(CTextEditorView) afx_msg void OnIconbut0(); //}}AFX_MSG afx_msg void OnMybut1(); afx_msg void OnMybut2(); afx_msg void OnMybut3(); DECLARE_MESSAGE_MAP() |
void CTextEditorView::OnMybut1() { MessageBox( "哈!你单击了动态按钮。" ); } void CTextEditorView::OnMybut2() { …… } void CTextEditorView::OnMybut3() { …… } |
if( p_MyBut[0] ) delete p_MyBut[0]; |
CButton* CTextEditorView::NewMyRadio(int nID,CRect rect,int nStyle) { CString m_Caption; m_Caption.LoadString( nID ); //取按钮标题 CButton *p_Radio = new CButton(); ASSERT_VALID(p_Radio); p_Radio->Create( m_Caption, WS_CHILD | WS_VISIBLE | nStyle | WS_TABSTOP | BS_AUTORADIOBUTTON, rect, this, nID ); //创建按钮 return p_Radio; } |
CButton *p_MyRadio[3]; p_MyRadio[0] = NewMyRadio( IDC_MYRADIO1, CRect(15,90,60,105), WS_GROUP ); p_MyRadio[1] = NewMyRadio( IDC_MYRADIO2, CRect(15,108,60,123), 0 ); p_MyRadio[2] = NewMyRadio( IDC_MYRADIO3, CRect(15,126,60,141), 0 ); |
int m_SelRadio; |
CButton *p_MyRadio[3]; p_MyRadio[0] = NewMyRadio( IDC_MYRADIO1, CRect(15,90,60,105), WS_GROUP ); p_MyRadio[1] = NewMyRadio( IDC_MYRADIO2, CRect(15,108,60,123), 0 ); p_MyRadio[2] = NewMyRadio( IDC_MYRADIO3, CRect(15,126,60,141), 0 ); p_MyRadio[m_SelRadio]->SetCheck(1); //设置第一个单选为选中状态 |
BEGIN_MESSAGE_MAP(CTextEditorView, CFormView) //{{AFX_MSG_MAP(CTextEditorView) ON_BN_CLICKED(IDC_ICONBUT0, OnIconbut0) //ClassWizard在此处添加 //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_MYRADIO1, OnMyRadio1) //单选按钮1 ON_BN_CLICKED(IDC_MYRADIO2, OnMyRadio2) //单选按钮2 ON_BN_CLICKED(IDC_MYRADIO3, OnMyRadio3) //单选按钮3 END_MESSAGE_MAP() |
protected: //{{AFX_MSG(CTextEditorView) afx_msg void OnIconbut0(); //ClassWizard在此处添加 //}}AFX_MSG afx_msg void OnMyRadio1(); //单选按钮1 afx_msg void OnMyRadio2(); //单选按钮2 afx_msg void OnMyRadio3(); //单选按钮3 DECLARE_MESSAGE_MAP() |
//单击单选按钮1 void CTextEditorView::OnMyRadio1() { m_SelRadio=0; } //单击单选按钮2 void CTextEditorView::OnMyRadio2() { m_SelRadio=1; } //单击单选按钮3 void CTextEditorView::OnMyRadio3() { m_SelRadio=2; } |
CTextEditorView::~CTextEditorView() { int i; for( i=0; i<3; i++) { if(p_MyRadio[i]) delete p_MyRadio[i]; } } |