while GetMessage(Msg, 0, 0, 0) do begin
TranslateMessage(Msg); { 鍵盤虛擬鍵碼 → 字符消息 }
DispatchMessage(Msg); { 分發到窗口回調 }
end;
GetMessage(var Msg, hWnd, wMsgFilterMin, wMsgFilterMax):
TranslateMessage:將 WM_KEYDOWN/UP 轉為 WM_CHAR 等字符消息,編輯框等控件依賴此機制DispatchMessage:調用窗口回調函數 WndProc只有收到 WM_QUIT 時 GetMessage 返回 0。在回調中調用 PostQuitMessage(exitCode) 向**當前線程**隊列投遞 WM_QUIT,exitCode 成為 WM_QUIT 的 wParam,最終可作為程序退出碼。
function WndProc(wnd: HWND; msg: UINT; wParam: Integer; lParam: Integer): Integer; stdcall;
begin
Result := 0;
if msg = WM_DESTROY then
PostQuitMessage(0)
else
Result := DefWindowProc(wnd, msg, wParam, lParam);
end.
處理開發者不關心的消息,返回回調所需的返回值,**必須調用**,否則窗口行為異常(無法移動、繪製、關閉等)。