Search code examples
winapievent-loop

Execute code after a window is displayed


I'm working on a windows application where I'm implementing the whole event loop and everything like that myself (there's a reason for that). In one place, I need to execute some code AFTER a window is shown. Normally, when the window is created, I do some initialisation when WM_CREATE message is received. WM_SHOWWINDOW is sent just BEFORE the window is displayed. However I need to get some code executed right AFTER the window is displayed for the first time. I can't seem to find a notification message sent AFTER the window is shown. Could it be that there isn't one?

Of course, I can keep a boolean - FirstRun - indicating whether or not I had performed my logic, and then execute the code when WM_ACTIVATE is received, provided the boolean is TRUE, then set FirstRun to FALSE so that the code is not execute the next time I am receive WM_ACTIVATE, but this seems somewhat unnatural to me.

It's been ages since I did win32 programming on this level, so can't remember much of it. What is the best approach here?


Solution

  • There is no special notification, but in many cases you can use this trick:

    LRESULT CALLBACK MainWndProc(
    HWND hwnd,        // handle to window
    UINT uMsg,        // message identifier
    WPARAM wParam,    // first message parameter
    LPARAM lParam)    // second message parameter
    { 
    switch (uMsg) 
    { 
        case WM_USER + 100:
            //window is just displayed, do some actions
            return DefWindowProc(hwnd, uMsg, wParam, lParam); 
        case WM_CREATE:
            PostMessage(hwnd, WM_USER + 100, 0, 0);
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
        default: 
            return DefWindowProc(hwnd, uMsg, wParam, lParam); 
    }
    return 0;
    }