最近在做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.