Workaround for Windows programming about "Close window cannot quit process"

Source: Internet
Author: User

There are two general scenarios

1. In the WinMain function, the last stage receives the message queue loop, and the GetMessage function parameter of the call provides an error
Such as:

while (GetMessage (&msg,hwnd, 0,0))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}

This error is often due to the negligence of the programmer, the individual compiler will have automatic completion (hint) function, will prompt the GetMessage function of the second parameter is an HWND type parameters, according to the habit, easy to write directly to the HWND, which is not right, in general, The second parameter is set to NULL, or null;

2, the message loop itself appears logic error
Such as:

while (TRUE)
{
if (GetMessage (&msg, NULL, 0, 0))
{
if (msg.message==wm_quit) {break;}
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
}

While the judgment part of the loop is always true, the program will not quit the process even if it accepts the exit message, which is a logic error and a programming habit, and it is generally recommended to put the return value of the GetMessage function directly into the while as a conditional statement to reduce errors.

Therefore, the correct wording should be written as:

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
return&msg.wparam;

Workaround for Windows programming about "Close window cannot quit process"

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.