VS2005 动态添加对话框并添加事件响应,添加子对话框

1. 资源里面insert dialog。

2. 右键点击对话框资源,选择:Add Class。

3. 添加了CToolBarDialog.h, CToolBarDialog.cpp两个文件。

4. 在CToolBarDialog.h里的最下面添加extern CToolBarDialog* g_pToolBarDlg;

5. 在CToolBarDialog.cpp里面添加:

#include "stdafx.h"
#include "SingleDoc.h"
#include "ToolBarDialog.h"


// CToolBarDialog dialog
CToolBarDialog* g_pToolBarDlg = 0;

static CToolBarDialog g_sToolBarDlg;

IMPLEMENT_DYNAMIC(CToolBarDialog, CDialog)

CToolBarDialog::CToolBarDialog(CWnd* pParent /*=NULL*/)
: CDialog(CToolBarDialog::IDD, pParent)
{
g_pToolBarDlg = this;
}

其中staticCToolBarDialog g_sToolBarDlg;这句话是为了能在程序入口点函数执行之前构造该对话框,也即执行对话框的构造函数,为g_pToolBarDlg赋值。

6. MainFram.cpp的int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)函数里面添加:

    g_pToolBarDlg->Create(CToolBarDialog::IDD, this);
g_pToolBarDlg->ShowWindow(SW_SHOW);

over!

 

另外添加2个事件:

右键单击对话框资源---properties---messsages添加lbuttondbclick和nclbuttonclick这2个事件。

    // 双击对话框的客户区时响应
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
// 双击对话框标题时相应
afx_msg void OnNcLButtonDblClk(UINT nHitTest, CPoint point);



添加子对话框:

1. 资源里面insert dialog。

2. 右键点击对话框资源,选择:Add Class。

3. 添加了CChildDialog.h, CToolBarDialog.cpp两个文件。

4. 为Doc类添加一个类成员变量:CChildDialog m_childDlg;

5. 跟上面的方法类似为doc类添加全局变量指针:g_pDocPointer;

6. MainFram.cpp的int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)函数里面添加:

    g_pDocPointer->m_childDlg1.Create(CChildDialog1::IDD, g_pToolBarDlg);
g_pDocPointer->m_childDlg1.ShowWindow(SW_SHOWNORMAL);



 



posted @ 2012-03-06 10:42  小 楼 一 夜 听 春 雨  阅读(2236)  评论(0)    收藏  举报