Exit of Windows Application

Source: Internet
Author: User
Tags getmessage

exit of Windows Application

Flyfish
The relationship between OnOK OnCancel EndDialog

In the Vc\atlmfc\src\mfc\dlgcore.cpp file
void Cdialog::onok () {if (! UpdateData (TRUE)) {TRACE (traceappmsg, 0, "UpdateData failed during dialog termination.\n");//The UpdateData routine would Set focus to correct itemreturn;} EndDialog (IDOK);} void Cdialog::oncancel () {EndDialog (IDCANCEL);} void Cdialog::enddialog (int nresult) {ASSERT (:: IsWindow (m_hwnd)); if (M_nflags & (wf_modalloop| Wf_continuemodal) Endmodalloop (Nresult);:: EndDialog (M_hwnd, nresult);}


Message routing is
onok-"EndDialog
oncancel-"EndDialog

When the user clicks the OK or Cancel button, they receive two messages Idok or IDCANCEL, execute OnOK or OnCancel, and then call function EndDialog to end the dialog box.


Win32 exit of the application
Main message loop: while (GetMessage (&msg, NULL, 0, 0)) {if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg);D ispatchmessage (&msg);}} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {int wmid, wmevent; Paintstruct PS; HDC hdc;switch (message) {case Wm_command:wmid    = LoWord (wParam); wmevent = HiWord (WParam);//Analysis Menu selection: switch (wmid) { Case Idm_about:dialogbox (HInst, Makeintresource (Idd_aboutbox), hwnd, about); Break;case Idm_exit:destroywindow (HWND) ; Break;default:return DefWindowProc (hWnd, message, WParam, LParam);} Break;case WM_PAINT:HDC = BeginPaint (hWnd, &ps);//TODO: Add any drawing code here ... EndPaint (hwnd, &PS); Break;case wm_destroy:postquitmessage (0); Break;default:return DefWindowProc (hwnd, message, WParam, LParam);} return 0;}




1 while the program is running, it constantly crawls messages from the message queue with GetMessage. If the message is Wm_quit,getmessage returns 0 and ends the while loop, the entire program is ended.
2 DestroyWindow Send Wm_destroy message
3 The standard response of the program to Wm_destroy is to call PostQuitMessage.
4 PostQuitMessage sends the WM_QUIT message, prepares the getmessage in the message loop to get the message, and ends the message loop.
Message routing is
destroywindow-"wm_destroy-" postquitmessage-"Wm_quit


The WM_CLOSE message is sent when you click the title bar Close button, and the WM_CLOSE message calls the DestroyWindow function.
Message routing is
wm_close-"DestroyWindow



Exiting the application in a thread


In a dialog-based program, create a thread afxbeginthread (processthread,this); thread function uint Cxdlg::P rocessthread (LPVOID pparam) {Cxdlg *pdlg= Static_cast<cxdlg*> (Pparam); if (NULL==PDLG) return 1;//call Exit Function here}



Exit function regardless of execution OnOK (), OnOK () or postquitmessage (0); The application has not exited
The window pointer is passed in the thread, not the window handle
while (GetMessage (Lpmsg, hwnd, 0, 0)) is equivalent to an HWND that is not valid
The PostQuitMessage function sends a WM_QUIT message to the thread message queue and returns immediately. This function simply notifies the system thread to request an immediate exit.
The parameter HWND in GetMessage is an invalid window handle or when LPMSG is a null pointer. The GetMessage return value is-1.

So need to get handle, call AfxGetMainWnd ()->sendmessage (WM_CLOSE);


Exit of Windows Application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.