销毁窗口前显示的消息框

messagebox display before window is destroyed

本文关键字:消息 显示 窗口      更新时间:2023-10-16

我正在编写一个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,然后程序立即终止?

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

case WM_CLOSE:
    DestroyWindow(hwnd);
    return 0;
case WM_DESTROY:
    PostQuitMessage(0);
    return 0;

在这里,尝试这种方法(您可以轻松地提示响应,然后决定是否调用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);
}

显示消息后,只需调用ExitProcess