Create Thumbnail Extractor objects for your MFC document types

View: 1635    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

Download  create-thumbnail-extractor.zip (411.35 KB)

You need to Sign In to download the file create-thumbnail-extractor.zip
If you do not have an account then Sign up for free here

CScribbleExtractor COM object 

The MFC-based thumbnail extension for Scribble (ThumbScb project) has been created as an MFC regular DLL. After the AppWizard i added an ATL object to the project. The object was coded to implement the two interfaces needed: IPersistFile to know about the file currently selected in the shell, and IExtractImage2 (derived from IExtractImage) to access the document and return a bitmap that renders its content.

// ScribbleExtractor.h
class ATL_NO_VTABLE CScribbleExtractor :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CScribbleExtractor, &CLSID_ScribbleExtractor>,
    public IPersistFile,
    public IExtractImage2
public: ...

// ScribbleExtractor.cpp
// IExtractImage::Extract
HRESULT CScribbleExtractor::Extract(HBITMAP* phBmpThumbnail)

    m_hPreview = theApp.CreateThumbnail(m_bmSize);
    *phBmpThumbnail = m_hPreview;
    return NOERROR;
// Code for other interface functions is omitted since it is boiterblate.

CExtractImageApp generic application class

As you can see, the COM object delegates document loading and thumbnail generation to the main application object. This is convenient because the CWinApp-derived class can determine the supported document types and create corresponding document objects via the document manager mechanism. In order to reuse as much code as possible i implemented the LoadDoc and CreateThumbnail functions in a generic CWinApp-derived class named CExtractImageApp. The first is implemented with generic MFC code. The helper function CanOpenDocument returns an appropriate document template that can create a document object to serve the file. The document object is dynamically created and then the content of the file is loaded from disk.  Loading code is mainly copy-pasted from the MFC OnOpenDocument function.

