Win32/C 无法在 CreateWindow 之后立即显示消息框

发布于 2024-10-02 03:34:21 字数 1007 浏览 0 评论 0原文

我正在代码中创建我的应用程序窗口,并且我试图在窗口存在时立即显示一个消息框。但我不能。我只看到新创建的窗口,没有消息框。如果我通过关闭应用程序窗口来退出应用程序,则消息框会突然出现,就好像它在某个队列中等待一样,仅在应用程序窗口关闭时才显示。我创建窗口的方式是否会以某种方式阻止模态消息框?注意:MessageBox 行仅用于测试。我将把它拿出来进行正常使用,因为它显然会干扰 GetMessage 循环。

//start relevant section of WinMain:
WNDCLASS wc={0};
wc.lpfnWndProc = WindowProc;
...
if (!RegisterClass(&wc) || !CreateWindow("mc", "mc", WS_POPUPWINDOW|WS_CAPTION|WS_VISIBLE, 100, 50, 100, 100, NULL, NULL, hInstance, NULL)) {
  Error("Can't create window");
  return 0;
}
ShowWindow(win, SW_SHOWNORMAL);
MessageBox(0, "Test", 0 ,0);
while (GetMessage(&msg,NULL,0,0)>0) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}
//end relevant section of WinMain

long FAR PASCAL WindowProc(HWND h, UINT m, WPARAM wParam, LPARAM l)
{
    switch (m) {
        //process other messages
        case WM_CREATE:
            win=h;
            //init stuff, paint something in the main window
        break;
    }
    return DefWindowProc(h, m, wParam, l);
}

I'm creating my app window in code and I'm trying to show a message box as soon as the window exists. But I can't. I see only the newly created window, no msg box. If I quit the app by closing its window, the msg box suddenly appears, as if it were waiting in some queue, to be shown only when the app window is closed. Does the way I create the window somehow block modal msg boxes? Note: the MessageBox line is there just for testing. I'll take it out for normal use, as it would obviously interfere with the GetMessage loop.

//start relevant section of WinMain:
WNDCLASS wc={0};
wc.lpfnWndProc = WindowProc;
...
if (!RegisterClass(&wc) || !CreateWindow("mc", "mc", WS_POPUPWINDOW|WS_CAPTION|WS_VISIBLE, 100, 50, 100, 100, NULL, NULL, hInstance, NULL)) {
  Error("Can't create window");
  return 0;
}
ShowWindow(win, SW_SHOWNORMAL);
MessageBox(0, "Test", 0 ,0);
while (GetMessage(&msg,NULL,0,0)>0) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}
//end relevant section of WinMain

long FAR PASCAL WindowProc(HWND h, UINT m, WPARAM wParam, LPARAM l)
{
    switch (m) {
        //process other messages
        case WM_CREATE:
            win=h;
            //init stuff, paint something in the main window
        break;
    }
    return DefWindowProc(h, m, wParam, l);
}

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

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

发布评论

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

评论(1

千柳 2024-10-09 03:34:21

听起来您并没有像您应该的那样立即从 WM_CREATE 返回,但您的窗口的整个生命周期都在 CreateWindow 内。因此,在窗口死机之前,MessageBox 实际上不会被调用,并且尝试将 wnd 作为消息框的父级传递是无效的参数(窗口不再存在)。

您不应为 WM_CREATE 调用 DefWindowProcWindowProc 中不应该有消息循环(即 DispatchMessage)(例外:处理主窗口子窗口的模式对话框的消息循环)。

如果可能的话,应该避免窗口过程的重入。

It sounds like you're not returning immediately from WM_CREATE like you're supposed to, but your window's entire lifetime is inside CreateWindow. So MessageBox doesn't actually get called until your window is dead, and trying to pass wnd as the parent of the message box is an invalid argument (the window no longer exists).

You shouldn't call DefWindowProc for WM_CREATE. You shouldn't have a message loop (i.e. DispatchMessage) inside WindowProc (exception: a message loop handling a modal dialog that is a child of the main window).

Re-entrancy of window procedures is something to avoid if possible.

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