Win32/C 无法在 CreateWindow 之后立即显示消息框
我正在代码中创建我的应用程序窗口,并且我试图在窗口存在时立即显示一个消息框。但我不能。我只看到新创建的窗口,没有消息框。如果我通过关闭应用程序窗口来退出应用程序,则消息框会突然出现,就好像它在某个队列中等待一样,仅在应用程序窗口关闭时才显示。我创建窗口的方式是否会以某种方式阻止模态消息框?注意: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您并没有像您应该的那样立即从
WM_CREATE
返回,但您的窗口的整个生命周期都在CreateWindow
内。因此,在窗口死机之前,MessageBox 实际上不会被调用,并且尝试将wnd
作为消息框的父级传递是无效的参数(窗口不再存在)。您不应为
WM_CREATE
调用DefWindowProc
。WindowProc
中不应该有消息循环(即DispatchMessage
)(例外:处理主窗口子窗口的模式对话框的消息循环)。如果可能的话,应该避免窗口过程的重入。
It sounds like you're not returning immediately from
WM_CREATE
like you're supposed to, but your window's entire lifetime is insideCreateWindow
. So MessageBox doesn't actually get called until your window is dead, and trying to passwnd
as the parent of the message box is an invalid argument (the window no longer exists).You shouldn't call
DefWindowProc
forWM_CREATE
. You shouldn't have a message loop (i.e.DispatchMessage
) insideWindowProc
(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.