用CMfcShellTree和CMFCShellListCtrl实现资源管理器并过滤扩展名

资源管理器

CMfcShellTree和CMFCShellListCtrl是VS2008 SP1和VS2010内自带的控件,用这两个控件实现资源管理器只需几行代码。  

 
   
CMFCShellTreeCtrl m_tree;
CMyShellListCtrl m_list;
m_tree.Create(WS_CHILD
| WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_TABSTOP | 0x4423 , CRect( 0 , 0 , 300 , 400 ), this , 1 );
m_list.Create(WS_CHILD
| WS_VISIBLE | LVS_REPORT | WS_BORDER | WS_HSCROLL | WS_TABSTOP, CRect( 300 , 0 , 700 , 400 ), this , 2 );

 

用CMfcShellTree和CMFCShellListCtrl实现资源管理器并过滤扩展名_第1张图片

 

VS2008的话,需要加入如下初始化代码 

 
   
BOOL CXXXApp::InitInstance()
{
……
InitShellManager();
……
}

  

过滤扩展名

两个控件过滤扩展名的方法类似,都是重载EnumObjects。以列表控件为例,CMyShellListCtrl派生自CMFCShellListCtrl,重载EnumObjects中的代码从基类中拷贝而来,修改2个地方。 

 
   
HRESULT CMyShellListCtrl::EnumObjects(LPSHELLFOLDER pParentFolder, LPITEMIDLIST pidlParent)
{
    ASSERT_VALID(
this );
    ASSERT_VALID(afxShellManager);

    LPENUMIDLIST pEnum;
    HRESULT hRes
= pParentFolder -> EnumObjects(NULL, m_nTypes, & pEnum);

   
if (SUCCEEDED(hRes))
    {
        LPITEMIDLIST pidlTemp;
        DWORD dwFetched
= 1 ;
        LPAFX_SHELLITEMINFO pItem;

       
// enumerate the item's PIDLs
        while (pEnum -> Next( 1 , & pidlTemp, & dwFetched) == S_OK && dwFetched)
        {
            LVITEM lvItem;
            ZeroMemory(
& lvItem, sizeof (lvItem));

           
// fill in the TV_ITEM structure for this item
            lvItem.mask = LVIF_PARAM | LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;

           
// AddRef the parent folder so it's pointer stays valid
            pParentFolder -> AddRef();

           
// put the private information in the lParam
            pItem = (LPAFX_SHELLITEMINFO)GlobalAlloc(GPTR, sizeof (AFX_SHELLITEMINFO));

            pItem
-> pidlRel = pidlTemp;
            pItem
-> pidlFQ = afxShellManager -> ConcatenateItem(pidlParent, pidlTemp);

            pItem
-> pParentFolder = pParentFolder;
            lvItem.lParam
= (LPARAM)pItem;

            lvItem.pszText
= _T( "" );
           
int n = GetItemCount();
            lvItem.iImage
= OnGetItemIcon(n, pItem);

           
// determine if the item is shared
            DWORD dwAttr = SFGAO_FOLDER | SFGAO_DISPLAYATTRMASK; // 在这里加入SFGAO_FOLDER

            pParentFolder
-> GetAttributesOf( 1 , (LPCITEMIDLIST * ) & pidlTemp, & dwAttr);

           
if (dwAttr & SFGAO_SHARE)
            {
                lvItem.mask
|= LVIF_STATE;
                lvItem.stateMask
|= LVIS_OVERLAYMASK;
                lvItem.state
|= INDEXTOOVERLAYMASK( 1 ); // 1 is the index for the shared overlay image
            }

           
if (dwAttr & SFGAO_GHOSTED)
            {
                lvItem.mask
|= LVIF_STATE;
                lvItem.stateMask
|= LVIS_CUT;
                lvItem.state
|= LVIS_CUT;
            }

             
// 这里加入代码

            
if (dwAttr & SFGAO_FOLDER)
            {
            }
           
else

            {


                SHFILEINFO sfi;
                TCHAR szPath [MAX_PATH];

               
if (SHGetPathFromIDList(pItem-> pidlFQ, szPath))
                {
                    CFileStatus fs;
                   
if
(CFile::GetStatus(szPath, fs))
                    {
                        CString s
=
szPath;
                       
int n = s.ReverseFind('.'
);
                       
if(n!=-1
)
                        {
                            s
= s.Mid(n+1
);
                            s.MakeLower();
                           
if(s == _T("jpg"
))
                               
continue
;   }
                    }
                }


            }

           
int iItem = InsertItem( & lvItem);

           
if (iItem >= 0 )
            {
               
// Set columns:
                const int nColumns = m_wndHeader.GetItemCount();
               
for ( int iColumn = 0 ; iColumn < nColumns; iColumn ++ )
                {
                    CString s
= OnGetItemText(iItem, iColumn, pItem);
                    SetItemText(iItem, iColumn,s );
                }
            }

            dwFetched
= 0 ;
        }

        pEnum
-> Release();
    }

   
return hRes;
}

你可能感兴趣的:(用CMfcShellTree和CMFCShellListCtrl实现资源管理器并过滤扩展名)