GetOpenFileName 能否将文件选择限制为与过滤器匹配的文件?

Can GetOpenFileName limit the file selection to files that match the filter?

我将 GetOpenFileName 与 OPENFILENAME 结构一起使用,该结构使用 lpstrFilter 将文件过滤为 *.JPG。但是,在文件打开对话框中,对于文件名,用户可以键入 *.XLS 并查看同一文件夹中的每个 Excel 文件,然后选择打开 XLS 文件。我想阻止用户这样做。例如,我想真正限制它们只能选择 JPG 文件。有办法吗?

OPENFILENAME结构中,启用OFN_EXPLOREROFN_ENABLEHOOK标志,并提供指向Explorer-style callback function in the lpfnHook field. When the callback receives the CDN_FILEOK通知的指针,您可以验证输入的文件(s) )(不只是文件名,甚至是实际文件内容,如果需要的话)然后 return 适当的 return 值到 accept/reject 选择:

If the hook procedure returns zero, the dialog box accepts the specified file name and closes.

To reject the specified file name and force the dialog box to remain open, return a nonzero value from the hook procedure and call the SetWindowLong function to set a nonzero DWL_MSGRESULT value.