窗口被销毁之前的消息框显示

时间:2013-03-10 05:27:53

标签: c++ windows winapi

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

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

显示消息后,只需致电ExitProcess