How to restrict FileDialog to specific path

南笙酒味 提交于 2019-12-06 07:39:29

问题


Is it possible to restrict a file dialog(open/save) to a specific folder is winapi?

OPENFILENAME fileDialogSettings;
...
fileDialogSettings.lpstrInitialDir = "Some path";
...
if(GetOpenFileName(&fileDialogSettings))
{
}

I want to have "Some path" as root path in the dialog and to restrict navigation to this folder and it's sub folders only. May I use lpfnHook for this?


回答1:


If you're targeting Vista+ only, you can make use of the IFileDialogEvents::OnFolderChanging method to block the change altogether.

For older versions of Windows, the OpenFileDialog allows you to specify a hook procedure in which you can pick up on the CDN_FOLDERCHANGE notification. While I can't see any message to disallow the change, you may be able to post a message to tell it to go "back", or just disable the "OK" button.

Another option is to handle CDN_FILEOK notification and refuse paths outside your required directory.

See this MSDN article for more details about the hook procedure. This question also talks about changing the directory in an open dialog.




回答2:


Look into OFN_NOCHANGEDIR flag, although the documentation says this:

Restores the current directory to its original value if the user changed the directory while searching for files.

This flag is ineffective for GetOpenFileName.

Edit: Reading your question again, I guess you don't want the user to navigate up from that directory, not sure if this is possible with GetOpenFileName, you might have to create your own dialog with a directory list view and restrict them that way.



来源:https://stackoverflow.com/questions/10520570/how-to-restrict-filedialog-to-specific-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!