不使用MFC在win32中保存/打开通用对话框
如何使用纯非托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在“输出”窗口中查看并观察 0xc0000005(AccessViolation 异常)的首次机会异常通知。 Wow64 模拟器中有一个后备程序,可以在调度 WM_CREATE 时吞掉异常。
该异常是由于未完全初始化 OPENFILENAMEA 结构引起的。快速修复:
并倾向于在调用 ShowWindow() 而不是 WM_CREATE 消息处理程序之前显示对话框。
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:
And favor displaying the dialog before calling ShowWindow() instead of the WM_CREATE message handler.
总体想法是正确的,但是如果您传递您作为所有者创建的窗口的句柄,那么它还不会被初始化。
对于诊断,请考虑创建变量来存储 API 函数返回值并在调试器中检查它们。
将结构初始化为零也更方便且不易出错,而不是显式将不需要的成员清零,如下所示:
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:
GetOpenFileName
阻塞(一段时间),然后如果通过“OK”关闭对话框则返回TRUE
,如果对话框被取消则返回FALSE
。实际结果(目录/文件路径)可以从
OPENFILENAME
结构中读取。GetOpenFileName
blocks (for a while), and then returns eitherTRUE
if the dialog was closed by 'OK', orFALSE
if it was cancelled.The actual result (the directory/file path) can be read from the
OPENFILENAME
structure.来自 https:// /learn.microsoft.com/en-us/windows/win32/dlgbox/using-common-dialog-boxes#opening-a-file 我们得到一个 utf-16 版本其中,我做了一些小改动:
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: