学习了下面这篇文章,研究它的CButton的实现方法,然后自己进行实现。
关键的一点是需要对对象进行实例化,即 new XXX();
============
我的任务如下:需要动态添加CMsChart
步骤如下:1。先用控件的方法,拖一个控件到对话框中,添加这个控件的事件。(目的是让他生成一些代码,以作参考,并且知道在哪里写)
生成的代码如下:
BEGIN_EVENTSINK_MAP(CTestDlgChartDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CTestDlgChartDlg)
ON_EVENT(CTestDlgChartDlg, IDC_COMBO2, -603 /* KeyPress */, OnKeyPressCB2, VTS_PI2)
ON_EVENT(CTestDlgChartDlg, IDC_MSCHART2, -605 /* MouseDown */, OnMouseDownMschart2, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
ON_EVENT(CTestDlgChartDlg, IDC_MSCHART2, 10 /* PointLabelSelected */, OnPointLabelSelectedMschart2, VTS_PI2 VTS_PI2 VTS_PI2 VTS_PI2)
ON_EVENT(CTestDlgChartDlg, IDC_MSCHART2, -600 /* Click */, OnClickMschart2, VTS_NONE)
ON_EVENT(CTestDlgChartDlg, IDC_MYBUTTON, -600 /* Click */, OnMyBtn, VTS_NONE)
ON_EVENT(CTestDlgChartDlg, IDC_MYBUTTON, 9 /* PointSelected */, OnPointSelectedMschart2, VTS_PI2 VTS_PI2 VTS_PI2 VTS_PI2)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
2。在stringTable中插入一个ID
3. 在.h文件中顶一个变量 CMSchart *m_pChart; //指针
4。在OnInitialDialog()中 对m_pChart 实例化 m_pChart = new CMsChart();
5. Create
m_pChart->Create(**,**......ID_MYCHART); //带上自己的ID
6. 修改消息映射
============
参考文章为:http://www.cnblogs.com/cy163/archive/2006/11/04/549724.html
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]; } } |