#pragma once
#include "ILHelper.h"
_COM_SMARTPTR_TYPEDEF(IExtractImage, __uuidof(IExtractImage));
/////////////////////////////////////////////////////////////////////////////
// CExtractImageHelper
class CExtractImageHelper
{
protected:
CExtractImageHelper(); // 実装しない
virtual ~CExtractImageHelper();
public:
// 完成
static HBITMAP GetImage(HWND hWnd, IShellFolderPtr spShellFolder, LPCITEMIDLIST pidl, const CSize& size, DWORD dwDepth = 32)
{
if (spShellFolder == NULL || pidl == NULL)
{
return NULL;
}
HRESULT hr = S_OK;
IExtractImagePtr spExtractImage;
hr = spShellFolder->GetUIObjectOf(hWnd, 1, &pidl, IID_IExtractImage, NULL, (void**)&spExtractImage);
if (FAILED(hr))
{
return NULL;
}
return GetImage(spExtractImage, size, dwDepth);
}
// 完成
// ショートカットや共有フォルダのイメージは未対応。。
static HBITMAP GetImage(IExtractImagePtr spExtractImage, const CSize& size, DWORD dwDepth = 32)
{
if (spExtractImage == NULL)
{
return NULL;
}
HRESULT hr = S_OK;
OLECHAR wszPathBuffer[MAX_PATH];
DWORD dwPriority = 0; // IEI_PRIORITY_NORMAL is defined nowhere!
DWORD dwFlags = IEIFLAG_SCREEN;
hr = spExtractImage->GetLocation(wszPathBuffer, MAX_PATH, &dwPriority, &size, dwDepth, &dwFlags);
if (FAILED(hr))
{
return NULL;
}
HBITMAP hBitmap = NULL;
hr = spExtractImage->Extract(&hBitmap);
if (FAILED(hr))
{
return NULL;
}
return hBitmap;
}
// pidlは、フルバージョンを入れる必要あり。。
static HBITMAP GetImageFromIcon(HWND hWnd, LPCITEMIDLIST pidl, const CSize& size, DWORD dwDepth = 32)
{
if (pidl == NULL)
{
return NULL;
}
SHFILEINFO info;
ZeroMemory(&info, sizeof(info));
UINT uFlags = SHGFI_PIDL | SHGFI_ICON | SHGFI_LARGEICON | SHGFI_SYSICONINDEX;
HIMAGELIST hImageList = (HIMAGELIST)::SHGetFileInfo((LPCSTR)pidl, 0, &info, sizeof(info), uFlags);
HICON hIcon = ::ImageList_GetIcon(hImageList, info.iIcon, ILD_TRANSPARENT);
HDC hDC = ::GetDC(hWnd);
HDC hMemDC = ::CreateCompatibleDC(hDC);
HBITMAP hBitmap = (HBITMAP)::CreateCompatibleBitmap(hDC, size.cx, size.cy);
HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC, (HBITMAP)hBitmap);
::FillRect(hMemDC, CRect(0, 0, size.cx, size.cy), ::GetSysColorBrush(COLOR_WINDOW));
::DrawIcon(hMemDC, (size.cx - 32) / 2, (size.cy - 32) / 2, hIcon);
// ::FrameRect(hMemDC, CRect(0, 0, size.cx, size.cy ), ::GetSysColorBrush(COLOR_BTNSHADOW));
::SelectObject(hMemDC, hOldBitmap);
::DeleteDC(hMemDC);
::DeleteDC(hDC);
return hBitmap;
}
public:
/*
// 完成
static HBITMAP ExtractThumb(LPSHELLFOLDER psfFolder, LPCITEMIDLIST localPidl, const SIZE* prgSize, DWORD dwRecClrDepth)
{
LPEXTRACTIMAGE pIExtract = NULL;
HRESULT hr;
hr = psfFolder->GetUIObjectOf(NULL, 1, &localPidl, IID_IExtractImage,
NULL, (void**)&pIExtract);
if(NULL == pIExtract) // early shell version, thumbs not supported
return NULL;
OLECHAR wszPathBuffer[MAX_PATH];
DWORD dwPriority = 0; // IEI_PRIORITY_NORMAL is defined nowhere!
DWORD dwFlags = IEIFLAG_SCREEN;
HBITMAP hBmpImage = NULL;
hr = pIExtract->GetLocation(wszPathBuffer, MAX_PATH, &dwPriority,
prgSize, dwRecClrDepth, &dwFlags);
// even if we've got shell v4.70+, not all files support thumbnails
if(NOERROR == hr)
hr = pIExtract->Extract(&hBmpImage);
pIExtract->Release();
return hBmpImage; // callers should DeleteObject this handle after use
}
*/
};