获取文件的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;
}
// 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;
}
相关文章推荐
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- 使用 IconHandler 2.0 获取 文件图标
- 一个获取文件的ICON类
- C# 获取系统Icon、获取文件相关的Icon
- java从apk文件获取包名、版本号、icon
- Windows CE如何根据文件名获取其对应文件图标icon
- 使用C#的ICON类轻松获取文件图标
- sharepoint 2010 获取文件小图标地址get document icon url
- 获取.exe/.dll/.icon文件的Icon图标资源
- 获取指定文件的图标,返回Icon
- sharepoint获取文件的ICON
- 转载:使用 IconHandler 2.0 获取 文件图标
- 纯java从apk文件里获取包名、版本号、icon
- 获取文件图标,返回Icon
- 纯java从apk文件里获取包名、版本号、icon
- Windows CE如何根据文件名获取其对应文件图标icon
- 用 Icon.ExtractAssociatedIcon 函数获取文件图标
- WPF 获取指定文件的Icon
- 转载:使用 IconHandler 2.0 获取 文件图标
- 如何获取当前程序文件的路径