【问题标题】:messagebox display before window is destroyed窗口被销毁前的消息框显示
【发布时间】:2013-03-11 19:21:57
【问题描述】:

我正在编写一个 Win32 程序并尝试在终止程序之前显示一个消息框。我希望它显示错误,然后在用户读取错误并按 OK 后关闭。

这是我尝试过的:

MessageBoxA(hwnd, "An Error occured! Please restart the program and try again.", "Error", MB_ICONERROR);
PostQuitMessage(0);

MessageBoxA(0, "An Error occured! Please restart the program and try again.", "Error", MB_ICONERROR);
PostQuitMessage(0);

MessageBoxA(hwnd, "An Error occured! Please restart the program and try again.", "Error", MB_ICONERROR);
DestroyWindow(hwnd);

MessageBoxA(0, "An Error occured! Please restart the program and try again.", "Error", MB_ICONERROR);
DestroyWindow(hwnd);

hwnd 是我的应用程序的主(也是唯一)窗口。它不仅不显示消息框,也不会立即终止程序。我可以听到许多连续的哔哔声,好像正在创建许多消息框,但我看不到它们。

如何更改代码以使消息框出现,用户按 OK,然后程序立即终止?

我正在我的主 WndProc 中处理 WM_CLOSE 和 WM_DESTROY:

case WM_CLOSE:
    DestroyWindow(hwnd);
    return 0;

case WM_DESTROY:
    PostQuitMessage(0);
    return 0;

【问题讨论】:

  • 由于没有提供(提示:应该是),这是在您的主窗口句柄 proc 的 WM_DESTROY 处理程序中吗?如果是这样,您能否将其包含在您的 WndProc 中,或者将可重现的 WndProc 简化为相同的问题?
  • 我在上面发布的消息框代码根本不在 WndProc 中。该代码在其他地方,我检测到错误。但是我已经编辑了我的帖子,以包括我如何在我的主 WndProc 中处理 WM_CLOSE 和 WM_DESTROY。
  • 您是否调试过您的程序以确保 MessageBoxA() 函数正在运行,并且程序正在该行退出?
  • 如果它 is 被调用,如果 MB 调用返回 0 表示调用失败,您是否检查了 GetLastError()?它可能有助于确定问题。记录的返回代码是in the documentation of MessageBox()
  • 我在 MessageBox 调用上设置了一个断点,发生了一些奇怪的事情。按 F10(Step Over)后,它继续执行相同的 MessageBox 行 23 次,每次都发出哔哔声。我在 MessageBox() 调用之后调用了 GetLastError(),它返回 0。

标签: c++ windows winapi


【解决方案1】:

在这里,试试这个方法(你很容易提示响应,然后决定是否调用 EndDialog)

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"

HINSTANCE hInst;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {
    }
    return TRUE;

    case WM_CLOSE:
    {
        MessageBox(hwndDlg, "An Error occured! Please restart the program and try again.", "Error", MB_ICONERROR);
        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {
        }
    }
    return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

【讨论】:

    【解决方案2】:

    显示消息后,只需致电ExitProcess

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多