void CtestThubnailView::ShowThubnail( CDC * pDC, CString fileName )
{
LPSHELLFOLDER pIShellFolder = NULL;
CString sParentFolder = GetPath( fileName );
if (sParentFolder.IsEmpty())
{
return ;
}
LPSHELLFOLDER pIDesktopFolder = NULL;
HRESULT hRes = SHGetDesktopFolder(&pIDesktopFolder);
if (FAILED(hRes))
{
return ;
}
LPITEMIDLIST pidl = GetPIDL(sParentFolder, pIDesktopFolder);
if (NULL == pidl)
{
return ;
}
hRes = pIDesktopFolder->BindToObject(pidl, NULL, IID_IShellFolder, (void**)&pIShellFolder);
if (FAILED(hRes))
{
return ;
}
LPMALLOC pMalloc;
if (SHGetMalloc (&pMalloc)== NOERROR)
{
pMalloc->Free(pidl);
pMalloc->Release();
} else
{
return ;
}
CString sRelFileName = GetName( fileName );
LPCITEMIDLIST relativePidl = GetPIDL(sRelFileName, pIShellFolder);
if (NULL == relativePidl)
{
pIShellFolder->Release();
return ;
}
SIZE size;
size.cx = GetSystemMetrics(SM_CXSCREEN) ;
size.cy = GetSystemMetrics(SM_CYSCREEN) ;
LPEXTRACTIMAGE pIExtract = NULL;
HRESULT hr;
hr = pIShellFolder->GetUIObjectOf(NULL, 1, &relativePidl, IID_IExtractImage,
NULL, (void**)&pIExtract);
if(NULL == pIExtract) // early shell version, thumbs not supported
{
return ;
}
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,
&size, 32, &dwFlags);
// even if we've got shell v4.70+, not all files support thumbnails
if(NOERROR == hr)
{
try{
hr = pIExtract->Extract(&hBmpImage);
CString strTmp;
strTmp.Format( "Extract file %S ok! \n" , wszPathBuffer );
TRACE( strTmp );
}
catch(...)
{
CString strTmp;
strTmp.Format( "Extract file %S error! \n" , wszPathBuffer );
TRACE( strTmp );
}
}
pIExtract->Release();
if (NULL == hBmpImage )
{
pIShellFolder->Release();
LPMALLOC pMalloc;
if (SHGetMalloc (&pMalloc)== NOERROR)
{
pMalloc->Free((void*)relativePidl);
pMalloc->Release();
}
return ;
}
pIShellFolder->Release();
if (SHGetMalloc (&pMalloc)== NOERROR)
{
pMalloc->Free((void*)relativePidl);
pMalloc->Release();
}
CBitmap bitmap;
bitmap.Attach( hBmpImage );
CDC dcComp;
dcComp.CreateCompatibleDC(pDC);
dcComp.SelectObject(&bitmap);
// get size of bitmap for BitBlt()
BITMAP bmInfo;
bitmap.GetObject(sizeof(bmInfo),&bmInfo);
// use BitBlt() to draw bitmap
pDC->BitBlt(0,0,bmInfo.bmWidth,bmInfo.bmHeight,
&dcComp, 0,0,SRCCOPY);
DeleteObject( hBmpImage );
}