引言:今天看MicrosoftDoc關于CFileDialog的doModal函式回傳值的部分,提到了實際上MFC提供了錯誤資訊顯示, 個人技術博客(文章整理+原始碼): https://zobolblog.github.io/LearnWinAPI/ 1.用法: CFileDialog::DoModal的回傳值IDOK或IDCANCEL,如果回傳IDCANCEL,請呼叫Windows CommdLGextenderror函式以確定是否發生錯誤, CommDlgExtendedError函式可以回傳任何常見對話框函式的常規錯誤代碼,如果由于用戶關倍訓取消了對話框,公共對話框函式回傳FALSE,則回傳值為零,否則,回傳值為非零錯誤代碼,此外,還存在僅針對特定公用對話框回傳的錯誤代碼,所有這些錯誤代碼都在Cderr中定義,h、 對于任何常用對話框函式,都可以回傳以下常規錯誤代碼, 2.錯誤型別: CDERR_DIALOGFAILURE 0xFFFF:The dialog box could not be created. The common dialog box function's call to the DialogBox function failed. For example, this error occurs if the common dialog box call specifies an invalid window handle. CDERR_FINDRESFAILURE 0x0006:The common dialog box function failed to find a specified resource. CDERR_INITIALIZATION 0x0002:The common dialog box function failed during initialization. This error often occurs when sufficient memory is not available. CDERR_LOADRESFAILURE 0x0007:The common dialog box function failed to load a specified resource. CDERR_LOADSTRFAILURE 0x0005:The common dialog box function failed to load a specified string. CDERR_LOCKRESFAILURE 0x0008:The common dialog box function failed to lock a specified resource. CDERR_MEMALLOCFAILURE 0x0009:The common dialog box function was unable to allocate memory for internal structures. CDERR_MEMLOCKFAILURE 0x000A:The common dialog box function was unable to lock the memory associated with a handle. CDERR_NOHINSTANCE 0x0004:The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding instance handle. CDERR_NOHOOK 0x000B: The ENABLEHOOK flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a pointer to a corresponding hook procedure. CDERR_NOTEMPLATE 0x0003:The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding template. CDERR_REGISTERMSGFAIL 0x000C:The RegisterWindowMessage function returned an error code when it was called by the common dialog box function. CDERR_STRUCTSIZE 0x0001:The lStructSize member of the initialization structure for the corresponding common dialog box is invalid. 還有很多,可以看一下官方串列:https://docs.microsoft.com/en-us/windows/win32/api/commdlg/nf-commdlg-commdlgextendederror 3.相關網址: https://docs.microsoft.com/en-us/cpp/mfc/reference/cfiledialog-class?view=msvc-170#domodal https://docs.microsoft.com/en-us/windows/win32/api/commdlg/nf-commdlg-commdlgextendederror 4.個人理解:
由于MFC是90年代撰寫出來的,所以局限于當時的計算機性能,很多設計都比較保守,比如錯誤資訊沒法像QT一樣(不過qt也是91年發布的,好像)直接列印出來,而是不得不單獨再出一個函式進行顯示,而且只能顯示一個16bit的數字,不能顯示字串, 此外,是多個Dialog共用一個錯誤提示區域,官方檔案也寫了“ the most recent error”,就是這個函式CommDlgExtendedError把一個App類的公共成員變數顯示了出來,也就是說如果兩個視窗的關閉時間挨的特別近,兩個都錯誤了,前面一個的錯誤資訊就被覆寫了,這個完全就是設計歷史比較悠久,一個缺陷了, 原始碼:
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("All FILE|*.*|"),NULL,NULL,1); if (dlg.DoModal() != IDOK) { CommDlgExtendedError(); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/493029.html
標籤:其他
