I'm just curious how can I make my program to draw thumbnail of file of it's type in windows explorer like a .gif file?
Is there eny resource about this? I'd prefer it for MFC.
Sorry for my bed English
This is a discussion on Curious about thumdnails within the Windows Programming forums, part of the Platform Specific Boards category; I'm just curious how can I make my program to draw thumbnail of file of it's type in windows explorer ...
I'm just curious how can I make my program to draw thumbnail of file of it's type in windows explorer like a .gif file?
Is there eny resource about this? I'd prefer it for MFC.
Sorry for my bed English
EDIT: Whoops, I just realised your post is asking how to create a thumbnail provider rather than how to get a thumbnail.
WARNING: The following post is rated R. It contains frequent use of goto.
- The following code is in C and is adapted from much more complicated C++ code here. See ThumbExtract\FileThumbExtract.cpp in the zip sample.
- This is not restricted to loading thumbnails for images. Other application install thumbnail providers for their document formats. Unfortunately, the web page thumbnail provider seems to have been removed in XP(?Why?). It is available in previous versions(9x/2000/NT).
- The sample code simply blits the thumbnail to the screen to keep the code short.
- This code will be fairly uncomprehensible if you have never seen the shell interfaces, even more so if you are not familiar with COM.
- Post back if you have queries.
Code:#include <shlobj.h> #include <stdio.h> #include <wchar.h> #include <tchar.h> #define SAFE_RELEASE(pObj) if (pObj) { (pObj)->lpVtbl->Release(pObj); (pObj) = NULL; } #define SAFE_MALLOC_FREE(pv) if (pv) { IMallocFree(pv); (pv) = NULL; } #define HR_TRY(func) if (FAILED(func)) { printf("Error: %s\n", #func); goto cleanup; } VOID IMallocFree(LPVOID pv) { static IMalloc * pMalloc; if (!pMalloc) SHGetMalloc(&pMalloc); if (pMalloc) pMalloc->lpVtbl->Free(pMalloc, pv); } HRESULT CreateThumbnail(LPOLESTR szPath, LPOLESTR szFile, DWORD dwWidth, DWORD dwHeight, HBITMAP * pThumbnail) { LPITEMIDLIST pidlURL = NULL, pidlWorkDir = NULL; DWORD dwPriority = 0, dwFlags = IEIFLAG_ASPECT; HRESULT hr; SIZE size; WCHAR szBuffer[MAX_PATH]; IExtractImage * peiURL = NULL; IShellFolder * psfWorkDir = NULL; IShellFolder * psfDesktop = NULL; CoInitialize(NULL); size.cx = dwWidth; size.cy = dwHeight; HR_TRY(hr = SHGetDesktopFolder(&psfDesktop)); HR_TRY(hr = psfDesktop->lpVtbl->ParseDisplayName(psfDesktop, NULL, NULL, szPath, NULL, &pidlWorkDir, NULL)); HR_TRY(hr = psfDesktop->lpVtbl->BindToObject(psfDesktop, pidlWorkDir, NULL, &IID_IShellFolder, &psfWorkDir)); HR_TRY(hr = psfWorkDir->lpVtbl->ParseDisplayName(psfWorkDir, NULL, NULL, szFile, NULL, &pidlURL, NULL)); HR_TRY(hr = psfWorkDir->lpVtbl->GetUIObjectOf(psfWorkDir, NULL, 1, &pidlURL, &IID_IExtractImage, NULL, &peiURL)); HR_TRY(hr = peiURL->lpVtbl->GetLocation(peiURL, szBuffer, MAX_PATH, &dwPriority, &size, 16, &dwFlags)); HR_TRY(hr = peiURL->lpVtbl->Extract(peiURL, pThumbnail)); cleanup: SAFE_MALLOC_FREE(pidlWorkDir); SAFE_MALLOC_FREE(pidlURL); SAFE_RELEASE(peiURL); SAFE_RELEASE(psfDesktop); SAFE_RELEASE(psfWorkDir); CoUninitialize(); return hr; } int main(void) { HRESULT hr; HBITMAP hBitmap; if (FAILED( hr = CreateThumbnail(L"C:\\Documents and Settings\\User\\My Documents\\", L"test.gif", 120, 120, &hBitmap) )) { TCHAR msg[256]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, hr, LANG_USER_DEFAULT, msg, 256, NULL); _tprintf(_T("hr = %x, msg = %s\n"), hr, msg); } else { HDC hdc = CreateCompatibleDC(NULL); HBITMAP hOld = SelectObject(hdc, hBitmap); BitBlt(GetDC(NULL), 0, 0, 120, 120, hdc, 0, 0, SRCCOPY); SelectObject(hdc, hOld); DeleteDC(hdc); DeleteObject(hBitmap); } getchar(); return 0; }
Last edited by anonytmouse; 03-08-2004 at 09:05 AM.