Windows編程非典型錯誤

來源:互聯網
上載者:User

 最近在做Windows編程,動手的時候有些小問題,貼在這裡以免重複犯錯.

#include <windows.h><br />LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br />int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)<br />{<br />WNDCLASS wd;<br />HWND hwnd;<br />MSG msg;<br />wd.style = CS_HREDRAW | CS_VREDRAW;<br />wd.lpfnWndProc = WndProc;<br />wd.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);<br />wd.cbClsExtra = 0;<br />wd.cbWndExtra = 0;<br />wd.hCursor = LoadCursor(NULL, IDC_ARROW);<br />wd.hIcon = LoadIcon(NULL, IDI_APPLICATION);<br />wd.lpszClassName = TEXT("Hello");<br />wd.lpszMenuName = NULL;<br />wd.hInstance = hInstance;</p><p>RegisterClass(&wd);</p><p>hwnd = CreateWindow(TEXT("hello"), TEXT("you "),<br />WS_OVERLAPPEDWINDOW | WS_VSCROLL,<br /> CW_USEDEFAULT, CW_USEDEFAULT,<br /> CW_USEDEFAULT, CW_USEDEFAULT,<br /> NULL, NULL, hInstance, NULL) ;</p><p>ShowWindow(hwnd, iCmdShow);</p><p>UpdateWindow(hwnd);</p><p>while(GetMessage(&msg, hwnd, 0, 0))<br />{<br />TranslateMessage(&msg);<br />DispatchMessage(&msg);<br />}</p><p>return msg.wParam;<br />}<br />LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)<br />{<br />switch(msg)<br />{<br />case WM_DESTROY:<br /> PostQuitMessage (0) ;<br /> return 0 ;<br /> }<br /> return DefWindowProc(hwnd, msg, wParam, lParam);<br />}

編譯通過,運行時貌似正常。可問題在於當單擊標題列右上方的關閉按鈕時,雖然整個頁面會被關閉,可是該進程卻並沒有被消滅,在工作管理員中仍舊能看到。

經過和某些範例的比較,終於發現問題出現在GetMessage(&msg, hwnd, 0, 0)這個語句中的第二個變數上。

MSDN中關於此變數的解釋為:

 

hWnd [in, optional]

Type: HWND

        

A handle to the window whose messages are to be retrieved. The window must belong to the current thread.

If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

          If hWnd is -1, GetMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by PostMessage (when the hWnd parameter is NULL) or PostThreadMessage.

 

         <Programming Windows> Chapter 3 describes what will happen when you click Close button in your program.

        Sometimes messages generate other message as a result of DefWindowProc processing. For example, suppose you eventually click the Close button, DefWindowProc processes this mouse input. When it detects that you have selected the Close option, it sends a WM_SYSCOMMAND message to the window procedure. WndProc passes this message to DefWindowProc. DefWindowProc responds by sending a WM_CLOSE message to the window procedure. WndProc again passes this message to DefWindowProc. DefWindowProc responds to the WM_CLOSE message by calling DestroyWindow. DestroyWindow causes Windows to send a WM_DESTROY message to the window procedure. WndProc finally responds to this message by calling PostQuitMesage to put a WM_QUIT message in the message queue. This message causes the message loop in WinMain to terminate and the program to end.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.