Enhance MFC Applications with Preview and Thumbnail Support

Introduction


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 (32×32) to 3
(16×16).

More by Author

Must Read