To simplify the case, I edit the code from the Visual Studio New Projects/Visual C++/Windows Desktop/Windows Desktop Application. Just add RegisterHotKey
and case WM_HOTKEY
and hWnd_Main
from InitInstance
.
The MessageBox will appear as many times as I press Shift+Ctrl+Alt+Win+Z
. I thought that the messagebox will block the message loop thread(i.e. MainThread in Visual studio Thread Window) and the second time I press Shift+Ctrl+Alt+Win+Z
, the Message WM_HOTKEY should be waited in the message queue. How can a single thread simultaneously run multiple times? Is my understanding wrong?
How can I limit only one entry of MessageBox(hWnd_Main, _T("Test2"), _T("Test2"), NULL);
? I open a file after the messagebox, and find out it could open multiple times.
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
RegisterHotKey(hWnd_Main, 0x666, MOD_ALT | MOD_CONTROL | MOD_SHIFT | MOD_WIN, 'Z');
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_HOTKEY:
{
UINT nID = LOWORD(wParam);
if (nID == 0x666)
{
printf("GetCurrentThreadId: %d\n",GetCurrentThreadId());
MessageBox(hWnd_Main, _T("Test"), _T("Test"), NULL);
MessageBox(hWnd_Main, _T("Test2"), _T("Test2"), NULL);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}