把dlg看作一个正常窗口即可,例子代码如下: BOOL CDlgApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need.#ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif CDlgDlg *dlg; dlg=new CDlgDlg(); m_pMainWnd = dlg; /*int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } */ dlg->Create(IDD_DLG_DIALOG,NULL); dlg->UpdateWindow(); dlg->ShowWindow(SW_SHOW); // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return TRUE; }
1.cmydlg必须是具有全局生命期的(或者与App的生命期内)有效的变量(App成员变量)
或者采用指针动态生成(new CMyDlg的方式);
2.InitInstance必须return TRUE;
3.必须为App的m_pMainWnd赋值如m_pMainWnd = &cmydlg;
4.对话框的退出代码中要调用PostQuitMessage(your exit code)或者发送WM_QUIT消息;
5.如果采用指针动态分配(new CMyDlg)的方式,要记得在ExitInstance中将指针删除;
1.cmydlg必须是具有全局生命期的(或者与App的生命期内)有效的变量(App成员变量)
或者采用指针动态生成(new CMyDlg的方式);
2.InitInstance必须return TRUE;
3.必须为App的m_pMainWnd赋值如m_pMainWnd = &cmydlg;
4.对话框的退出代码中要调用PostQuitMessage(your exit code)或者发送WM_QUIT消息;
5.如果采用指针动态分配(new CMyDlg)的方式,要记得在ExitInstance中将指针删除;
1.cmydlg必须是具有全局生命期的(或者与App的生命期内)有效的变量(App成员变量)
或者采用指针动态生成(new CMyDlg的方式);
2.InitInstance必须return TRUE;
3.必须为App的m_pMainWnd赋值如m_pMainWnd = &cmydlg;
4.对话框的退出代码中要调用PostQuitMessage(your exit code)或者发送WM_QUIT消息;
5.如果采用指针动态分配(new CMyDlg)的方式,要记得在ExitInstance中将指针删除;
BOOL CDlgApp::InitInstance()
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CDlgDlg *dlg;
dlg=new CDlgDlg();
m_pMainWnd = dlg;
/*int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
*/
dlg->Create(IDD_DLG_DIALOG,NULL);
dlg->UpdateWindow();
dlg->ShowWindow(SW_SHOW);
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return TRUE;
}
1.cmydlg必须是具有全局生命期的(或者与App的生命期内)有效的变量(App成员变量)
或者采用指针动态生成(new CMyDlg的方式);
2.InitInstance必须return TRUE;
3.必须为App的m_pMainWnd赋值如m_pMainWnd = &cmydlg;
4.对话框的退出代码中要调用PostQuitMessage(your exit code)或者发送WM_QUIT消息;
5.如果采用指针动态分配(new CMyDlg)的方式,要记得在ExitInstance中将指针删除;
Why? I can't ....
1.cmydlg必须是具有全局生命期的(或者与App的生命期内)有效的变量(App成员变量)
或者采用指针动态生成(new CMyDlg的方式); hjcao_wei的代码就是new出来的;2.InitInstance必须return TRUE;
hjcao_wei做到;3.必须为App的m_pMainWnd赋值如m_pMainWnd = &cmydlg;
hjcao_wei做到;4.对话框的退出代码中要调用PostQuitMessage(your exit code)或者发送WM_QUIT消息;
a8e的方法不全面,最好在重载的EndModalLoop()函数中;5.如果采用指针动态分配(new CMyDlg)的方式,要记得在ExitInstance中将指针删除;
hjcao_wei没有提出如何删除new的窗口;
在ExitInstance中加入;
delete m_pMainWnd;
m_pMainWnd = NULL;
(除非你能想办法将Dialog也做成View之类的动态创建删除的类否则就要自己删除以避免Memory Leak,注意:Dialog没有PostNcDestroy的调用)