1. Display dialog box
1.1 Display modal dialog box:
CDialogDemo dlg; dlg.DoModal();
1.2 Display non-modal dialog box:
CDialogDemo *dlg=new CDialogDemo(this); dlg->Create(IDD_GENERAL_CONTROL); dlg->ShowWindow(SW_SHOW);
2. Destroy dialog box
1.CDialog::OnOK(); // OK button pressed CDialog ::OnCancel(); //The cancel button is pressed
2.CDialog::DestoryWindow();
3.CDialog::EndDialog( IDD );
3. Close the own dialog in the modal dialog class Box
can close its own dialog box through PostMessage(WM_CLOSE) in the modal dialog class. After closing the MessageBox dialog box, the modal dialog box is closed.
The reference code is as follows:
<span style="font-size:18px;">BOOL CImportDataPromptDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here HANDLE hThread = StartImportDataThread(); if (!hThread) { LOG("启动导入数据线程失败") } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } DWORD WINAPI CImportDataPromptDialog::ImportDataThread(LPVOID Param) { CImportDataPromptDialog* importDlg = (CImportDataPromptDialog*)Param; if (CImportLog::ExtractionXMLData(importDlg->m_sXmlFilePath, importDlg->m_sDBPath)) { importDlg->MessageBox("导入数据成功","平台", MB_ICONINFORMATION); importDlg->PostMessage(WM_CLOSE); } else { importDlg->MessageBox("导入数据失败","平台", MB_ICONEXCLAMATION); importDlg->PostMessage(WM_CLOSE); } return 0; } HANDLE CImportDataPromptDialog::StartImportDataThread() { LOG("启动导入数据线程"); CWinThread* hThread = AfxBeginThread((AFX_THREADPROC)CImportDataPromptDialog::ImportDataThread,(LPVOID)this); return (hThread->m_hThread); }</span>
4. Detailed explanation of several related important functions
4.1 CloseWindow
function function: This function minimizes the specified window, but does not destroy the window.
Function prototype: BOOL CloseWindow(HWND hWnd);
Parameters: hWnd: handle of the window to be minimized.
Return value: If the function succeeds, the return value is non-zero; if the function fails, the return value is zero. If you want to get more error information, please call the GetLastError function.
Note: The window size is minimized to an icon and moved to the icon area of the screen. The system displays the window's icon without displaying the window, and displays the window title beneath the icon. Applications must use the DestroyWindow function to destroy the window.
4.2 DestroyWindow
Function: Destroy the specified window. This function invalidates the window and removes its keyboard focus by sending the WM_DESTROY message and the WM_NCDESTROY message. This function also destroys the window's menu, clears the thread's message queue, destroys the timer related to the window process, releases the window's ownership of the clipboard, and interrupts the clipboard viewer's viewing chain.
Function prototype: BOOL DestroyWindow(HWND hWnd // handle to window to destroy);
hWnd: handle of the window to be destroyed.
Return value: If the function succeeds, the return value is non-zero: If the function fails, the return value is zero. If you want to get more error information, please call the GetLastError function.
Note: A thread cannot use this function to destroy windows created by other threads. If this window is a child window that does not have the WS_EX_NOPARENTNOTIFY style, the WM_PARENTNOTIFY message will be sent to its parent window when the window is destroyed. Windows CE: This function will not send the WM_NCDESTROY message.
4.3 EndDialog
Function: This function clears a modal dialog box and causes the system to terminate any processing of the dialog box.
Function prototype: BOOL EndDialog(HWND hDlg, int nResult);
Parameters: hDlg: Indicates the dialog window to be cleared. NResult: Specifies the value returned to the application from the create dialog function.
Return value: If the function call is successful, the return value is non-zero; if the function call fails, the return value is zero. If you want to get the error information, please call the GetLastError function.
Note: Dialog boxes created by DialogBox, DialogBoxParam, DialogBoxlndirect and DialogBoxlndirectParam functions must be cleared using the EndDialog function. The application calls the EndDialog function from within the dialog box application and this function cannot be used for other purposes. A dialog box application can call the EndDialog function at any time; even during WM_INITDIALOG message processing. If the application calls this function during WM_INTDIALOG message processing, the dialog box is cleared before display and input focus are set. The EndDialog function does not clear the dialog box immediately. Instead, it sets a flag and allows the dialog box application to return control to the system. The system detects the flag before attempting to retrieve the next message from the application queue. If the flag has been set, the system terminates the message loop, clears the dialog box, and uses the value in nResUlt as the value returned from the function that created the dialog box.