參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32
http://masm32.com/board/index.php?topic=3584.0
https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
當視窗有事件需要被處理時,系統會透過呼叫註冊的Callback副程式來處理,而這個Callback副程式就是在註冊Class時,設定給lpfnWndProc的事件處理副程式,因此,當這個事件處理副程式被呼叫時,可以處理自己感興趣的事件,而其餘事件則交給系統處理(透過呼叫DefWindowProc()),需要注意的是,例如:WM_CLOSE,就必須由該視窗自己處理,因為系統無法知道還有哪些額外的資源需要被釋放掉
main.c
#include <windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CLOSE: DestroyWindow(hWnd); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wndClass = {0}; const char *className = "main"; wndClass.lpfnWndProc = WndProc; wndClass.lpszClassName = className; RegisterClass(&wndClass); CreateWindow(className, "main", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 300, 300, NULL, NULL, NULL, NULL); MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return 0; }
Line 6~11:關閉視窗的順序為:主視窗收到WM_CLOSE事件時,呼叫DestroyWindow(),DestroyWindow()會自動將子視窗的相關資源也一併釋放掉(包含Resource描述的資源),系統接著會發送WM_DESTROY事件給主視窗,待主視窗收到WM_DESTROY事件時,呼叫PostQuitMessage()結束視窗,值得注意的地方是,SendMessage()是屬於Block方式呼叫(必須等待動作執行完畢才返回),而PostQuitMessage()則是Non-block方式呼叫
Line 13:其餘事件由系統代為處理
編譯、執行
$ i686-w64-mingw32-gcc main.c -o main.exe $ wine ./main.exe