5.返回值IDCANCEL和CommDlgExtendedError函数 -windows编程


 引言:今天看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();
    }

 

原创文章,作者:jamestackk,如若转载,请注明出处:https://blog.ytso.com/268423.html

(0)
上一篇 2022年6月20日
下一篇 2022年6月20日

相关推荐

发表回复

登录后才能评论