Three exit program messages: wm_close, wm_destroy, and wm_quit

Source: Internet
Author: User

1. Send sendmessage and postmessage messages

Postmessage puts the message into the message queue and returns immediately. sendmessage is not returned until the message is processed in the window.

2. Differences between the three messages

Wm_close:

Select "close" in the System menu or click "X" in the upper-right corner of the window, and you will receive wm_close during the window process. Defwindowproc calls destroywindow to process wm_close. Of course, you can not let defwindowproc process it, but handle it by yourself, for example, asking the user to save the changes. If you select "cancel" and ignore this message, the program runs as usual. If you confirm that you want to exit, you will call destroywindow.

Wm_destroy:

Next, destroywindow clears the window and sends wm_destroy as the window process. Defwindowproc does not process wm_destroy. That is to say, if you do not process this message, although your window has been destroyed, the process will not end. When wm_destroy is processed, resources are released (for example, the requested memory), and postquitmessage is called.

 

Wm_quit:

Postquitmessage sends wm_quit to the message queue. Note that wm_quit will never reach the window process, because after getmessage gets wm_quit, false is returned to end the message loop, the process ends, and the program exits.

 

 

Assume that the user executes hellowin and the user finally clicksCloseButton, or if the system menu is selected with a keyboard or mouseClose, Defwindowproc handles this keyboard or mouse input. After detecting that the user has selectedCloseOption, it sends a wm_syscommand message to the window message processing program. Wndproc sends the message to defwindowproc. Defwindowproc sends a wm_close message to the window message handler to respond. Wndproc sends it to defwindowproc again. Destroywindow calls destroywindow to respond to this wm_close message. Destroywindow causes windows to send a wm_destroy message to the window message handler. Wndproc then calls postquitmessage and puts a wm_quit message into the message queue to respond to the message. This message causes the message in winmain to terminate cyclically, and then the program ends.

3. Exit the Program Statement

Exit (0 );

Postquitmessage (0 );

Onok (); oncancel ();

Sendmessage (wm_close, 0, 0 );

Exitprocess (0 );

Exit (0) is the most rapid, in practice

 

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////////////////////

 

 

There are three messages that seem to be similar, all of which handle closed tasks. They are wm_close, wm_destroy, and wm_quit. They are similar, but you need to know the differences between them! A wm_close message should be sent when a window or application is closed. If you want to receive the wm_close message, you can ask the user whether to exit. A message box is usually displayed when you know that users are allowed to confirm or have errors or what should be paid attention.
Insert: Message Box
Int MessageBox (
Hwnd, // handle of owner window
Lptstr lptext, // address of text in message box
Maid, // address of title of message box
Uint utype // style of message box
);

1. When you receive the wm_close message, you can do two things. One is that you accept the default processing and return a value. If you do so, the application or window will be closed as planned; or, you will return 0, the application or window will remain unchanged. The following is the basic part of the Code:
If (MSG = wm_close)
{
If (MessageBox (hmainwindow, "Are you sure want to quit? "," Notice ", mb_yesno | mb_iconexclamation) = IDNO)
Return (0 );
// Otherwise, let the default handler take care of it
}

2. The message wm_destroy is a bit different. It is sent when the window is closed. When you receive the wm_destroy message, the window has been visually deleted. However, when a main window is closed, the application does not end because it can continue running without a window.

3. however, when a user closes the main window and wants it to end the application, if you want to do so, when receiving the wm_destroy message, you must send a wm_quit message. 4. wm_quit is the message sent when the application ends. Generally, it can be seen that the process is killed. 5. postquitmessage is a request to terminate the thread to the system. The system also needs to clear the memory before terminating the thread. When we close a program, we send the wm_close message (function sendmessage ?), Then call the destroywindow function. When destroywindow is called, The system sends a wm_destroy message to the program to terminate the entire program. **************************************** **************************************** **************************************** *****************One Dialog Box sends a window to another to close the message.Dialog Box A cadlg; Dialog Box B cbdlg

Declare B as the member variable cbdlg m_bdlg IN;

A. Disable sending messages B: sendmessage (m_bdlg.getsafehwnd (), wm_close, 0, 0 );
Or directly: m_bdlg.sendmessage (wm_close );
**************************************** **************************************** **************************************** **************************************** *****************

What are the differences between wm_destroy and wm_close functions?
An error occurred while executing the following program.
Void cmainframe: onclose ()
{
Cmdiframewnd: onclose ();
Cdocument * Doc;
Doc = This-> getactivedocument ();
}
No error occurs when the following program is executed,
Void cmainframe: ondestroy ()
{
Cdocument * Doc;
Doc = This-> getactivedocument ();
Cmdiframewnd: ondestroy ();
}
Cause analysis:
Wm_close is sent before the window is closed. You can also decide whether to close the window.
Wm_destroy is sent when the window is closed, and the window has been removed from the screen
The error in your program is that after you call cmdiframewnd: onclose (); the form has
Destroy dropped. The window object pointed to by this pointer does not exist, so an error occurs.
That is, the processing order is to process wm_close first (the window is not closed), and then process wm_destroy (the window is closed)
The part after cmdiframewnd: onclose (); is not executed. If you need to execute it, you can put it in ondestroy (), that is, your second segment.
When the parent class is called to handle cmdiframewnd: onclose () by default, the system sends
The wm_destroy message destroy the window, so this pointer can be used in ondestroy,
After cmdiframewnd: onclose () is generated, the window object pointed to by this pointer does not exist. Similarly:
Void cmainframe: onclose ()
{
Cdocument * Doc;
Doc = This-> getactivedocument ();
Cmdiframewnd: onclose ();
}
No error
An error occurred while executing the following program,
Void cmainframe: ondestroy ()
{
Cmdiframewnd: ondestroy ();
Cdocument * Doc;
Doc = This-> getactivedocument ();
}
The reason is as follows:
In onclose (), destorywindow () is called, while in destorywindow (), wm_destroy and wm_ncdestroy are sent; When destorywindow () is executed, ondestroy () and onncdestory () are also executed, after cmdiframewnd: onclose () is returned, the cmainframe object is released and the this pointer cannot be reused.

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.