這個問題關係到 shell介面編程部分,一個比較令人糾結的問題時,不管操作那個部分的檔案夾,都要從shell檔案系統的root節點(Desktop)開始操作,直到找到你要的節點位置。概因樹形結構表達有關。不說了,直接上代碼吧:
LPITEMIDLIST pidlItems = NULL, pidlURL = NULL,
pidlWorkDir = NULL; ULONG ulParseLen = 0; HRESULT hr; TCHAR pszPath[MAX_PATH*2]; DWORD dwPriority = 0, dwFlags = IEIFLAG_SCREEN; SIZE size = { dwWidth, dwHeight }; IExtractImage* peiURL = NULL; //
interface is used to request a thumbnail image from a Shell folder IShellFolder* psfDesktop = NULL; IShellFolder* psfWorkDir = NULL; IMalloc* pMalloc = NULL;
TString TempTumbnail = GetMediaLibraryDefaultDir() + _T( "MLThumbnail.jpg" );
CImage myImage;
TString wsDir,wsFile,wsTempFile;
BOOL bComInit = TRUE; hr = CoInitialize( NULL );
hr = SHGetMalloc( &pMalloc ); hr = SHGetDesktopFolder(&psfDesktop);
wsDir = LocalFile; wsDir.erase(wsDir.find_last_of(_T( '\\' ))
+ 1, wsDir.length()); wsFile = LocalFile; wsFile.erase(0, wsFile.find_last_of(_T( '\\' ))
+ 1);
// get working directory TCHAR m_wsBuffer[255]; _tcscpy_s(m_wsBuffer,wsDir.c_str()); // ParseDisplayName:Translates
a file object's or folder's display name into an item identifier list. hr = psfDesktop->ParseDisplayName(NULL, NULL, m_wsBuffer, &ulParseLen, &pidlWorkDir, NULL); hr = psfDesktop->BindToObject(pidlWorkDir, NULL, IID_IShellFolder, (LPVOID*)&psfWorkDir);
psfDesktop->Release(); psfDesktop = NULL; pMalloc->Free(pidlWorkDir); pidlWorkDir = NULL;
// retrieve link information _tcscpy_s(m_wsBuffer,wsFile.c_str()); hr = psfWorkDir->ParseDisplayName(NULL, NULL, m_wsBuffer, &ulParseLen, &pidlURL, NULL);
// query IExtractImage hr = psfWorkDir->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST*)&pidlURL, IID_IExtractImage, NULL, (LPVOID*)&peiURL);
// define thumbnail
properties hr = peiURL->GetLocation(pszPath, MAX_PATH*2, &dwPriority, &size, 16, &dwFlags);
// generate thumbnail HBITMAP hThumbnail= NULL; hr = peiURL->Extract(&hThumbnail);
// clean-up IExtractImage peiURL->Release(); peiURL = NULL;
myImage.Attach(hThumbnail); myImage.Save(TempTumbnail.c_str());
// free allocated structures 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();