VS2019, MFC 模态对话框与非模态对话框

1. 模态对话框 显示时,应用程序被暂停,只有模态对话框关闭了 才能操作其他应用.

2. 非模态对话框 显示时 其他程序仍然可以运行。

3. 首先建立一个对话框 

3.1 在资源视图 右键Dialog 增加一个IDD_DIALOG1

VS2019, MFC 模态对话框与非模态对话框_第1张图片

3.2 右键 Dialog面板 为其增加CDialog 类CTestDlg

VS2019, MFC 模态对话框与非模态对话框_第2张图片

4. 增加菜单项,用于当点击菜单项时候 弹出新建的 对话框。设为非Popup类型,ID:ID_DIALOG

VS2019, MFC 模态对话框与非模态对话框_第3张图片

4.1 右键子菜单 ”对话框“ ,为其添加“事件处理程序(A)...”。把OnDialog放到view类中处理。

VS2019, MFC 模态对话框与非模态对话框_第4张图片

4.2 把CtestDlg.h 头文件放到view 类CPP 文件中,这样view CPP中就能找到CtestDlg。并在生成的OnDialog函数中增加语句:

void CSunXinjiaocheng07View::OnDialog()
{
	// TODO: 在此添加命令处理程序代码
    /*模态对话框创建
	CTestDlg dlg;
	dlg.DoModal();//程序执行到此处,将停止。直到对话框关闭。
	*/
	//非模态对话框,需要用其成员create来创建
	CTestDlg* dlg = new CTestDlg();//从对空间创建一个CTestDlg类,以解决程序退出类销毁问题。当然也可以给view类增加一个CTestDlg 成员.
	                               //这里并将为做Destory处理,请注意!
	dlg->Create(IDD_DIALOG1, this);//创建非模态对话框
	dlg->ShowWindow(SW_SHOW);//不调用显示是不会显示模态对话框的。
	
}

5. 对于模态对话框点击OK时,创建的窗口随类消失。而非模态对话框则不会。所以需要用(重写)虚函数OnOK成员函数,当点击OK按钮 就去调用DistroyWindow去销毁窗口。

注意:提供相关类函数说明

MFC Library Reference
CDialog::DoModal
Example  See Also  Send Feedback
 

 

Call this member function to invoke the modal dialog box and return the dialog-box result when done.

 
virtual INT_PTR DoModal( );

Return Value

An int value that specifies the value of the nResult parameter that was passed to the CDialog::EndDialog member function, which is used to close the dialog box. The return value is –1 if the function could not create the dialog box, or IDABORT if some other error occurred, in which case the Output window will contain error information from GetLastError.

Remarks

This member function handles all interaction with the user while the dialog box is active. This is what makes the dialog box modal; that is, the user cannot interact with other windows until the dialog box is closed.

If the user clicks one of the pushbuttons in the dialog box, such as OK or Cancel, a message-handler member function, such as OnOK or OnCancel, is called to attempt to close the dialog box. The default OnOK member function will validate and update the dialog-box data and close the dialog box with result IDOK, and the default OnCancel member function will close the dialog box with result IDCANCEL without validating or updating the dialog-box data. You can override these message-handler functions to alter their behavior.

Note:

PreTranslateMessage is now called for modal dialog box message processing.

Example

Visual C++  Copy Code
void CMyDialog::OnMenuShowAboutDialog()
{
   // Construct the dialog box passing the 
   // ID of the dialog template resource
   CDialog aboutDlg(IDD_ABOUTBOX);

   // Create and show the dialog box
   INT_PTR nRet = -1;
   nRet = aboutDlg.DoModal();

   // Handle the return value from DoModal
   switch (nRet)
   {
      case -1: 
         AfxMessageBox(_T("Dialog box could not be created!"));
         break;
      case IDABORT:
         // Do something
         break;
      case IDOK:
         // Do something
         break;
      case IDCANCEL:
         // Do something
         break;
      default:
         // Do something
         break;
   };
}

 

 Collapse AllExpand All      Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript 

