Enhance MFC Applications with Preview and Thumbnail Support
Introduction
Visual C++ 2010 adds the ability to simply implement Thumbnail and Preview support for Microsoft Foundation Classes (MFC) projects with a registered file extension. The file extension is a critical part of the Thumbnail and Preview support, as the COM DLL that handles the rendering of both thumbnails and previews is mapped by file extension. Figure 1 shows a file displayed in Windows Explorer with both a Thumbnail and Preview display rendered.
Figure 1. Thumbnail and Preview Display in Windows Explorer
Despite the similar functions of thumbnail and preview handling, the actual implementation of the COM interfaces to support the functionality is very different. Thumbnail support is similar to MFC-provided Search functionality covered in a previous article, with a method added to the CDocument-derived class in a project that handles the thumbnail rendering. The Document class is shared between two separate projects when thumbnail support is selected in the application wizard (the application wizard settings are shown in Figure 2) - the standard MFC application project that generate the main executable, and an ATL DLL project that produces a COM DLL that is loaded by Windows to generate the thumbnail for a project. The MFC AppWizard will also generate ATL Registrar Scripts (*.rgs) that will correctly register the COM DLL and associate it with the file extension for thumbnail rendering.
Figure 2. AppWizard Settings with Thumbnail and Preview Support
Conditional compilation directives mean that the thumbnail generation code is not actually included in the main executable. The default implementation for the thumbnail-rendering method generated by the AppWizard is:
void CXXXDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
// Modify this code to draw the document's data
dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));
CString strText = _T("TODO: implement thumbnail drawing here");
LOGFONT lf;
CFont* pDefaultGUIFont = CFont::FromHandle(
(HFONT) GetStockObject(DEFAULT_GUI_FONT));
pDefaultGUIFont->GetLogFont(&lf);
lf.lfHeight = 36;
CFont fontDraw;
fontDraw.CreateFontIndirect(&lf);
CFont* pOldFont = dc.SelectObject(&fontDraw);
dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
dc.SelectObject(pOldFont);
}
The COM interface required for thumbnail support is
IThumbnailProvider, and this has a single
method called GetThumbnail that requires
implementation. GetThumbnail is implemented
within the MFC CDocument class, and after some device
context preparation code,
CDocument::GetThumbnail will call through to
the virtual OnDrawThumbnail method that is
shown above. Windows Vista and Windows 7 supports thumbnails
of up to 256 by 256 pixels, so there is actually a quite
large rendering area available in some circumstances. By
default, Windows will not render the thumbnail for a
document below a size of 20 by 20 pixels, but this can be
customized by a registry entry at HKEY_CLASSES_ROOT |
.fileextension | ThumbnailCutoff. ThumbnailCutoff is a
DWORD, with cut-off size ranging from 0 (32x32) to 3
(16x16).

Comments
There are no comments yet. Be the first to comment!