前些天在论坛上发帖,询问有关Windows文件夹中显示Excel文件预览的问题。在Excel保存文件之前选择【文件】——>【属性】——>【保存文件预览】,这样保存成的Excel文件在Window文件夹左面的详细信息面板中可以看到预览图片,向JPG图片一样。安装了PDF阅读软件也一样。
有人回复说使用IFileViewer,于是花了两天时间调研Windows shell编程,下了好几本有关Windows Shell编程的书籍,都没有找到IFileViewer的使用方法,本人对COM技术懂一些,但没有太多的时间系统的学习一下Windows Shell。于是问题滞留不前。
有一天忽然间发现在文件夹中以缩略图的方式浏览,也可以看到预览图片,于是就想到换一种方法,得到Windows的文件缩略图,然后将其显示在窗口上。事实证明该方法确实有效。原来的想法是,用IFileViewer加载一个文件,然后传进去一个窗口句柄,就可以显示该文件的预览图片了。
下面介绍获得Windows文件缩略图的方法:
HRESULT
CShortCutDlg::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;
}
//
获得IMalloc接口
hr = SHGetMalloc( &pMalloc );
if ( FAILED( hr ) )
{
goto OnExit;
}
//
获得桌面文件夹
hr = SHGetDesktopFolder(&psfDesktop);
if(FAILED(hr)) goto OnExit;
//// create shortcut if URL
//if(PathIsURLW(m_wsPath.c_str())) {
// wsTempFile = m_wsTempPath; wsTempFile += TEMP_URL;
// //
自定义函数CreateURLShortcut
// if(!CreateURLShortcut(m_wsPath.c_str(), wsTempFile.c_str())) goto OnExit;
// wsDir = m_wsTempPath;
// wsFile = TEMP_URL;
//}
//else {
// wsDir = m_wsDir;
// wsFile = m_wsFile;
//}
wsDir = m_wsDir;
wsFile = m_wsFile;
// get working directory
wcscpy(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);
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;
// retrieve link information
wcscpy(m_wsBuffer,wsFile.c_str());
hr = psfWorkDir->ParseDisplayName(NULL, NULL, m_wsBuffer, &ulParseLen, &pidlURL, NULL);
if(FAILED(hr)) goto OnExit;
// query IExtractImage
hr = psfWorkDir->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST*)&pidlURL, IID_IExtractImage, NULL, (LPVOID*)&peiURL);
if(FAILED(hr)) goto OnExit;
// define thumbnail properties
hr = peiURL->GetLocation(pszPath, MAX_PATH, &dwPriority, &size, 16, &dwFlags);
if(FAILED(hr)) goto OnExit;
// generate thumbnail
hr = peiURL->Extract(pThumbnail);
if(FAILED(hr)) goto OnExit;
// clean-up IExtractImage
peiURL->Release();
peiURL = NULL;
OnExit
:
// 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();
CoUninitialize();
return hr;
}
该函数中,需要文件所在文件夹的路径和文件名。注意不需要文件的全路径。此函数执行成功,将文件的缩略图句柄保存在第三个参数中,之后的事情就是用GDI将改图像显示在界面上了。
附源码:我的资源
获取Windows文件的缩略图
本文介绍了如何通过Windows Shell编程获取Excel文件的预览缩略图。通过调用IExtractImage接口,从Shell文件夹中请求并提取文件的缩略图,然后在应用程序窗口上显示。提供的代码示例展示了具体实现过程。
1792





