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);
}
Type: int
One of the following flags to indicate how the window is to be shown.
SW_HIDE
Hides the window and activates another window.
SW_MAXIMIZE
Maximizes the specified window.
SW_MINIMIZE
Minimizes the specified window and activates the next top-level window in the z-order.
SW_RESTORE
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.
SW_SHOW
Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT
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.
SW_SHOWMAXIMIZED
Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED
Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE
Displays the window as a minimized window. The active window remains active.
SW_SHOWNA
Displays the window in its current state. The active window remains active.
SW_SHOWNOACTIVATE
Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL
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并没有自动给出析构函数,这时需要我们手动添加,在对话框对象析构时就会调用我们自定义的析构函数了 。
手动添加析构函数:
在CtestDlg.h中添加~CtestDlg()
在CtestDlg中添加实现函数:
CtestDlg::~CtestDlg()
{
// 如果非模态对话框已经创建则删除它
if (NULL != dlg)
{
// 删除非模态对话框对象
delete dlg;
}
}
这样一个完整的非模态对话框的现实就完成啦