limit directory with GetOpenFileName()

点点圈 提交于 2020-01-03 04:09:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13443660/limit-directory-with-getopenfilename

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