HRESULT PreviewGenerator::ShowPreviewWithShellItemImageFactory(CString filePath) { HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); IShellItemImageFactory *imageFactory; hr = SHCreateItemFromParsingName(filePath, NULL, IID_PPV_ARGS(&imageFactory)); if (hr == S_OK) { CRect pRect; previewControl->GetWindowRect(pRect); int s = pRect.Height(); if (s>pRect.Width()) s = pRect.Width(); //s = 256; _SIIGBF flag = SIIGBF_RESIZETOFIT; if (fileExt != L"pdf" && fileExt != L"PDF") { s = 256; flag = SIIGBF_BIGGERSIZEOK; } SIZE size = { s, s }; HBITMAP btmap; hr = imageFactory->GetImage(size, flag, &btmap); if (hr == S_OK) DrawBitMap(btmap); } CoUninitialize(); imageFactory->Release(); return hr; }
// On Vista or 7 we could use SHIL_JUMBO to get a 256x256 icon, // but we'll use SHCreateItemFromParsingName as it'll give an identical // icon to the one shown in explorer and it scales automatically. bool WinIconProvider::addIconFromShellFactory(QString filePath, QIcon& icon) const { HRESULT hr = S_FALSE; if (fnSHCreateItemFromParsingName) { IShellItem* psi = NULL; hr = fnSHCreateItemFromParsingName((PCWSTR)filePath.utf16(), 0, IID_IShellItem, (void**)&psi); if (hr == S_OK) { IShellItemImageFactory* psiif = NULL; hr = psi->QueryInterface(IID_IShellItemImageFactory, (void**)&psiif); if (hr == S_OK) { HBITMAP iconBitmap = NULL; SIZE iconSize = {preferredSize, preferredSize}; hr = psiif->GetImage(iconSize, SIIGBF_RESIZETOFIT | SIIGBF_ICONONLY, &iconBitmap); if (hr == S_OK) { QPixmap iconPixmap = QtWin::fromHBITMAP(iconBitmap); icon.addPixmap(iconPixmap); DeleteObject(iconBitmap); } psiif->Release(); } psi->Release(); } } return hr == S_OK; }