Berry's blog with C++ & .Net

Windows & VxWorks development and learning notes.

导航

使用VC取得文件关联图标的方法

Posted on 2009-10-10 22:39  Berry029  阅读(1424)  评论(0编辑  收藏  举报

一个简单的学习加强实例,用VC取得文件的关联图标,看下面的代码一起来学习下!

CFileDialog fileDialog( TRUE,\"*.*\",NULL,NULL,NULL);

if (fileDialog.DoModal() != IDOK)  
   return;

CString str= fileDialog.GetFileExt();
str=\".\"+str;

SHFILEINFO   sfi;  
SHGetFileInfo(str,0,&sfi,sizeof(sfi),SHGFI_ICON|SHGFI_LARGEICON|SHGFI_USEFILEATTRIBUTES);

int i=sfi.iIcon;
CStatic* pStatic = (CStatic*) GetDlgItem(IDC_STATIC);
HICON hIcon=sfi.hIcon;
pStatic->SetIcon (hIcon);

上面的IDC_STATIC是一个静态控件。 
记得用下面的函数释放用SHGetFileInfo获取的icon句柄


If SHGetFileInfo returns an icon handle in the hIcon member of the SHFILEINFO structure pointed to by psfi, you are responsible for freeing it with DestroyIcon when you no longer need it.
BOOL DestroyIcon(HICON hIcon);