HICON FileManager::GetFileIcon(LPCTSTR lpszFile) { if (INVALID_FILE_ATTRIBUTES == ::GetFileAttributes(lpszFile)) return false; HICON hIcon = NULL; SHFILEINFO sfi = { 0 }; //HIMAGELIST hil; PIDLIST_ABSOLUTE pidl = ::ILCreateFromPath(lpszFile); if (pidl) { ::SHGetFileInfo((TCHAR*)pidl /*t_wszActionPath*/, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_LARGEICON | SHGFI_PIDL); ILFree(pidl); IImageList *lpImgList = NULL; ::SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void**)&lpImgList); if (lpImgList) { lpImgList->GetIcon(sfi.iIcon, ILD_NORMAL, &hIcon);//ILD_TRANSPARENT | ILD_BLEND50 | ILD_BLEND25 lpImgList->Release(); } ::DestroyIcon(sfi.hIcon); } return hIcon; }
HRESULT PreviewGenerator::GetHighResolutionIcon(CString filePath) { // Get the image list index of the icon SHFILEINFO sfi; if (!SHGetFileInfo(filePath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL; // Get the jumbo image list IImageList *piml; HRESULT hr = SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml)); if (hr != S_OK) return hr; // Extract an icon HICON hico; piml->GetIcon(sfi.iIcon, ILD_IMAGE, &hico); HDC hdc = previewControl->GetDC()->GetSafeHdc(); DrawIconEx(hdc, 0, 0, hico, 256, 256, 0, NULL, DI_NORMAL); //ICONINFO iconinfo; //GetIconInfo(hico, &iconinfo); //HBITMAP hBitmap = iconinfo.hbmColor; //BITMAP bmpInfo; //GetObject(hBitmap, sizeof(bmpInfo), &bmpInfo); //SIZE bitmapSize; //bitmapSize.cx = bmpInfo.bmWidth; //bitmapSize.cy = bmpInfo.bmHeight; piml->Release(); return hr; }
//------------------------------------------------------------------------------ // CDevicePropertyPage::GetIconFromItem [STATIC FUNC] // // Gets a handle to the icon of the shell item. phIcon needs to be cleaned // up with DestroyIcon() when done. //------------------------------------------------------------------------------ HRESULT CDevicePropertyPage::GetIconFromItem( __in IShellItem* pShellItem, __in int iImageList, __out HICON* phIcon ) { HRESULT hr = S_OK; int iIcon = 0; PITEMID_CHILD pidl = NULL; IImageList* pImageList = NULL; IParentAndItem* pParentAndItem = NULL; IShellFolder* pShellFolder = NULL; *phIcon = NULL; hr = pShellItem->QueryInterface( &pParentAndItem ); if( S_OK == hr ) { hr = pParentAndItem->GetParentAndItem( NULL, &pShellFolder, &pidl ); } if( S_OK == hr ) { hr = SHGetImageList( iImageList, __uuidof(IImageList), reinterpret_cast<void**>(&pImageList) ); } if( S_OK == hr ) { iIcon = SHMapPIDLToSystemImageListIndex( pShellFolder, pidl, NULL ); hr = pImageList->GetIcon( iIcon, 0, phIcon ); } // // Cleanup // if( NULL != pImageList ) { pImageList->Release(); } if( NULL != pidl ) { ILFree( pidl ); } if( NULL != pShellFolder ) { pShellFolder->Release(); } if( NULL != pParentAndItem ) { pParentAndItem->Release(); } return hr; }// CDevicePropertyPage::GetIconFromItem
bool WinIconProvider::addIconFromImageList(int imageListIndex, int iconIndex, QIcon& icon) const { HICON hIcon = 0; IImageList* imageList; HRESULT hResult = SHGetImageList(imageListIndex, IID_IImageList, (void**)&imageList); if (hResult == S_OK) { hResult = ((IImageList*)imageList)->GetIcon(iconIndex, ILD_TRANSPARENT, &hIcon); imageList->Release(); } if (hResult == S_OK && hIcon) { icon.addPixmap(QtWin::fromHICON(hIcon)); DestroyIcon(hIcon); } return SUCCEEDED(hResult); }
HICON GetHighResolutionIcon(LPTSTR pszPath) { // Get the image list index of the icon SHFILEINFO sfi; if (!SHGetFileInfo(pszPath, -1, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_LARGEICON)) { return NULL; } // Get the jumbo image list IImageList *piml; if (DEFAULT_ICOH_SIZE_WIDHT == 32) { if (FAILED(SHGetImageList(SHIL_LARGE, IID_IImageList, (void **)(&piml)))) { return NULL; } } else // 48 { if (FAILED(SHGetImageList(SHIL_EXTRALARGE, IID_IImageList, (void **)(&piml)))) { return NULL; } } // Extract an icon HICON hico; piml->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hico); int w, h; piml->GetIconSize(&w, &h); // Clean up piml->Release(); // Return the icon return hico; }