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

一个简单的学习加强实例,用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);


你可能感兴趣的:(文件)