File Open Dialog with Multiple Selection – Part 2: Vista Style

As told in the previous article, if the Open File dialog has Vista style, it’s not possible to use common dialog box messages like CDM_GETFOLDERPATH and CDM_GETSPEC in order to calculate the necessary buffer size, for multiple selection.
Instead, we can use Common Item Dialog API, in our case IFileOpenDialog interface.

Using IFileOpenDialog interface

Here are the main steps:

  1. call CFileDialog::GetIFileOpenDialog to get ponter to IFileOpenDialog interface;
  2. use IFileOpenDialog::GetSelectedItems; it gets a IShellItemArray* containing currently selected items (an array of IShellItem* elements);
  3. for each item call IShellItem::GetDisplayName; use the name length in order to calculate the necessary buffer size.

Example 4

Further, we can use the returned value to set the OPENFILENAME buffer, if necessary, in CFileDialog::OnFileNameChange overridden function (see the previous article).
Anyway, more implementation details can be found in the attached demo application.

Demo application

File Open Dialog with Multiple Selection - Demo Application

File Open Dialog with Multiple Selection – Demo Application

The demo application contains the complete implementation of a CFileDialog-derived class, that fixes the multiple selection issues described in this article. It can be used either for Vista or old style file dialogs. Also, to see what happens if use the default buffer, just uncheck “Use ext. buffer” then push “Open…” and finally, select a large number of items from the open file dialog list.

Demo project download: File Open Dialog with Multiple Selection (1083)

Notes

  • Common Item Dialog API is supported on Windows Vista and newer.
  • CFileDialog with Vista style is implemented in Visual Studio 2008 and newer.

Resources

See also

Leave a Reply