3

Vista introduced an interface: IFileDialog::SetFilter, which allows me to setup a filter that will be called for every potential filename to see if it should be shown to the user.

Microsoft removed that in Windows 7, and didn't support it in XP.

I am trying to customize the our Open file dialog so that I can control which files are displayed to the end user. These files are marked internally with a product-code - there isn't anything in the filename itself to filter on (hence file extension filters are not useful here -= I need to actually interrogate each one to see if it is within the extra filter parameters that our users specified).

I would guess that Microsoft removed the SetFilter interface because too often it was too slow. I can imagine all sorts of similar ideas to this one which don't scale well for networks and cloud storage and what have you.

However, I need to know if there is an alternative interface that accomplishes the same goal, or if I really am restricted to only looking at the file extension for filtering purposes in my File dialogs?


Follow-up:
After looking further into CDN_INCLUDEITEM, which requires the pre-vista version of OPENFILENAME, I have found that this is the most useless API imaginable. It only filters NON-filesystem objects. In other words, you can't use it to filter files. Or folders. The very things one would filter 99.99% of the time for a file open or save dialog. Unbelievable!

There is a very old article by Paul DiLascia which offers the technique of removing each offending filename from the list view control each time the list view is updated.

However, I know from bitter experience that the list view can update over time. If you're looking at a large folder (many items) or the connection is a bit slow (heavily loaded server and/or large number of files), then the files are added to the dialog piecemeal. So one would have to filter out offending filenames repeatedly.

In fact, our current customized file open dialog uses a timer to look at the view's list of filenames periodically to see if any files of a given pattern exist, in order to enable another control. Otherwise it's possible to check for the existence of these files, find none, but a moment later the view updates to have more filenames, and no events are sent to your dialog to indicate that the view has been changed. In fact, my experience with having to write and maintain code for the common controls file dialogs over the years has been that Microsoft is not very cluefull when it comes to how to write such a thing. Events are incomplete, sent at not-useful times, repeated when not necessary, and whole classes of useful notifications don't exist.

Sadly, I think I might have to give up oh this idea. Unless someone has a thought as to how I might be able to keep up with the view spontaneously changing while the user is trying to interact with it (i.e. it would be awkward to go deleting out entries from the list view and changing the user's visual position, or highlighted files, or scroll position, etc.)

Mordachai
  • 9,412
  • 6
  • 60
  • 112
  • 1
    If you are selecting a single file, try SHBrowseForFolder with BIF_BROWSEINCLUDEFILES and pass your file filtering logic to IFolderFilterSite::SetFilter. – Sheng Jiang 蒋晟 Dec 10 '12 at 22:34
  • Interesting idea. I don't think this will work for us because we've customized the common file open and save as dialogs - so this would add a whole other set of issues (we need the file type drop-down, for instance). But cool thinking-outside the box :) – Mordachai Dec 11 '12 at 16:24
  • @Mordachai: may be you have a copy of that article by Paul DiLascia? Or at the least the date (year/month) of publication? That link you've provided no longer works and MS search is not too friendly. – Roman Mar 04 '19 at 06:45
  • @Roman I wish I did. But I have no idea what edition that was from, and it was old when I posted about it here, 6+ years ago :P – Mordachai Mar 09 '19 at 21:26

2 Answers2

2

You need to initialise the callbacks for your CFileDialog. Then you need to process CDN_INCLUDEITEM notification code to include or exclude items.

You can also check this great article. The author uses some other approaches in addition to callbacks

cha
  • 10,301
  • 1
  • 18
  • 26
  • That applies to the old-style `GetOpenFileName()` and `GetSaveFileName()` dialogs, not to the newer style `IFileDialog` dialogs. An even then, there are some files that `CDN_INCLUDEITEM` is not allowed to filter out. – Remy Lebeau Dec 11 '12 at 00:36
  • 1
    I think Mordachai did not want to use the new style, as he wanted to support XP – cha Dec 11 '12 at 01:20
  • 1
    Currently I am forced to support XP as well. The following version we may drop XP support (in a year). However, I am not certain that this will be the decision even then. So this might be a good enough solution for the next year or two. Grumble at MS's schizophrenic attitude: on the one hand they insist on only filtering by meta data external to the file. On the other, they have axed all interfaces that allowed one to extend the meta-data external to your files in the shell (and require all meta data to be stored in the file itself now). !@@#$!@#$ – Mordachai Dec 11 '12 at 15:40
  • 1
    Bottom line is CDN_INCLUDEITEM only applies to non-filesystem objects. So you cannot filter on files - which would be my objective. So this is a non-solution. :( – Mordachai Jan 14 '13 at 18:41
1

As you have already discovered, starting in Windows 7 it is no longer possible to filter out files from being displayed based on content, only file extension. You can, however, validate that the user's selected file(s) are acceptable to you before allowing the dialog to close, and if they are not then display a message box to the user and keep the dialog open. That is the best you will be able to do unless you create your own custom dialog.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Yes, this I was aware of, and unfortunately this doesn't help (we would like to filter what is shown - as that is the assistance to the end-user we are trying to achieve). :( – Mordachai Dec 11 '12 at 15:37