限制目录与GetOpenFileName() [英] limit directory with GetOpenFileName()
问题描述
我知道Microsoft建议使用 GetOpenFileName()
和 GetSaveFileName()
的公共项目对话框,但我
如何限制 GetOpenFileName()
只允许用户选择文件在某个目录中?
例如,我只希望用户能够从Images目录中选择一个文件。我不希望他们在打开文件时能够导航到其他目录。他们必须从Images目录中选择一个文件。
标志 OFN_NOCHANGEDIR 听起来像是我需要的,
如果不能使用 和 更新 $ c> IFileOpenDialog GetOpenFileName()$
和 GetOpenFileName()可以使用新的Common Item对话框
GetSaveFileName()
,您可以将分配一个回调过程到 OPENFILENAME :: lpfnHook
领域。当它收到 CDN_FILEOK
通知,通过发送对话框检索所选文件夹 CDM_GETFOLDERPATH
消息。如果您不能接受该文件夹,请向用户显示一条消息,然后通过调用 SetWindowLong()
来拒绝所选文件,以将非零值设置为 DWL_MSGRESULT
值,并从钩子过程返回非零值。
IFileSaveDialog
,您可以实现 IFileDialogEvents :: OnFolderChanging
事件回调以防止导航到不需要的文件夹,错误 HRESULT
除以外的值S_OK
或 E_NOTIMPL
p>
I know that Microsoft recommends using the Common Item Dialog boxes over GetOpenFileName()
and GetSaveFileName()
but I am working with some older code.
How can I limit GetOpenFileName()
to only allow the user to select files in a certain directory?
For example, I only want the user to be able to select a file from the "Images" directory. I don't want them to be able to navigate into other directories when opening a file. They must select a file from the "Images" directory.
The flag OFN_NOCHANGEDIR sounds like it could be what I need, but that just changes the current directory back to the original directory if the user changes it when opening a file.
If it's not possible with GetOpenFileName()
, is it possible with the newer Common Item Dialog?
For GetOpenFileName()
and GetSaveFileName()
, you can assign a callback procedure to the OPENFILENAME::lpfnHook
field. When it receives a CDN_FILEOK
notification, retrieve the selected folder by sending the dialog a CDM_GETFOLDERPATH
message. If the folder is not acceptable to you, display a message to the user and then reject the selected file(s) by calling SetWindowLong()
to set a nonzero DWL_MSGRESULT
value, and return a nonzero value from the hook procedure.
Update: for IFileOpenDialog
and IFileSaveDialog
, you can implement the IFileDialogEvents::OnFolderChanging
event callback to prevent navigation to unwanted folders, by simply returning an error HRESULT
value other than S_OK
or E_NOTIMPL
.
这篇关于限制目录与GetOpenFileName()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!