Solved

Filter file types in CMFCShellListCtrl

Posted on 2011-02-19
11
1,810 Views
Last Modified: 2013-11-20

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
Comment
Question by:apostrophe27
  • 5
  • 5
11 Comments
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 200 total points
ID: 34952815
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
 

Author Comment

by:apostrophe27
ID: 34963254

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
 
LVL 19

Expert Comment

by:mrwad99
ID: 35035287
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 19

Expert Comment

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

Author Comment

by:apostrophe27
ID: 35098234

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
 
LVL 19

Expert Comment

by:mrwad99
ID: 35107214
Yeah, EnumObjects() was mentioned in the first post by DeepuAbrahamK: did you not try his code??
0
 

Author Comment

by:apostrophe27
ID: 35108416
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
 
LVL 19

Assisted Solution

by:mrwad99
mrwad99 earned 300 total points
ID: 35108595
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
 

Accepted Solution

by:
apostrophe27 earned 0 total points
ID: 35319048
I haven't gotten this to work, and I've moved on to other things now.

Thanks for the help.

0
 
LVL 19

Expert Comment

by:mrwad99
ID: 35322179
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
 

Author Closing Comment

by:apostrophe27
ID: 35357026
I haven't gotten this to work, and I've moved on to other things.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question