( Index )
Month

Brief Information about the Nov '03 CSIG Meeting

The Hex Dump Program and MFC SDI

B. Arnold

Hex Dump

Welcome to the CSIG, a Special Interest Group of the ACGNJ. The subject for this month is a further journey into C++ Windows programming using the example that we started last month. This is a Hex Dump Utility for displaying the information inside any file. The "command line code" (sometimes call DOS code) has been merged with Windows code created by the MFC (Microsoft Foundation Classes), SDI (Single Document Interface) application generator in the Microsoft Visual C++ version 6.0 compiler. Now that we have created a basic Windows program it is time to add features that will make it similar to commercial programs.

There are small and large features that I would like to add. The most complicated feature is printing. Let's tackle that first and spend a significant portion of the meeting discussing the details of adding a printing capability to the Microsoft MFC, SDI interface. There are a number of procedures that control printing. Some of these were just "stubs" in the previous program. These procedures include OnPreparePrinting(), OnBeginPrinting(), OnEndPrinting(), OnDraw(), and OnPrint(). This will add the ability to print to the printer of choice; to print all pages or just a few; to select and print just the selection; and to use the print preview functionality of Windows. Also included in the printing code will be the creation of the proper font for the printer.

There are several other items to be added. How about a percent indicator showing the progress of loading the file and a READY message when it's done. For the progress indicator we will use an undocumented Windows message called WM_KICKIDLE. How about copying a selection to the clipboard. We already have Drag-and-Drop to the program icon via the default Windows interface. How about Drag-and-Drop right onto the open program.

In summary, the program allows the viewing of information within otherwise unreadable files such as executables, zip files, bitmap files, etc. As shown in the example, it produces a hexadecimal dump of the target file.

Sample Code

void CDumpwView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
    {
    // Use a 10 Point, Fixed Pitch font.
    mFont.CreatePointFont(100, "Courier New", pDC);
    }

void CDumpwView::OnEndPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    mFont.DeleteObject();                               // Bruce_Arnold
    }

/////////////////////////////////////////////////////////////////////////////
// OnDraw - Render the text on the graphical device.
                                                                // *** ADDED__CODE ***
void CDumpwView::OnDraw(CDC* pDC) 
    {
    ASSERT(pDC->m_hDC != NULL);
    int i, idx, lines;  char buff[300];

    int x = pDC->GetDeviceCaps(LOGPIXELSX) / 2;                 // 1/2 inch border
    int dy= pDC->GetDeviceCaps(LOGPIXELSY) / 6;                 // 6 lines per inch
    int topMargin = 6*dy - pDC->GetDeviceCaps(PHYSICALOFFSETY); // 1 inch down
    // int ht = pDC->GetDeviceCaps(VERTRES); // Assume 11 inch paper.  Typ ht=3141

    lines = m_List.GetCount();
    idx = (m_nCurPage - 1) * LINESPERPAGE;

    if (pDC->IsPrinting())
        {
        x = x;          // dummy code for debug break point
        }
    
    pOldFont = pDC->SelectObject(&mFont);               // Change to our custom Font

    for (i=0; idx<lines && i<LINESPERPAGE; i++,idx++) 
        {
        m_List.GetText(idx, buff);
        pDC->TextOut(x, topMargin + i*dy, buff);        // Render on the graphical device
        }

    i = LINESPERPAGE+1;                                 // move to bottom of page plus 1

    CString sTmp = GetDocument()->GetPathName();        // Put name on Footer left.
    wsprintf(buff, "%s", sTmp.GetBuffer(100));
    pDC->TextOut(x,   topMargin + i*dy, buff);

    wsprintf(buff, "Page %d", m_nCurPage);              // Put page on Footer right.
    pDC->TextOut(14*x, topMargin + i*dy, buff);

    pDC->SelectObject(pOldFont);                        // Deselect custom Font
    }

/////////////////////////////////////////////////////////////////////////////
// OnPrint - Render the text on the graphical device.
                                                                // *** ADDED__CODE ***
void CDumpwView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
    //
    //  Note: OnPrepareDC and OnDraw are normally used in all cases (screen rendering
    //        and printer rendering).  In our case, the CListBox handles the screen
    //        rendering and this one, OnPrint(), is only used for printing and print 
    //        previewing via the CDumpwView::OnDraw() function above.
    //
    m_nCurPage = pInfo->m_nCurPage;     // update my local variable.
    CFormView::OnPrint(pDC, pInfo);     //  ... OnDraw()
    }

"Random Access" questions start at 7:30 Tuesday night.

SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page