Click here to Skip to main content
12,070,248 members (28,609 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

HexView - an introductory MFC application

, 27 Jan 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article introduces MFC programming basics

HexView screenshot


With the Application Wizard of the Visual Studio, you can create an MFC application with just a few mouse clicks. But you also can build your MFC application from scratch. In this article, we build a simple application that displays the contents of a file in hexadecimal values.

Using the code

Any MFC application is represented by a class that inherits from CWinApp. We call our application class CHexApp:

// HexApp.h

class CHexApp : public CWinApp 
    virtual BOOL InitInstance();



Most of the functionality necessary to a Windows application is buried inside inherited classes that are provided by MFC. We only need to specify what is specific to our application. Therefore, we override the InitInstance method that is called by the framework.

// HexApp.cpp

BOOL CHexApp::InitInstance()
    // Fensterobjekt erzeugen
    CMDIFrameWnd* pFrame = new CMainFrame;

        // Schablone für Dokument erzeugen
        AddDocTemplate(new CMultiDocTemplate(

        // Resourcen laden
        // Fenster zeigen

        // Fensterobjekt zuweisen
        m_pMainWnd = pFrame;

        return TRUE;

    return FALSE;

In this method, we create the main window of our application. As we want to display child windows inside it, our main window inherits from the CMDIFrameWnd MFC class:

// MainFrame.h

class CMainFrame : public CMDIFrameWnd

    BOOL LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,
                   CWnd* pParentWnd, CCreateContext* pContext);
    BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* /*pContext*/);

    afx_msg void OnGetMinMaxInfo(MINMAXINFO* lpMMI);


The classes CHexDoc and CHexView build the heart of our HexView application. They implement the document-view mechanism which is part of most MFC applications. CHexDoc is responsible for reading the contents of a file into memory. This is done inside the Serialize method:

void CHexDoc::Serialize(CArchive& archive)
    // Dokument soll geladen werden
        // Unterliegende Datei ermitteln
        CFile* pFile = archive.GetFile();

            // Länge der Datei ermitteln
            m_dwLength = pFile->GetLength();

            // Entsprechend Speicher allokieren
            m_pData = new BYTE[m_dwLength];

                // Komplette Datei in Speicher laden
                m_dwLength = pFile->Read(m_pData, m_dwLength);

The OnDraw method of class CHexView displays the data loaded into memory. Via GetDocument, the view object gets a reference to the document and its public members m_dwLength and m_pData. OnDraw then does a bit string manipulation and prints out the file contents line-by-line.

Other methods of this class (e.g. OnKeyDown) are responsible for handling user interactions.

void CHexView::OnDraw(CDC* pDC)
    // Position of Scrollbalken
    m_dwOffset = (GetScrollPos(SB_VERT) & 0xfffffff0);
    // workaround for offset problem

    // Hole Zeiger auf Dokumentobjekt
    CHexDoc* pDoc = (CHexDoc*) GetDocument();

    // Jede Zeile ausgeben
    for(DWORD dwLine = 0, dwOffset = m_dwOffset; 
        dwLine < m_dwPageLines; dwLine++, dwOffset += 16)
        // Anzahl Zeichen in aktueller Zeile
        DWORD dwCount = 16 < (dwCount = pDoc->m_dwLength - 
                                 dwOffset) ? 16 : dwCount;

        CString strLeft, strRight, strFormat;

        for(register DWORD dwChar = 0; dwChar < dwCount; dwChar++)
            // Einzelnes Zeichen hinzufügen
            strLeft  += " %02x";
            strRight += "%c";

            // Umwandlung in (hexadezimales) Zeichen
            strLeft.Format(strFormat = strLeft, 
                    BYTE(pDoc->m_pData[dwOffset + dwChar]));
            strRight.Format(strFormat = strRight, 
                     CHAR(pDoc->m_pData[dwOffset + dwChar]));

            // Offset einfügen
            strFormat.Format("%06x   ", dwOffset);

            // Ausgabezeile zusammenfügen
            strFormat += strLeft + CString(' ', 
                         49 - 3 * dwChar) + strRight;

        // Schriftart zur Ausgabe wählen

        // Zeile in Fenster ausgeben
        pDC->TextOut(10, dwOffset, strFormat);

Points of Interest

  • The MFC framework assumes certain guidelines concerning the layout of an application. E.g., in a menu, a minimum of two entries (File, ?) are required. If not present, we get an assertion failure. If an application, like HexView, shall contain only a single menu entry, we have to override the methods LoadFrame and OnCreateClient in our main frame window class. The implementation is as follows:
    // MainFrame.cpp
    BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,
            CWnd* pParentWnd, CCreateContext* pContext)
        // Klasse CMDIFrameWnd überspringen
        return CFrameWnd::LoadFrame(nIDResource, dwDefaultStyle,
                                    pParentWnd, pContext);
    BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* p)
        // Klasse CMDIFrameWnd überspringen
        return CreateClient(lpcs, NULL);
  • Scrolling with the mouse wheel may cause a problem. It seems to intervene the displaying mechanism. The line offsets go wrong and lines are printed only halfway. A first fix is m_dwOffset = (GetScrollPos(SB_VERT) & 0xfffffff0).


  • 12-20-2005: published HexView at The Code Project.
  • 01-25-2006: fixed problem when displaying small files.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMisleading and incorrect Pin
Chris Hills9-Feb-06 2:09
memberChris Hills9-Feb-06 2:09 
GeneralRe: Misleading and incorrect Pin
vl10612-Feb-06 4:07
membervl10612-Feb-06 4:07 
GeneralBeing a greedy person... Pin
Iain Clarke1-Feb-06 23:36
memberIain Clarke1-Feb-06 23:36 
GeneralBug, cosmetic change Pin
Roger657-Jan-06 4:18
memberRoger657-Jan-06 4:18 
GeneralRe: Bug, cosmetic change Pin
vl10625-Jan-06 8:36
membervl10625-Jan-06 8:36 
AnswerRe: Bug, cosmetic change Pin
curtj at morpho dot com31-Jan-06 13:05
membercurtj at morpho dot com31-Jan-06 13:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160208.1 | Last Updated 27 Jan 2006
Article Copyright 2006 by vl106
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid