• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2036
  • Last Modified:

Filter file types in CMFCShellListCtrl


I'm working with the new MFC control CMFCShellListCtrl and I'm trying to add filters to the view. I want to filter by file type or string. Since it's new, the documentation and examples are limited.

So how to I set up filters?

Thanks

0
apostrophe27
Asked:
apostrophe27
  • 5
  • 5
3 Solutions
 
Deepu AbrahamR & D Engineering ManagerCommented:
I haven't tried this myself but this should work.

virtual HRESULT CMFCShellListCtrl::EnumObjects(LPSHELLFOLDER pParentFolder, LPITEMIDLIST pidlParent)

Sample code to get the pidl.

LPITEMIDLIST pidl = NULL;
hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidl);

if (SUCCEEDED(hr))                    
{
    SHFILEINFOW sfi = {0};
    hr = SHGetFileInfo((LPCTSTR)pidl,
                        -1,
                        &sfi,
                        sizeof(sfi),
                        SHGFI_PIDL | SHGFI_DISPLAYNAME)
            
    if (SUCCEEDED(hr))
    {
        // The display name is now held in sfi.szDisplayName.
    }
}

ILFree(pidl);

Open in new window

0
 
apostrophe27Author Commented:

I don't fully understand your code, but it looks like it retrieves the information on a file. Is that correct?

What I want to do is have the explorer window in the list control display only a certain type of file, .txt for example, or a string.  How do I use your example to do that?

Thanks.
0
 
mrwad99Commented:
This question really is hanging about isn't it!  

I have had a good look around and come up with next to nothing for this one...the only thing I can suggest is taking a look at

http://www.codeproject.com/Messages/3206089/How-to-use-the-CMFCShellListCtrl-Class.aspx

Here, the author here claims to have found a "callback" function that will be called every time an item is added: if this is the case, you need to override that to filter out the file types you don't want to show.  However, he claims it does not work, but he might be doing something slightly wrong; maybe your eyes can see what is incorrect with it.  I don't have VS 2010 installed so I cannot try this.

Sorry if this is of no use: if you still can find no answers I might have to install VS 2010 and nail this myself :)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
mrwad99Commented:
PS, I take it you have looked at the MFC 10 "Explorer" sample: http://msdn.microsoft.com/en-us/library/bb982182.aspx ?
0
 
apostrophe27Author Commented:

Sorry it's been awhile. I got stuck working on other things.

I saw the code project post, and I didn't understand what he was doing. Since it didn't work for him I didn't pursue it.

I saw the Explorer sample too, but it doesn't show any filtering.

I just saw a post at:
http://social.msdn.microsoft.com/Forums/en-GB/windowssdk/thread/794521ed-66e2-494f-ab28-75100fdf58d1 

They're talking about creating and open file dialog box

" You can create a dialog and add a CMFCShellListCtrl-derived control to it. There is an undocumented EnumObjects function you can override to filter the results. "

I'm not sure if that's useful or not

0
 
mrwad99Commented:
Yeah, EnumObjects() was mentioned in the first post by DeepuAbrahamK: did you not try his code??
0
 
apostrophe27Author Commented:
Right, he did mention EnumObjects(). Sorry, I didn't catch that when I reread the thread.

I didn't try his code because I didn't understand how to use it. I did him how to use it, but I got no response.

0
 
mrwad99Commented:
OK.

1).  You need a class derived from CMFCShellListCtrl in your application.  In this class's header file, add

virtual HRESULT EnumObjects(LPSHELLFOLDER pParentFolder, LPITEMIDLIST pidlParent);

Open in new window


2) In your class's .cpp file, add

<CREDIT FOR CODE GOES TO DeepuAbrahamK>
virtual HRESULT <YOUR DERIVED CLASS NAME>::EnumObjects(LPSHELLFOLDER pParentFolder, LPITEMIDLIST pidlParent)
{
LPITEMIDLIST pidl = NULL;
hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidl);

if (SUCCEEDED(hr))                    
{
    SHFILEINFOW sfi = {0};
    hr = SHGetFileInfo((LPCTSTR)pidl,
                        -1,
                        &sfi,
                        sizeof(sfi),
                        SHGFI_PIDL | SHGFI_DISPLAYNAME)
            
    if (SUCCEEDED(hr))
    {
        // The display name is now held in sfi.szDisplayName.
        TRACE ( _T("Got file %s\n"),  sfi.szDisplayName);
    }
}

ILFree(pidl);
return S_OK;
}

Open in new window


3) Now.  I am not sure what needs to be returned from the function in order to prevent the add of an item.  Add the code above, add see what output you get in the output window when debugging.  If you do get a list of all the files in the current folder, it means that all we need to do is figure out the required return value to filter out file types we don't want to display.  At a guess, we could return S_FAIL to *not* add the file, although I don't honestly know.

I don't have MFC 10 on my machine, but one of the first things I would do is check out the MFC source code for CMFCShellListCtrl::EnumObjects().  You can do this easily by calling it from your overridden version.  See if the above code works ok for now, if it does not, replace all of it with a call to the base class version, i.e.

virtual HRESULT <YOUR DERIVED CLASS NAME>::EnumObjects(LPSHELLFOLDER pParentFolder, LPITEMIDLIST pidlParent)
{
return CMFCShellListCtrl::EnumObjects(pParentFolder,pidlParent);
}

Open in new window


and see what the base class version does, posting the code here please.
0
 
apostrophe27Author Commented:
I haven't gotten this to work, and I've moved on to other things now.

Thanks for the help.

0
 
mrwad99Commented:
No problem.  Thanks for splitting the points.

At some point I will install VS 2010 and see if I can figure this out; when I do (note the confidence - *when*, not *if* :) ) I will let you know.
0
 
apostrophe27Author Commented:
I haven't gotten this to work, and I've moved on to other things.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now