CFileDialog - 选定的过滤器和“提示”显示所有文件
我有 CFileDialog 并为其设置过滤器(文本文件 *.txt)。
当它打开时,我只看到 TXT 文件,没错。但!当我在文件名中输入文本时,提示(在文件名字段下)显示所有文件(具有任何扩展名的文件)。
某些标志可以改变这种行为吗?我希望强制提示仅显示 TXT 文件。
...
CFileDialog f(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_NOCHANGEDIR,_T("Text files (*.txt)|*.txt|All files (*.*)|*.*||"));
if( f.DoModal() != IDOK ) return;
...
I have CFileDialog and set filter for it (Text files *.txt).
When it opens, I see only TXT files, thats right. But! when I'm typing text into filename, the hint (under filename field) is showing all files (files with any extension).
Can be this behavior changed by some flag? I want force hint to show only TXT files.
...
CFileDialog f(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_NOCHANGEDIR,_T("Text files (*.txt)|*.txt|All files (*.*)|*.*||"));
if( f.DoModal() != IDOK ) return;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的经验是,文件过滤器将控制文件列表中显示的内容,但是当您键入时,自动完成功能会与当前目录中的所有内容相匹配。我想不出一个好方法来证明它无法完成,但我没有在 MFC 文档或代码中看到任何可以让您做到这一点的内容。
您可以子类化 CFileDialog 并重写 CFileDialog::OnFileNameOK() 函数以拒绝输入任何与您的条件不匹配的文件名。您还可以通过重写 CFileDialog::OnFileNameChange() 来获得所需的功能,以在单击“打开”(或“保存”)按钮之前拒绝用户提供的文件名,但我自己还没有这样做,不知道具体会如何锻炼。
My experience with this is that the file filter will control what is shown in the list of files, but when you type the auto-complete is matching against everything in the current directory. I can't think of a good way to prove that it can't be done, but I haven't seen anything in the MFC docs or code that would let you do that.
You can subclass the CFileDialog and override the CFileDialog::OnFileNameOK() function to reject the entry of any file name that doesn't match your criteria. you might also be able to get the functionality you want by overriding CFileDialog::OnFileNameChange() to reject a user supplied file name before they click the Open (or Save) button, but I have not done that myself to know exactly how it would work out.