//================================================================================= BOOL CPrjGetFileIconTestApp::InitInstance() { //if you don't initilize the COM ,the SHGetFileInfo function can't get some file's icon,such as htm/html/xml. //如果你不初始化COM,SHGetFileInfo 函数就不能得到一些文件到图标,如:htm/html/xml. if (!AfxOleInit()) { AfxMessageBox("AfxOleInit Failed"); return FALSE; } .... .... .... } BOOL CPrjListViewCtrlTestDlg::OnInitDialog() { .... .... .... InitData(); .... .... .... } bool CPrjListViewCtrlTestDlg::InitData() { //------------------------------------------------------------------------------------------------ m_ImgListAttachment.Create(16,16,ILC_COLOR32|ILC_MASK,0,0); //Small Icon //m_ImgListAttachment.Create(32,32,ILC_COLOR32|ILC_MASK,0,0); //Large Icon m_lvTest.SetImageList(&m_ImgListAttachment,LVSIL_SMALL); //Small Icon //m_lvTest.SetImageList(&m_ImgListAttachment,LVSIL_NORMAL); //Large Icon return true; } //================================================================================= //Get the icon's handle of file HICON CPrjGetFileIconTestDlg::GetFileIcon(CString strFileExtentName, CString strFileFullPathName) { HICON hIcon; if(0 != strFileExtentName.CompareNoCase("ico") && 0 != strFileExtentName.CompareNoCase("exe") && 0 != strFileExtentName.CompareNoCase("scr")) { //Get the icon of the common files - 普通文件图标 strFileExtentName = "." + strFileExtentName; SHFILEINFO sfi; SHGetFileInfo(strFileExtentName,0,&sfi,sizeof(sfi),SHGFI_ICON|SHGFI_LARGEICON|SHGFI_USEFILEATTRIBUTES); int i=sfi.iIcon; hIcon=sfi.hIcon; } else { //Get the icon of the Exe file Ico file or scr file - Exe,Ico,Scr 图标 hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T(strFileFullPathName),0); } return hIcon; } void CPrjListViewCtrlTestDlg::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default //------------------------------------------------------------------------------------------ CFileDialog fileDialog( TRUE,"*.*",NULL,NULL,NULL); HICON hIcon; int iItemIndex; int iRow; //------------------------------------------------------------------------------------------ if (fileDialog.DoModal() != IDOK) return; hIcon = GetFileIcon(fileDialog.GetFileExt(),fileDialog.GetFileName()); m_ImgListAttachment.Add(hIcon); iItemIndex = m_lvTest.InsertItem(LVIF_IMAGE|LVIF_TEXT, iRow, fileDialog.GetFileName(), LVIS_SELECTED, LVIS_SELECTED, m_ImgListAttachment.GetImageCount() - 1, 0); //------------------------------------------------------------------------------------------ CDialog::OnLButtonDblClk(nFlags, point); }