您的位置:首页 > 其它

获取文件的ICON

2015-04-08 10:26 127 查看
要使用系统的API,如下示例:

// CTreeListFileDlg dialog

int CSystemImageList::m_nRefCount=0;

//////////////////////////////CSystemImageList Implementation /////////////////////////////////

CSystemImageList::CSystemImageList()

{

if (m_nRefCount == 0)

{

//Attach to the system image list

SHFILEINFO sfi;

HIMAGELIST hSystemImageList = (HIMAGELIST) SHGetFileInfo(_T("\\"), 0, &sfi, sizeof(SHFILEINFO),

SHGFI_SYSICONINDEX | SHGFI_SMALLICON);

VERIFY(m_ImageList.Attach(hSystemImageList));

}

//Increment the reference count

m_nRefCount++;

}

CSystemImageList::~CSystemImageList()

{

//Decrement the reference count

m_nRefCount--;

if (m_nRefCount == 0)

{

//Detach from the image list to prevent problems on 95/98 where

//the system image list is shared across processes

m_ImageList.Detach();

}

}

CImageList& CSystemImageList::GetImageList()

{

return m_ImageList;

}

BOOL CTreeListFileDlg::OnInitDialog()

{

CDialog::OnInitDialog();

// TODO: Add extra initialization here

m_csFilter = _T("All Files (*.*)");

UpdateData(FALSE);

//设置系统图标

m_ctlFileTree.SetImageList(&m_SysImageList.GetImageList(),TVSIL_NORMAL);

OnViewRefresh();

return TRUE; // return TRUE unless you set the focus to a control

// EXCEPTION: OCX Property Pages should return FALSE

}

int CTreeListFileDlg::GetIconIndex(HTREEITEM hItem)

{

TV_ITEM tvi;

ZeroMemory(&tvi, sizeof(TV_ITEM));

tvi.mask = TVIF_IMAGE;

tvi.hItem = hItem;

if (m_ctlFileTree.GetItem(&tvi))

return tvi.iImage;

else

return -1;

}

int CTreeListFileDlg::GetIconIndex(const CString& sFilename)

{

//Retreive the icon index for a specified file/folder

SHFILEINFO sfi;

if (SHGetFileInfo(sFilename, 0, &sfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON) == 0)

return -1;

return sfi.iIcon;

}

int CTreeListFileDlg::GetSelIconIndex(const CString& sFilename)

{

//Retreive the icon index for a specified file/folder

SHFILEINFO sfi;

if (SHGetFileInfo(sFilename, 0, &sfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON) == 0)

return -1;

return sfi.iIcon;

}

int CTreeListFileDlg::GetSelIconIndex(HTREEITEM hItem)

{

TV_ITEM tvi;

ZeroMemory(&tvi, sizeof(TV_ITEM));

tvi.mask = TVIF_SELECTEDIMAGE;

tvi.hItem = hItem;

if (m_ctlFileTree.GetItem(&tvi))

return tvi.iSelectedImage;

else

return -1;

}

HTREEITEM CTreeListFileDlg::InsertFileItem(const CString &sFile, const CString &sPath, HTREEITEM hParent)

{

//Retreive the icon indexes for the specified file/folder

int nIconIndex = GetIconIndex(sPath);

int nSelIconIndex = GetSelIconIndex(sPath);

if (nIconIndex == -1 || nSelIconIndex == -1)

{

TRACE(_T("Failed in call to SHGetFileInfo for %s, GetLastError:%d\n"), sPath, ::GetLastError());

return NULL;

}

//Add the actual item

CString sTemp(sFile);

TV_INSERTSTRUCT tvis;

ZeroMemory(&tvis, sizeof(TV_INSERTSTRUCT));

tvis.hParent = hParent;

tvis.hInsertAfter = TVI_LAST;

tvis.item.mask = TVIF_CHILDREN | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;

tvis.item.pszText = sTemp.GetBuffer(sTemp.GetLength());

tvis.item.iImage = nIconIndex;

tvis.item.iSelectedImage = nSelIconIndex;

tvis.item.cChildren = HasGotSubEntries(sPath);

HTREEITEM hItem = m_ctlFileTree.InsertItem(&tvis);

sTemp.ReleaseBuffer();

return hItem;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: