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

Source: Internet
Author: User

For Windows Operations, the window function receives a wm_destroy message when the user taps the Close button.

The window function should call PostQuitMessage (0) to insert a wm_quit message into the message queue. GetMessage If a wm_quit message is obtained from the message queue, it returns 0, which causes the message loop to end, the WinMain function exits, and the entire application exits.

Simply close the window and exit the application by clicking the Close button in the upper right corner of the window. 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 end of the application exits. In fact, this is a very "complex" process, described below:

1) Click the "Close" button in the upper right corner of the window to insert the WM_CLOSE message into the message queue;

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

3) The window is closed and the WM_DESTROY message is inserted into the message queue;

4) window 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 can 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 window ; Wm_quit represents the end of the process .

How do I bring up a dialog before closing the application to confirm our operation? You can add message processing to the wm_close in WndProc. The code is as follows:

window 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);}}


The effect is as follows:

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

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

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.