1 Attachment(s)
CFileDialog: Getting true selected files.
Hello all -
I have a class derived from CFileDialog, and I want to do some processing of user-selected files as the selections change. In my class, I override the OnFileNameOK, OnFileNameChange, and OnFolderChange methods to get the new list of selected files (full paths). I can't rely on CFileDialog's GetFileName or GetPathName/GetNextPathName to get this list of files, because 1) even when the user clears the selection, these still return the previously-selected values, and 2) I need to be able to tell the difference between folders and files.
So I'm trying to go the COM route. I have a small helper class that tries to do the dirty work for me. In the overrides of the methods above, I call GetSelectedFiles on the member instance of this helper I create in my dialog's constructor. This works great, however I've found that if the user clicks the Libraries shortcut on the left, then goes into Documents, it doesn't seem to work. The GetFolderPath called from the helper returns an empty string (when I'd expect it to return some symbolic string at least that other COM methods would know how to interpret). Interestingly enough, if I create a subfolder and navigate into it, I can get the selected files.
I'd consider myself less than a novice at COM, so any help would be appreciated. Please see my attached helper.
Re: CFileDialog: Getting true selected files.
Quote:
Originally Posted by
sjc
I have a class derived from CFileDialog, and I want to do some processing of user-selected files as the selections change. In my class, I override the OnFileNameOK, OnFileNameChange, and OnFolderChange methods to get the new list of selected files (full paths). I can't rely on CFileDialog's GetFileName or GetPathName/GetNextPathName to get this list of files, because 1) even when the user clears the selection, these still return the previously-selected values, and 2) I need to be able to tell the difference between folders and files.
This doesn't sound correct. First of all, CFileDialog doesn't allow you to select folders - only files so 2) doesn't make sense. 1) shouldn't happen if you are coding it properly.
The following should return the list of selected files (be sure to set the OFN_ALLOWMULTISELECT style).
Code:
if (dlg.DoModal() == IDOK)
{
POSITION pos = dlgFile.GetStartPosition();
if( NULL != pos )
{
CString strFile = dlgFile.GetNextPathName(pos);
}
}
Re: CFileDialog: Getting true selected files.
Thanks for the response Arjay, but you're not understanding my issue. I'm trying to do a little processing of the items the user selects WHILE the dialog is still up. I have an advanced need to do it this way versus letting the dialog exit. And because of this the user could do things like multi-select folder and files, or they could be in a directory that doesn't even have files or folder (like My Computer). Every time the selection or folder changes, I need to gather up a list of all the selected files that match a certain criteria - all while the dialog is still up. Because of this, the existing CFileDialog methods are unreliable.
Re: CFileDialog: Getting true selected files.
Ah, I see. The folders you are having trouble with, do they happen to be virtual folders?
Re: CFileDialog: Getting true selected files.
Yes in fact. I extend the CFileDialog with a custom resource where I put a list box on the right side with some other configuration controls. I have a button that should enable when the user selects valid files, and when clicked will shoot those files over to a list on the right so the user can gather a bunch of files at once to perform some action. However when they navigate to the Libraries\Documents folder, I can't seem to get the correct file data. Interestingly if I create a subfolder in this directory and go into that, it does work. It seems to not work when it's directly in one of these special directories.
The 'pIDList' in my attached document does seem to find a selected file, but when I enter my 'GetDisplayNameOf' method, the call to 'GetDisplayNameOf' on the IShellFolder returns an E_INVALIDARG error. I have a feeling it's because the 'IShellFolder' isn't correct, but I don't know why.
I'm aware of the SHGetSpecialFolderLocation method, but I'm not sure if there's a way I can incorporate it since I can't seem to get the current path when I'm in one of these special directories.
Re: CFileDialog: Getting true selected files.
Read up on the SH docs when dealing with virtual directories. There are some limitations, but I don't remember what they are off the top of my head.