2013-03-10 51 views
0

我正在寫一個Win32程序並試圖在終止程序之前顯示一個消息框。我希望它顯示錯誤,然後在用戶讀取錯誤並按下確定後關閉。窗口被破壞之前的消息框顯示

這是我曾嘗試:

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是我的應用程序的主(只)窗口。它不僅不顯示消息框,也不會立即終止程序。我可以聽到很多連續的嘟嘟聲,好像很多消息框正在創建,但我沒有看到它們。

如何更改代碼,使消息框出現,用戶按下確定,然後程序立即終止?

我處理WM_CLOSE和WM_DESTROY在我的主要的WndProc像這樣:

case WM_CLOSE: 
    DestroyWindow(hwnd); 
    return 0; 

case WM_DESTROY: 
    PostQuitMessage(0); 
    return 0; 
+0

因爲它沒有提供(提示:它應該是)這是在WM_DESTROY的處理程序爲您的主窗口句柄PROC?如果是這樣,你可以將它包含在你的WndProc中,還是可重複使用的WndProc解決相同的問題? – WhozCraig 2013-03-10 05:36:38

+0

我上面發佈的messagebox代碼根本不在WndProc中。該代碼在其他地方,我檢測到錯誤。但我已經編輯了我的帖子,包括我如何在我的主WndProc中處理WM_CLOSE和WM_DESTROY。 – user974967 2013-03-10 05:42:11

+1

您是否調試過程序以確保MessageBoxA()函數完全運行,並且該程序正在該行中退出? – Mmarss 2013-03-10 05:44:13

回答

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); 
} 
0

當您顯示信息後,只需致電ExitProcess即可。