本文只描述了一种添加无模式对话框的方法,不涉及所有方法的讨论,有好的方法,欢迎大家讨论。关于模式和无模式的对话框的区别,在上篇文章中已有,不再赘述,直接捞干的。
1、创建的是基于对话框的工程。(我的是wince下的);
2、有一个父窗口,插入新的窗口如newDlg,并将其属性设置为child;
3、给newDlg 添加类,如newDlg.cpp , newDlg.h
4、在父窗口文件中添加以下代码:
某个按钮操作函数{ //创建
newDlg NewDlg;
NewDlg.Create(NewDlg.IDD,this);
NewDlg.ShowWindow(SW_SHOW);
}
某个按钮操作函数{ //销毁
NewDlg.ShowWindow(SW_HIDE);
NewDlg.SendMessage(WM_DESTROY);
}
5、别忘了包含头文件。
6、关于create函数:
1 virtual BOOL Create( 2 LPCTSTR lpszTemplateName, 3 CWnd* pParentWnd = NULL 4 ); 5 virtual BOOL Create( 6 UINT nIDTemplate, 7 CWnd* pParentWnd = NULL 8 ); 9
- lpszTemplateName
-
Contains a null-terminated string that is the name of a dialog-box template resource. - pParentWnd
-
Points to the parent window object (of type CWnd) to which the dialog object belongs. If it is NULL, the dialog object‘s parent window is set to the main application window. - nIDTemplate
-
Contains the ID number of a dialog-box template resource.You can put the call to Create inside the constructor or call it after the constructor is invoked.
Two forms of the Create member function are provided for access to the dialog-box template resource by either template name or template ID number (for example, IDD_DIALOG1).
For either form, pass a pointer to the parent window object. If pParentWnd is NULL, the dialog box will be created with its parent or owner window set to the main application window.
The Create member function returns immediately after it creates the dialog box.
Use the WS_VISIBLE style in the dialog-box template if the dialog box should appear when the parent window is created. Otherwise, you must call ShowWindow. For further dialog-box styles and their application, see the DLGTEMPLATE structure in the Windows SDK and Window Styles in the MFC Reference.
Use the CWnd::DestroyWindow function to destroy a dialog box created by the Create function.
例子:
1 void CMyDialog::OnMenuShowSimpleDialog() 2 { 3 //m_pSimpleDialog initialized to NULL in the constructor of CMyDialog class 4 m_pSimpleDlg = new CSimpleDlg(); 5 //Check if new succeeded and we got a valid pointer to a dialog object 6 if(m_pSimpleDlg != NULL) 7 { 8 BOOL ret = m_pSimpleDlg->Create(IDD_SIMPLEDIALOG, this); 9 10 if(!ret) //Create failed. 11 AfxMessageBox(_T("Error creating Dialog")); 12 13 m_pSimpleDlg->ShowWindow(SW_SHOW); 14 } 15 else 16 { 17 AfxMessageBox(_T("Error Creating Dialog Object")); 18 } 19 }