显示窗口后执行代码

发布于 2024-12-05 15:37:35 字数 394 浏览 1 评论 0原文

我正在开发一个Windows应用程序,我正在其中实现整个事件循环以及类似的一切(这是有原因的)。在一个地方,我需要在显示窗口后执行一些代码。通常,当创建窗口时,我会在收到 WM_CREATE 消息时进行一些初始化。 WM_SHOWWINDOW 在窗口显示之前发送。但是,我需要在第一次显示窗口后立即执行一些代码。我似乎找不到在显示窗口后发送的通知消息。难道没有吗?

当然,我可以保留一个布尔值 - FirstRun - 指示我是否执行了我的逻辑,然后在收到 WM_ACTIVATE 时执行代码,前提是布尔值是 TRUE,然后将 FirstRun 设置为 FALSE,以便代码不执行下次我收到 WM_ACTIVATE 时,但这对我来说似乎有点不自然。

我已经很久没有在这个级别上进行过 win32 编程了,所以记不太清了。这里最好的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾铮苏瑾 2024-12-12 15:37:35

没有特别的通知,但在很多情况下你可以使用这个技巧:

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 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;
}
披肩女神 2024-12-12 15:37:35

没有一个,因为这就是 WM_SHOWWINDOW 的用途。一旦该消息被传递到默认消息处理过程,就会显示该窗口。您可以做的最好的事情是使用 IsWindowVisible 通过某种计时器。

尽管必须依赖这样的东西,但您的程序设计似乎有缺陷。你想做什么?

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?

浊酒尽余欢 2024-12-12 15:37:35

只要您自己实现整个事件循环和类似的所有内容,您就可以直接在 WinMain() 中处理它,如下所示:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文