What happens when you click the "Close" button in Windows?

Source: Internet
Author: User

For Windows operations, the form function receives a wm_destroy message when the user taps the Close button.

The form function should call PostQuitMessage (0) to insert a wm_quit message into the message queue. GetMessage assumes that the wm_quit message is obtained from the message queue, it returns 0, causing the message loop to end, the WinMain function exits, and the entire application exits.

Simply close the form and exit the application by tapping the Close button in the upper-right corner of the form. Could you please confirm our operation by popping up a dialog box before closing the application?

To do this, first understand what happens when you click the "Close" button to start, until the last application exits. In fact, this is a very "complex" process, with descriptive narratives such as the following:

1) Click the "Close" buttonin the upper right corner of the form to insert the WM_CLOSE message into the message queue;

2) Form function call DefWindowProc processing wm_close message : Call DestroyWindow () function;

3) The form closes and the WM_DESTROY message is inserted into the message queue;

4) Form function function processing WM_DESTROY message : Call postquitmessage function, insert wm_quit message to message queue;

5) The GetMessage in the message loop of the main function gets the wm_quit message returns 0, resulting in the end of the message loop , which in turn WinMain the function and ends the process .

We are able to see three messages from this process:wm_close, Wm_destroy, Wm_quit.

Wm_close on behalf of the user wants to end the application ; Wm_destroy represents the closing of the form ; Wm_quit represents the end of the process .

How do I bring up a dialog before closing the application to confirm our operation? Ability to add message handling to Wm_close in WndProc. The code is as follows:

form functions for message processing lresult CALLBACK WndProc (HWND hwnd,uint msg,wparam wparam,lparam LPARAM) {int Nsel=0;switch (msg) {case Wm_ Close:nsel=messagebox (hwnd,l "Are you really going to quit?" ", szwindowtitle,mb_yesno| Mb_iconquestion), if (Nsel==idyes) DestroyWindow (hWnd), return 0;case wm_destroy:postquitmessage (0); return 0;default: return DefWindowProc (Hwnd,msg,wparam,lparam);}}


Effects such as the following:

About form functions and message handling mechanisms: http://blog.csdn.net/sanqima/article/details/34427471

http://blog.csdn.net/sanqima/article/details/32179711

What happens when I click the "Close" button in Windows?

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.