Modal dialog box and non-modal dialog box--non-original | Dark Horse Net Lesson Notes

Modal dialog

When the modal dialog box is displayed, the execution of the program will be suspended, and other tasks in the program can not be executed until the modal dialog box is closed.

1) Put a Button on the interface through the toolbox, double-click this button to jump to the button processing function:

//Button processing function
void CDialogDlg::OnBnClickedButton1()
{ // TODO: add control notification handler code here }

2) Resource View -> Dialog -> Right Click -> Insert Dialog:

3) Modify the dialog ID

4) Click the dialog template -> right click -> add class DlgExec

5) A custom class DlgExec is added in the class view

6) The button processing function creates a dialog box and runs it modally.
To create a modal dialog box, you need to call the member function CDialog::DoModel of the CDialog class. The function of this function is to create and display a dialog box:
//Start the modal dialog box button

void CDialogDlg::OnBnClickedButton1()
{
	//需要包含头文件:#include "DlgExec.h"
	CDlgExec dlg;
	dlg.DoModal(); //以模态方式运行
}

Non-modal dialog

When the non-modal dialog box is displayed, the operation is switched to perform other tasks in the program without closing the dialog box.

The graphical interface operation process is the same as that of the modal dialog box, except that the implementation of the non-modal dialog box is different. First create (CDialog::Create) once, and then display (CWnd::ShowWindow).

1) Declare the dialog object in the main dialog .h class

2) Create a dialog box in the constructor or OnCreate() function of the main dialog class, the purpose is to create the dialog box only once:

//主对话框构造函数
CDialogDlg::CDialogDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDialogDlg::IDD, pParent)
{
    
    
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_dlg.Create(IDD_DIALOG_SHOW); //IDD_DIALOG_SHOW为对话框ID
}

3) The button processing function displays the dialog box:

//启动非模态对话框按钮
void CDialogDlg::OnBnClickedButton2()
{
    
    
	// TODO:  在此添加控件通知处理程序代码

	m_dlg.ShowWindow(SW_SHOWNORMAL); //显示非模态对话框
}

Guess you like

Origin blog.csdn.net/weixin_49035356/article/details/109613711