// Load document. This function is responsible for opening the document
// and setting m_pOpenDoc to the document loaded.
BOOL CExtractImageApp::LoadDoc(LPCTSTR lpFileName)
    CString sFileName = lpFileName;
    CDocTemplate* pDocTemplate = CanOpenDocument(sFileName); // helper function 
                                                             // defined above
    if (pDocTemplate) {
        if(!m_pDoc) {
            m_pDoc = pDocTemplate->CreateNewDocument();
            m_pDoc->m_bAutoDelete = TRUE;
        if (m_pDoc)
            // load content of file, code taken from MFC OnOpenDocument and modified
            CFileException fe;
            CFile* pFile = m_pDoc->GetFile(sFileName,    CFile::modeRead, &fe);
            if (pFile == NULL)
                return FALSE;

            CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete);
            loadArchive.m_pDocument = m_pDoc;
            loadArchive.m_bForceFlat = FALSE;
                if (pFile->GetLength() != 0)
                    m_pDoc->Serialize(loadArchive);     // load me
                m_pDoc->ReleaseFile(pFile, FALSE);
            catch(CException *e)
                m_pDoc->ReleaseFile(pFile, TRUE);
                m_pDoc->DeleteContents();   // remove failed contents
                return FALSE;
            return TRUE;
            //delete pDoc;
    return FALSE;

CreateThumbnail function creates the thumbnail bitmap and draws the document content on it. It is essentially a template function because it calls the GetDocSize and OnDraw functions that are pure virtual (must be implemented by the derived class). Drawing is done in isotropic mapping to match document dimensions with thumbnail dimensions. The minus sign in SetViewportExt

HBITMAP CExtractImageApp::CreateThumbnail(const SIZE bmSize)
    HBITMAP hThumb; CBitmap bmpThumb;
    if(!m_pDoc) return NULL;

    CSize bmDocSize = GetDocSize(); // derived class knows it
    // Create memory DC, create a color bitmap, and draw on it
    CDC memdc;
    CBitmap* pOldBm = memdc.SelectObject(&bmpThumb);

    OnDraw(&memdc); //let the derived class to handle it
    hThumb = (HBITMAP)bmpThumb.Detach();
    return hThumb;

Derived application class

In the main application class derived from CExtractImageApp, the above mentioned pure virtual functions are implemented by calling the respective functions of the already loaded document. Before this, I have to downcast the CDocument m_pDoc pointer to a CScribbleDoc pointer. If you have multiple document classes you can use the CObject::IsKindOf function to find the kind of document class m_pDoc points to. In the InitInstance function i add a template for the scribble document type.  CMDIChildWnd and CView are MFC classes, while CScribbleDoc is a simplified version of the original scribble document class. The only functions that CScribbleDoc  is required to implement for this project are the Serialize function called during document loading and the  GetDocSize and OnDraw functions called during thumbnail creation. Normally, OnDraw function belongs to the CScribbleView class but here there is no need for doc/view architecture. To simplify implementation i copied OnDraw from CScribbleView. In the first line i replaced GetDocument() with this

BOOL CThumbScbApp::InitInstance()
    if (!InitATL())
        return FALSE;
    // Create document template
    AddDocTemplate(new CMultiDocTemplate(IDR_SCRIBBTYPE,

    return CWinApp::InitInstance();

void CThumbScbApp::OnDraw(CDC *pDC)
    CScribbleDoc *mydoc = (CScribbleDoc *)m_pDoc;

CSize CThumbScbApp::GetDocSize()
    CScribbleDoc *mydoc = (CScribbleDoc *)m_pDoc;
    return mydoc->GetDocSize();

// scribdoc.cpp
void CScribbleDoc::OnDraw(CDC* pDC)
    CScribbleDoc* pDoc = this; //GetDocument();

How to debug

Debugging shell extensions is difficult. I have created a COM object (ThumbExtract project) that uses shell interfaces to get an IExtractImage for a file and then creates a bitmap. In the same folder you 'll find small VB6 EXE project called TestThumbExtract that references the ThumbExtract object. An instance of this test application is shown below. Click the button to select a file and a large thumbnail for that file will appear in the picture box. You can debug your shell extension by setting the TestThumbExtract.exe as the 'Executable for debug session' in Project/Settings/Debug. 

TestThumbExtract application

Thumbnail Project Wizard

Since code changes for a new thumbnail shell extension project are few, i made a custom AppWizard (ThumbWiz project) based on the ThumbScb project. The compiled wizard DLL is named ThumbWiz.awx and you must copy it to "C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Template" folder. An entry named "Thumbnail Project Wizard" appears then in the New Project dialog. The wizard has a custom step which askes your object's name (e.g. Scribble) and the file extensions supported (if you want to support many extensions separate them with commas). The names of the application class, the document class and the COM class created, are based on the object name you give. Also, for each file extension a new document template is created in InitInstance. New GUIDs are automatically generated. The sceleton code includes TODO comments indicating where to add the needed details. In CThumbWizAppWiz::CustomizeProject i customize the project settings (specifically the MIDL settings) and also create a post-build step to register the object. Registry entries under the file extension keys are created so that the system can determine what object to load for each file extension. Be careful not to replace the entries for well known types (like .jpg) because no backup of registry entries is taken.

Create Thumbnail Extractor objects for your MFC document types

Thumbnail views is a nice feature of Windows Explorer. However, little is known about how to create shell thumbnail extensions for custom documents. I was developing a medical image visualisation software and wanted this feature for the DICOM (medical) images that the software can load. After searching the Web i finally found a relevant article in the MSDN magazine: More Windows 2000 UI Goodies: Extending Explorer Views by Customizing Hypertext Template Files. The articles covers this topic and includes a simple image extractor for icon files. After creating my DICOM image extractor (i can give it upon request) i created also an image extractor shell extension for Scribble (MFC tutorial) documents and specifically for Scribble Step 5. I tried to write the code in an objected oriented way which promotes reuse (i am a "fun" of the OO "guru" Paul Dilascia, author of the MSDN mag). Finally, i converted the scribble image extractor project to a Custom AppWizard so that you can easily generate the sceleton code of an image extractor for your MFC documents. The figure above shows the thumbnail view of a folder containing a medical file and also my precious scribbles Smile | :)

Posted on 28-03-2014 


To comment you must be logged in members.

Files with category

  • Sound Player in Visual Studio C++ .NET

    Sound Player in Visual Studio C++ .NET

    View: 127    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This sound player project use the libZPlay from http://libzplay.sourceforge.net . Methods are called from library libzplay.dll with [InteropServices::DllImport("")]. The use of the library is relatively easy.

  • Quiz Management System in C++

    Quiz Management System in C++

    View: 128    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Hello World...!!! Though I am not an expert programmer but today i want to share my little knowledge of C/C++ programming by sharing this simple Quiz Management System with you people. I hope this may help some students in their mini...

  • General Quiz

    General Quiz

    View: 236    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is one of my first WPF projects, and it may be used as a template for a quiz test. The code can be improved but I think its useful for the ones who don't have experience with WPF.

  • SqlCommand to T-SQL Printer

    SqlCommand to T-SQL Printer

    View: 435    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    In this article, we are going to see a log printer working with Entity Framework that converts SqlCommand to a completely executable T-SQL statement, including all parameter declarations and write to log file. Once a command is executed, You will see...

  • Windows Clipboard Formats

    Windows Clipboard Formats

    View: 228    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    This reference gives you an overview of standard and common clipboard formats with links to the data structures.

  • Insert 1 element at the beginning of an array managed by a pointer without moving

    Insert 1 element at the beginning of an array managed by a pointer without moving

    View: 176    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    Insert 1 element at the beginning of an array managed by a pointer without moving

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 281    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    What if you have one or more modules, running independently in a system, which should collaborate between each other? Well, they will have to be able to exchange data, organized as events, messages or packets of some sort.

  • How to Swap Two Numbers using Call by Reference in C

    How to Swap Two Numbers using Call by Reference in C

    View: 721    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    How to swap two numbers using call by reference in C , C++. In the interviews interviewer generally asked the difference between call by reference and call by value

File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd