Header: Winuser.h
Library: User32.lib
DLL: User32.dll
BOOL TranslateMessage(
const MSG *lpMsg
);
lpMsg | Pointer to an MSG structure that contains message information retrieved from the calling thread's message queue. |
---|
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError
.
Note that TranslateMessage
always returns TRUE
if the message is not a keyboard message.
WM_KEYDOWN
, WM_KEYUP
) into character messages (WM_CHAR
).WM_CHAR
message to the thread's message queue.while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
TranslateMessage
for messages that are not keyboard messages.#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CHAR:
// Echo the typed character
{
wchar_t c = (wchar_t)wParam;
OutputDebugStringW(&c);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR, int)
{
const wchar_t CLASS_NAME[] = L"SampleWindow";
WNDCLASS wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0, CLASS_NAME, L"TranslateMessage Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 300,
NULL, NULL, hInst, NULL);
ShowWindow(hwnd, SW_SHOWDEFAULT);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}