0

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;
}
Kishimo
  • 93
  • 9
  • 3
    You are misunderstanding how modal windows work. If it blocked the message loop of the owning window how would that window repaint itself? You should research it a bit more. – Captain Obvlious Dec 29 '17 at 03:55
  • 3
    Helpful reading: [If MessageBox()/related are synchronous, why doesn't my message loop freeze?](https://stackoverflow.com/questions/1256963/if-messagebox-related-are-synchronous-why-doesnt-my-message-loop-freeze/1256988#1256988) – Raymond Chen Dec 29 '17 at 04:19
  • @RaymondChen Thanks for your link. How can I limit only one executing after the messagebox if I open a file after user clicking the messagebox? – Kishimo Dec 29 '17 at 06:52
  • 2
    Set a flag when you first handle the input, reset that flag when you finish. – David Heffernan Dec 29 '17 at 09:02

0 Answers0