1新建VS2010/MFC文件,在面板上添加一个按钮 并为按钮添加响应函数
void CtestDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
}
2在Resource View中新建一个Dialog 并在类向导中为该Dialog建立一个类在Solution Explorer 中会多出两个文件 Cmode.h and Cmode.cpp
3在testDlg.h中添加头文件 Cmode.h 添加类成员 Cmode *dlg 并初始化dlg =NULL;
4编写非模态对话框显示的代码:
void CtestDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
// 如果指针变量dlg的值为NULL,则对话框还未创建,需要动态创建
if(NULL == dlg)
{
// 创建非模态对话框实例
dlg = new Cmode();
dlg->Create(IDD_DIALOG1,this);
}
dlg->ShowWindow(SW_SHOW);
}
ShowWindow(int nShow); Parameters
-
nShow [in]
-
Type: int
One of the following flags to indicate how the window is to be shown.
-
Hides the window and activates another window.
-
Maximizes the specified window.
-
Minimizes the specified window and activates the next top-level window in the z-order.
-
Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
-
Activates the window and displays it in its current size and position.
-
Sets the show state based on the information specified in the STARTUPINFO structure passed to the CreateProcess function that started the application. An application should call ShowWindow with this flag to set the initial visual state of its main window.
-
Activates the window and displays it as a maximized window.
-
Activates the window and displays it as a minimized window.
-
Displays the window as a minimized window. The active window remains active.
-
Displays the window in its current state. The active window remains active.
-
Displays a window in its most recent size and position. The active window remains active.
-
Default state. Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when it displays the window for the first time.
-
-
-
因为此非模态对话框实例是动态创建的,所以需要手动删除此动态对象来销毁对话框,一般都是在析构函数中去释放资源,MFC并没有自动给出析构函数,这时需要我们手动添加,在对话框对象析构时就会调用我们自定义的析构函数了 。
SW_HIDE
SW_MAXIMIZE
SW_MINIMIZE
SW_RESTORE
SW_SHOW
SW_SHOWDEFAULT
SW_SHOWMAXIMIZED
SW_SHOWMINIMIZED
SW_SHOWMINNOACTIVE
SW_SHOWNA
SW_SHOWNOACTIVATE
SW_SHOWNORMAL
-
手动添加析构函数:
在CtestDlg.h中添加~CtestDlg()
在CtestDlg中添加实现函数:
CtestDlg::~CtestDlg()
{
// 如果非模态对话框已经创建则删除它
if (NULL != dlg)
{
// 删除非模态对话框对象
delete dlg;
}
}
这样一个完整的非模态对话框的现实就完成啦