The program cannot exit due to incorrect getmessage usage.

Source: Internet
Author: User
Tags textout
# Include <windows. h> # include <stdio. h> lresult callback winuoowproc (// callback function declaration hwnd, uint umsg, wparam, lparam); int winapi winmain (hinstance, // winmain main function hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {wndclass wndcls; // create a window class wndcls. cbclsextra = 0; wndcls. cbwndextra = 0; wndcls. hbrbackground = (hbrush) getstockobject (black_brush); wndcls. hcursor = loadcursor (null, idc_cross); wndcl S. hicon = loadicon (null, idi_error); wndcls. hinstance = hinstance; wndcls. lpfnwndproc = winuoowproc; wndcls. lpszclassname = "first window"; wndcls. lpszmenuname = NULL; wndcls. style = cs_dblclks | cs_hredraw | cs_vredraw; registerclass (& wndcls); // registration window hwnd; // create window hwnd = createwindow ("first window", "window by dancer_dus7 ", ws_overlappedwindow, 300,200,600,400, null, null, hinstance, null); showwindow (hwnd, sw_shownormal );/ /Display window updatewindow (hwnd); // update window MSG; while (getmessage (& MSG, null/* hwnd )) // note that if the comments are changed to hwnd, the program will still run in Task Manager {translatemessage (& MSG); dispatchmessage (& MSG);} return 0 ;} lresult callback winuoowproc (// callback function hwnd, uint umsg, wparam, lparam) {Switch (umsg) // Message Processing {Case wm_char: Char szchar [20]; sprintf (szchar, "char is % d", wparam); MessageBox (hwnd, szchar, "key code:", 0); break; Case wm_lbuttondow N: MessageBox (hwnd, "Mouse clicked! "," Mouse ", mb_ OK); HDC = getdc (hwnd); textout (HDC, 0, 50," Hello world! ", Strlen (" Hello world! "); Releasedc (hwnd, HDC); break; Case wm_paint: HDC; paintstruct pS; HDC = beginpaint (hwnd, & PS); textout (HDC, 0, 0, "First window! ", Strlen (" first window! "); Endpaint (hwnd, & PS); break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, umsg, wparam, lparam );} return 0 ;}

In the description in msdn, avoid the use of while (getmessage (lpmsg, hwnd, 0, 0) to avoid the result of-1. The window has been destroyed but is still running.

Note that the function return value can be nonzero, zero, or-1. Thus, you shoshould avoid code like this:

(Getmessage (lpmsg, hwnd, 0, 0 ))...

The possibility of a-1 return value means that such code can lead to fatal application errors.

While (getmessage (lpmsg, null, 0, 0) is correct.

 

However, I still don't think it is clear, that is, when the second parameter of getmessage is hwnd, the window is destroyed and the program is still running in the task management window, what does msdn not seem to understand? Hope you can answer your questions.

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.