开发者

Execute code after a window is displayed

I'm working on a windows application where I'm implementing th开发者_运维技巧e 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?


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;
}


There isn't one because that's what WM_SHOWWINDOW is for. Once that mesage is passed to the default message handling procedure, the window will be shown. The best thing you could do is poll with IsWindowVisible via some sort of timer.

Your program design seems flawed though to have to rely on something like this. What are you trying to do?


As long as you are implementing the whole event loop and everything like that yourself, you can handle this directly in WinMain() like this:

HWND hWnd = CreateWindow(...);

if (!hWnd) return 0;

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

OnWindowJustDisplayed();    // here

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