HRESULT CreateThumbnail(DWORD dwWidth, DWORD dwHeight, HBITMAP* pThumbnail)
{
LPITEMIDLIST pidlItems = NULL, pidlURL = NULL, pidlWorkDir = NULL;
ULONG ulParseLen = 0;
HRESULT hr;
WCHAR pszPath[MAX_PATH];
DWORD dwPriority = 0, dwFlags = IEIFLAG_ASPECT;
SIZE size = { dwWidth, dwHeight };
IExtractImage* peiURL = NULL; // nterface is used to request a thumbnail image from a Shell folder
IShellFolder* psfDesktop = NULL;
IShellFolder* psfWorkDir = NULL;
IMalloc* pMalloc = NULL;
wstring wsDir,wsFile,wsTempFile;
// 初始化Com库
if ( CoInitialize( NULL ) != 0 )
{
AfxMessageBox( _T( "初始化Com库失败!" ) );
goto OnExit;
}
hr = SHGetMalloc( &pMalloc );
if ( FAILED( hr ) )
{
goto OnExit;
}
hr = SHGetDesktopFolder(&psfDesktop);
if(FAILED(hr)) goto OnExit;
wsDir = m_wsDir;
wsFile = m_wsFile;
wcscpy(m_wsBuffer,wsDir.c_str());
hr = psfDesktop->ParseDisplayName(NULL, NULL, m_wsBuffer, &ulParseLen, &pidlWorkDir, NULL);
if(FAILED(hr)) goto OnExit;
hr = psfDesktop->BindToObject(pidlWorkDir, NULL, IID_IShellFolder, (LPVOID*)&psfWorkDir);
if(FAILED(hr)) goto OnExit;
psfDesktop->Release();
psfDesktop = NULL;
pMalloc->Free(pidlWorkDir);
pidlWorkDir = NULL;
wcscpy(m_wsBuffer,wsFile.c_str());
hr = psfWorkDir->ParseDisplayName(NULL, NULL, m_wsBuffer, &ulParseLen, &pidlURL, NULL);
if(FAILED(hr)) goto OnExit;
hr = psfWorkDir->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST*)&pidlURL, IID_IExtractImage, NULL, (LPVOID*)&peiURL);
if(FAILED(hr)) goto OnExit;
hr = peiURL->GetLocation(pszPath, MAX_PATH, &dwPriority, &size, 16, &dwFlags);
if(FAILED(hr)) goto OnExit;
hr = peiURL->Extract(pThumbnail);
if(FAILED(hr)) goto OnExit;
peiURL->Release();
peiURL = NULL;
OnExit:
if(peiURL != NULL) peiURL->Release();
if(pidlURL != NULL) pMalloc->Free(pidlURL);
if(pidlWorkDir != NULL) pMalloc->Free(pidlWorkDir);
if(psfDesktop != NULL) psfDesktop->Release();
if(psfWorkDir != NULL) psfWorkDir->Release();
CoUninitialize();
return hr;
} |