0

我正在编写一个 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;
4

2 回答 2

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);
}
于 2013-03-10T05:52:25.913 回答
0

显示消息后,只需调用ExitProcess

于 2013-03-11T11:21:57.810 回答