不使用MFC在win32中保存/打开通用对话框

发布于 2024-12-09 19:38:15 字数 923 浏览 0 评论 0原文

如何使用纯非托管 Win32 API 创建默认的“保存/打开”对话框? 请按照此处,在主窗口的消息循环中处理WM_CREATE消息时执行以下代码: 我还包含了

            OPENFILENAMEA ofn;
        char Buffer[300];
        fill(Buffer, Buffer + 300, '\0');
        ofn.lStructSize = sizeof(OPENFILENAMEA);
        ofn.hwndOwner = hWnd;
        ofn.lpstrFile = Buffer;
        ofn.nMaxFile = 300;
        ofn.Flags = OFN_EXPLORER;
        ofn.lpstrFilter = NULL;
        ofn.lpstrCustomFilter = NULL;
        ofn.nFilterIndex = 0;
        ofn.lpstrFileTitle = NULL;
        ofn.lpstrInitialDir = NULL;
        ofn.lpstrTitle = NULL;
        out << GetOpenFileNameA(&ofn) << endl;
        out << Buffer << (int)CommDlgExtendedError();

然而,这段代码给出了NO输出。帮助?!

How do you create the deafault Save/Open dialog boxes using pure unmanaged Win32 API ?
Following the guide here, the following code is executed when WM_CREATE message is handled in the message loop of the main window:
Ive included <Commdlg.h> also.

            OPENFILENAMEA ofn;
        char Buffer[300];
        fill(Buffer, Buffer + 300, '\0');
        ofn.lStructSize = sizeof(OPENFILENAMEA);
        ofn.hwndOwner = hWnd;
        ofn.lpstrFile = Buffer;
        ofn.nMaxFile = 300;
        ofn.Flags = OFN_EXPLORER;
        ofn.lpstrFilter = NULL;
        ofn.lpstrCustomFilter = NULL;
        ofn.nFilterIndex = 0;
        ofn.lpstrFileTitle = NULL;
        ofn.lpstrInitialDir = NULL;
        ofn.lpstrTitle = NULL;
        out << GetOpenFileNameA(&ofn) << endl;
        out << Buffer << (int)CommDlgExtendedError();

However, this code gives NO output whatsoever. Help?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2024-12-16 19:38:15

处理WM_CREATE消息时执行以下代码

在“输出”窗口中查看并观察 0xc0000005(AccessViolation 异常)的首次机会异常通知。 Wow64 模拟器中有一个后备程序,可以在调度 WM_CREATE 时吞掉异常。

该异常是由于未完全初始化 OPENFILENAMEA 结构引起的。快速修复:

 OPENFILENAMEA ofn = {0};

并倾向于在调用 ShowWindow() 而不是 WM_CREATE 消息处理程序之前显示对话框。

the following code is executed when WM_CREATE message is handled

Look in the Output window and observe the first-chance exception notification for 0xc0000005, an AccessViolation exception. There's a backstop in the Wow64 emulator that swallows exceptions while WM_CREATE is being dispatched.

The exception is caused by not fully initializing the OPENFILENAMEA structure. Quick fix:

 OPENFILENAMEA ofn = {0};

And favor displaying the dialog before calling ShowWindow() instead of the WM_CREATE message handler.

秉烛思 2024-12-16 19:38:15

总体想法是正确的,但是如果您传递您作为所有者创建的窗口的句柄,那么它还不会被初始化。

对于诊断,请考虑创建变量来存储 API 函数返回值并在调试器中检查它们。

将结构初始化为零也更方便且不易出错,而不是显式将不需要的成员清零,如下所示:

OPENFILENAME ofn = { 0 };

The overall idea is right, but if you are passing the handle of the window you are creating as the owner, then it is not going to be initialized yet.

For diagnostics, consider creating variables to store the API function return values and examining them in the debugger.

It is also more convenient and less error-prone to initialize the structure to zero, instead of explictely zeroing out unneeded members, like this:

OPENFILENAME ofn = { 0 };
唠甜嗑 2024-12-16 19:38:15

GetOpenFileName 阻塞(一段时间),然后如果通过“OK”关闭对话框则返回 TRUE,如果对话框被取消则返回 FALSE

实际结果(目录/文件路径)可以从OPENFILENAME结构中读取。

GetOpenFileName blocks (for a while), and then returns either TRUE if the dialog was closed by 'OK', or FALSE if it was cancelled.

The actual result (the directory/file path) can be read from the OPENFILENAME structure.

别在捏我脸啦 2024-12-16 19:38:15

来自 https:// /learn.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes#opening-a-file 我们得到一个 utf-16 版本其中,我做了一些小改动:

OPENFILENAME ofn = { 0 };       // common dialog box structure
WCHAR szFile[260];       // buffer for file name 
HWND hwnd;              // owner window
HANDLE hf;              // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box. 

if (GetOpenFileName(&ofn)==TRUE) 
    hf = CreateFile(ofn.lpstrFile, 
                    GENERIC_READ,
                    0,
                    (LPSECURITY_ATTRIBUTES) NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    (HANDLE) NULL);

from https://learn.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes#opening-a-file we get a utf-16 version of this, with some small changes of mine:

OPENFILENAME ofn = { 0 };       // common dialog box structure
WCHAR szFile[260];       // buffer for file name 
HWND hwnd;              // owner window
HANDLE hf;              // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box. 

if (GetOpenFileName(&ofn)==TRUE) 
    hf = CreateFile(ofn.lpstrFile, 
                    GENERIC_READ,
                    0,
                    (LPSECURITY_ATTRIBUTES) NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    (HANDLE) NULL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文