Visual Basic
C#
Visual C++
J#
JScript

MFC Library Reference
CDialog::Create
Example  See Also  Send Feedback
 

 

Call Create to create a modeless dialog box using a dialog-box template from a resource.

 
virtual BOOL Create(
   LPCTSTR lpszTemplateName,
   CWnd* pParentWnd = NULL 
);
virtual BOOL Create(
   UINT nIDTemplate,
   CWnd* pParentWnd = NULL 
);

Parameters

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.

Return Value

Both forms return nonzero if dialog-box creation and initialization were successful; otherwise 0.

Remarks

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.

Example

Visual C++  Copy Code
void CMyDialog::OnMenuShowSimpleDialog()
{
   //m_pSimpleDialog initialized to NULL in the constructor of CMyDialog class
   m_pSimpleDlg = new CSimpleDlg();
   //Check if new succeeded and we got a valid pointer to a dialog object
   if(m_pSimpleDlg != NULL)
   {
      BOOL ret = m_pSimpleDlg->Create(IDD_SIMPLEDIALOG, this);

      if(!ret)   //Create failed.
         AfxMessageBox(_T("Error creating Dialog"));

      m_pSimpleDlg->ShowWindow(SW_SHOW);
   }
   else
   {
      AfxMessageBox(_T("Error Creating Dialog Object"));
   }
}

 

MFC Library Reference
CWnd::ShowWindow
Example  See Also  Send Feedback
 

 

Sets the visibility state of the window.

 
BOOL ShowWindow(
   int nCmdShow 
);

Parameters

nCmdShow

Specifies how the CWnd is to be shown. It must be one of the following values:

  • SW_HIDE   Hides this window and passes activation to another window.

  • SW_MINIMIZE   Minimizes the window and activates the top-level window in the system's list.

  • SW_RESTORE   Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.

  • SW_SHOW   Activates the window and displays it in its current size and position.

  • SW_SHOWMAXIMIZED   Activates the window and displays it as a maximized window.

  • SW_SHOWMINIMIZED   Activates the window and displays it as an icon.

  • SW_SHOWMINNOACTIVE   Displays the window as an icon. The window that is currently active remains active.

  • SW_SHOWNA   Displays the window in its current state. The window that is currently active remains active.

  • SW_SHOWNOACTIVATE   Displays the window in its most recent size and position. The window that is currently active remains active.

  • SW_SHOWNORMAL   Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.

Return Value

Nonzero if the window was previously visible; 0 if the CWnd was previously hidden.

Remarks

 

 

MFC Library Reference
CDialog::OnOK
Example  See Also  Send Feedback
 

 

Called when the user clicks the OK button (the button with an ID of IDOK).

 
virtual void OnOK( );

Remarks

Override this method to perform actions when the OK button is activated. If the dialog box includes automatic data validation and exchange, the default implementation of this method validates the dialog box data and updates the appropriate variables in your application.

If you implement the OK button in a modeless dialog box, you must override the OnOK method and call DestroyWindow inside it. Do not call the base-class method, because it calls EndDialog which makes the dialog box invisible but does not destroy it.

Note:

You cannot override this method when you use a CFileDialog object in a program that is compiled under Windows XP. For more information about CFileDialog, see CFileDialog Class.

Example

Visual C++  Copy Code
void CSimpleDlg::OnOK()
{
   // TODO: Add extra validation here

   // Ensure that your UI got the necessary input 
   // from the user before closing the dialog. The 
   // default OnOK will close this.
   if (m_nMyValue == 0) // Is a particular field still empty?
   {	
      // Inform the user that he can't close the dialog without
      // entering the necessary values and don't close the 
      // dialog.
      AfxMessageBox(_T("Please enter a value for MyValue"));
      return; 
   }

   CDialog::OnOK(); // This will close the dialog and DoModal will return.
}

你可能感兴趣的:(MFC)