IExtractImage
void* CShellListCtrl::GetThumbnailImage(CString &fileName, int longestEdge, int colorDepth)
{
// divide the file name into a folder path and file name.
WCHAR wDirName[MAX_PATH];
WCHAR wFileName[MAX_PATH];
CString dir = fileName.Left(fileName.ReverseFind('\\'));
CString file = fileName.Right(fileName.GetLength() - dir.GetLength() - 1);
TCHAR tName[MAX_PATH];
strcpy(tName, dir.GetBuffer());
wcscpy(wDirName, CT2W(tName));
strcpy(tName, file.GetBuffer());
wcscpy(wFileName, CT2W(tName));
IShellFolder* pDesktop = NULL;
IShellFolder* pSub = NULL;
IExtractImage* pIExtract = NULL;
LPITEMIDLIST pList = NULL;
HBITMAP hBmp = NULL;
// get the desktop directory
if (SUCCEEDED(SHGetDesktopFolder(&pDesktop)))
{
// get the pidl for the directory
HRESULT hr = pDesktop->ParseDisplayName(NULL, NULL, wDirName, NULL, &pList, NULL);
if (FAILED(hr))
{
throw new CUserInterfaceException("Failed to parse the directory name");
}
// get the directory IShellFolder interface
hr = pDesktop->BindToObject(pList, NULL, IID_IShellFolder, (void**)&pSub);
if (FAILED(hr))
{
throw new CUserInterfaceException("Failed to bind to the directory");
}
// get the file's pidl
hr = pSub->ParseDisplayName(NULL, NULL, wFileName, NULL, &pList, NULL);
if (FAILED(hr))
{
throw new CUserInterfaceException("Failed to parse the file name");
}
// get the IExtractImage interface
LPCITEMIDLIST pidl = pList;
hr = pSub->GetUIObjectOf(NULL, 1, &pidl, IID_IExtractImage, NULL, (void**)&pIExtract);
// set our desired image size
SIZE size;
size.cx = longestEdge;
size.cy = longestEdge;
if(pIExtract == NULL)
{
return NULL;
}
// The IEIFLAG_ORIGSIZE flag tells it to use the original aspect
// ratio for the image size. The IEIFLAG_QUALITY flag tells the
// interface we want the image to be the best possible quality.
DWORD dwFlags = IEIFLAG_ORIGSIZE | IEIFLAG_QUALITY;
OLECHAR pathBuffer[MAX_PATH];
hr = pIExtract->GetLocation(pathBuffer, MAX_PATH, NULL, &size, colorDepth, &dwFlags);
if (FAILED(hr))
{
throw new CUserInterfaceException("The call to GetLocation failed");
}
hr = pIExtract->Extract(&hBmp); //THIS IS WHERE I GET THE ERROR MESSAGE
pIExtract->Release();
} // if
// Release the COM objects we have a reference to.
pDesktop->Release();
pSub->Release();
return hBmp;
}
十一月 2nd, 2007 in
代码片断