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 );