2

I wrote the following WndProc. It is used by a Notify Icon. I removed the unimportant parts (like default labels) for an better overview.

When I click the Nofify Icon with the right mouse button, the context menu appears. When I click an item, I get the corresponding return value of TrackPopupMenu and print it out. However, TrackPopupMenu is a blocking call, but the WndProc is just working fine while the context menu is opened. Why?

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        hMenu = CreatePopupMenu();
        AppendMenu(hMenu, MF_STRING, ID_TRAY_EXIT_CONTEXT_MENU_ITEM, displayString);
    }
         break;
    case WM_TRAYICON:
        switch (lParam)
        {
        case WM_RBUTTONUP:
        {
            POINT curPoint;
            GetCursorPos(&curPoint);
            UINT clicked = TrackPopupMenu(
                hMenu,
                TPM_RETURNCMD | TPM_NONOTIFY,
                curPoint.x,
                curPoint.y,
                0,
                hWnd,
                NULL
                );

            std::cout << std::to_string(clicked) << std::endl;
        }
            break;
        }
        std::cout << lParam << std::endl;
        break;
    }
    return 0;
}
CalibeR.50
  • 370
  • 2
  • 13

1 Answers1

4

Because TrackPopupMenu is pumping messages while it is executing. That is, it has a message processing loop that calls DispatchMessage for any new messages posted to the thread's message queue, and DispatchMessage in turn calls your window procedures with the messages intended for each window.

jlahd
  • 6,257
  • 1
  • 15
  • 21