为什么Delphi TOpenDialog无法在初始目录中打开?
我正在使用 TOpenDialog
(在 Delphi 10.4 中)向用户显示我在其 Documents 文件夹中为他们安装的 PDF 文件。在该文件夹中,我创建了一个文件夹 MyFolder10.2
并将 PDF 文件复制到其中。
代码很简单并且过去一直有效,即使现在它仍然可以在我较旧的较慢的 Win10 机器上运行。但在我的较新、更快的 Win10 计算机上,它只能在某些时候工作。当它不起作用时,会打开一个文件对话框,但在其他目录中(不确定它来自哪里),并且它不会过滤 .pdf
中设置的文件类型(.pdf
)代码>TOpenDialog组件。
有什么办法可以解开这个谜团吗?
docPath:= GetEnvironmentVariable('USERPROFILE') + '\Documents\MyFolder10.2\';
OpenDocsDlg.InitialDir := docPath;
OpenDocsDlg.Execute;
I am using TOpenDialog
(in Delphi 10.4) to show the user the PDF files I have installed for them in their Documents folder. In that folder, I have created a folder MyFolder10.2
and copied the PDF files there.
The code is simple and has worked in the past, and even now it still works on my older slower Win10 machine. But on my newer faster Win10 computer, it only works SOME of the time. When it does not work, a file dialog is opened but in some other directory (not sure where that comes from), and it does not filter the file type (.pdf
) that was set in the TOpenDialog
component.
Any way to track down this mystery?
docPath:= GetEnvironmentVariable('USERPROFILE') + '\Documents\MyFolder10.2\';
OpenDocsDlg.InitialDir := docPath;
OpenDocsDlg.Execute;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Vista 之前,
TOpenDialog
是GetOpenFileName()
API,其中TOpenDialog.InitialDir
映射到OPENFILENAME.lpstrInitialDir
字段。在 Vista 及更高版本上,
TOpenDialog
(通常,取决于配置)包装IFileDialog
/IFileOpenDialog
相反,API,其中TOpenDialog.InitialDir
映射到IFileDialog.SetFolder()
方法(不是
IFolderDialog.SetDefaultFolder()
,正如人们所期望的那样)。
根据
OPENFILENAME
文档:并根据常见项目对话框文档:
既不是
TOpenDialog
也不是TFileOpenDialog
具有映射到IFileDialog.SetDefaultFolder()
或IFileDialog.SetSaveAsItem()
方法的属性。但是,TFileOpenDialog.Dialog
属性确实使您可以访问底层的IFileDialog
,因此您可以手动调用这些方法,例如在TFileOpenDialog.OnExecute
事件中。Prior to Vista,
TOpenDialog
is a wrapper for theGetOpenFileName()
API, whereTOpenDialog.InitialDir
maps to theOPENFILENAME.lpstrInitialDir
field.On Vista and later,
TOpenDialog
(usually, depending on configuration) wraps theIFileDialog
/IFileOpenDialog
API instead, whereTOpenDialog.InitialDir
maps to theIFileDialog.SetFolder()
method (not toIFolderDialog.SetDefaultFolder()
, as one would expect).Per the
OPENFILENAME
documentation:And per the Common Item Dialog documentation:
Neither
TOpenDialog
norTFileOpenDialog
have properties that map to theIFileDialog.SetDefaultFolder()
orIFileDialog.SetSaveAsItem()
methods. However, theTFileOpenDialog.Dialog
property does give you access to the underlyingIFileDialog
, so you can manually call these methods, such as in theTFileOpenDialog.OnExecute
event.@Remy Lebeau 的详细回答促使我再次尝试修复一个有问题的常见场景,尽管我进行了多次尝试,但我之前尚未成功修复:
我有一个带有 TFileOpenDialog 和 TFileSaveDialog 的图像编辑 VCL 应用程序,通常用于在连接的 Android 设备上逐个编辑一堆屏幕截图,并将其保存在台式计算机上。
问题是在计算机上保存后,FileOpenDialog 的 DefaultFolder 没有粘贴到打开的 Android 文件夹中。现在已根据我的喜好进行了修复,并且需要使用对话框的人可能会对解决方案感兴趣。
测试代码:
The detailed answer by @Remy Lebeau triggered me to try again to fix a problematic common scenario I've not managed to fix before, despite numerous attempts:
I have an image edit VCL application with a TFileOpenDialog and a TFileSaveDialog that is often used for editing a bunch of screenshots one by one on a connected Android device which are saved on a desktop computer.
The problem was that the DefaultFolder for the FileOpenDialog didn't stick to the opened Android folder after a save on the computer. That is now fixed to my liking, and the solution might be of interest to someone who needs to play around with the dialogs.
The test code: