| Tenouk C & C++ | MFC Home | Splitter Windows & Multiple Views 4 | Context-Sensitive Help 1 | Download | Site Index |


 

 

 

Module 14d:

Splitter Windows and Multiple Views 5

 

 

 

 

This is a continuation from the previous module... Program examples compiled using Visual C++ 6.0 (MFC 6.0) compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this Tutorial are listed below:

  1. Resource Requirements...a continuation...

  2. CMymfc21DApp Class

  3. CMainFrame Class

  4. Testing the MYMFC21D Application

 

Next, create CHexView by cloning the CStringView as shown in the previous example.

 

Creating and adding new files (class) to project.

 

Figure 41: Creating and adding new files (class) to project.

 

Adding empty HexView.h header file to the project.

 

Figure 42: Adding empty HexView.h header file to the project.

 

Copy the following cloned codes (StringView.h and StringView.cpp) into the HexView.h and HexView.cpp respectively.

 

HEXVIEW.H

// HexView.h : interface of the CHexView class

//

/////////////////////////////////////////////////////////////////////////////

 

#if !defined(AFX_CHEXVIEW_H__0A59E9CC_50F1_4B8E_8A3E_C3ED2955CE9D__INCLUDED_)

#define AFX_CHEXVIEW_H__0A59E9CC_50F1_4B8E_8A3E_C3ED2955CE9D__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

class CHexView : public CScrollView

{

protected: // create from serialization only

       CHexView();

       DECLARE_DYNCREATE(CHexView)

 

// Attributes

public:

       CPoemDoc* GetDocument();

 

// Operations

public:

 

// Overrides

       // ClassWizard generated virtual function overrides

       //{{AFX_VIRTUAL(CHexView)

       public:

       virtual void OnDraw(CDC* pDC);  // overridden to draw this view

       virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

       protected:

       virtual void OnInitialUpdate(); // called first time after construct

       virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);

       virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);

       virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

       //}}AFX_VIRTUAL

 

// Implementation

public:

       virtual ~CHexView();

#ifdef _DEBUG

       virtual void AssertValid() const;

       virtual void Dump(CDumpContext& dc) const;

#endif

 

protected:

 

// Generated message map functions

protected:

       //{{AFX_MSG(CHexView)

              // NOTE - the ClassWizard will add and remove member functions here.

              //    DO NOT EDIT what you see in these blocks of generated code !

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

private:

       CRect m_rectPrint;

};

 

#ifndef _DEBUG  // debug version in CHexView.cpp

inline CPoemDoc* CHexView::GetDocument()

   { return (CPoemDoc*)m_pDocument; }

#endif

 

/////////////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_CHEXVIEW_H__0A59E9CC_50F1_4B8E_8A3E_C3ED2955CE9D__INCLUDED_)

 

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

HEXVIEW.CPP

// HexView.cpp : implementation of the CHexView class

//

 

#include "stdafx.h"

#include "mymfc21D.h"

 

#include "PoemDoc.h"

#include "HexView.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

/////////////////////////////////////////////////////////////////////////////

// CHexView

 

IMPLEMENT_DYNCREATE(CHexView, CScrollView)

 

BEGIN_MESSAGE_MAP(CHexView, CScrollView)

       //{{AFX_MSG_MAP(CHexView)

              // NOTE - the ClassWizard will add and remove mapping macros here.

              //    DO NOT EDIT what you see in these blocks of generated code!

       //}}AFX_MSG_MAP

       // Standard printing commands

       ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)

       ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)

       ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)

END_MESSAGE_MAP()

 

/////////////////////////////////////////////////////////////////////////////

// CHexView construction/destruction

 

CHexView::CHexView() : m_rectPrint(0, 0, 11520, -15120)

{

       // TODO: add construction code here

 

}

 

CHexView::~CHexView()

{    }

 

BOOL CHexView::PreCreateWindow(CREATESTRUCT& cs)

{

       // TODO: Modify the Window class or styles here by modifying

       //  the CREATESTRUCT cs

 

       return CScrollView::PreCreateWindow(cs);

}

 

/////////////////////////////////////////////////////////////////////////////

// CHexView drawing

 

void CHexView::OnDraw(CDC* pDC)

{

// hex dump of document strings

    int        i, j, k, l, n, nHeight;

    CString    outputLine, str;

    CFont      font;

    TEXTMETRIC tm;

 

    CPoemDoc* pDoc = GetDocument();

    font.CreateFont(-160, 80, 0, 0, 400, FALSE, FALSE, 0,

        ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,

        DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");

    CFont* pOldFont = pDC->SelectObject(&font);

    pDC->GetTextMetrics(&tm);

    nHeight = tm.tmHeight + tm.tmExternalLeading;

 

    j = pDoc->m_stringArray.GetSize();

    for (i = 0; i < j; i++) {

        outputLine.Format("%02x   ", i);

        l = pDoc->m_stringArray[i].GetLength();

        for (k = 0; k < l; k++) {

            n = pDoc->m_stringArray[i][k] & 0x00ff;

            str.Format("%02x ", n);

            outputLine += str;

        }

        pDC->TextOut(720, -i * nHeight - 720, outputLine);

    }

    pDC->SelectObject(pOldFont);

}

 

void CHexView::OnInitialUpdate()

{

           CScrollView::OnInitialUpdate();

    CSize sizeTotal(m_rectPrint.Width(), -m_rectPrint.Height());

    CSize sizePage(sizeTotal.cx / 2, sizeTotal.cy / 2);   // page scroll

    CSize sizeLine(sizeTotal.cx / 100, sizeTotal.cy / 100); // line scroll

    SetScrollSizes(MM_TWIPS, sizeTotal, sizePage, sizeLine);

}

 

/////////////////////////////////////////////////////////////////////////////

// CHexView printing

 

BOOL CHexView::OnPreparePrinting(CPrintInfo* pInfo)

{

       pInfo->SetMaxPage(1);

    return DoPreparePrinting(pInfo);

}

 

void CHexView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

       // TODO: add extra initialization before printing

}

 

void CHexView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

       // TODO: add cleanup after printing

}

 

/////////////////////////////////////////////////////////////////////////////

// CHexView diagnostics

 

#ifdef _DEBUG

void CHexView::AssertValid() const

{

       CScrollView::AssertValid();

}

 

void CHexView::Dump(CDumpContext& dc) const

{

       CScrollView::Dump(dc);

}

 

CPoemDoc* CHexView::GetDocument() // non-debug version is inline

{

       ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPoemDoc)));

       return (CPoemDoc*)m_pDocument;

}

#endif //_DEBUG

 

/////////////////////////////////////////////////////////////////////////////

// CHexView message handlers

 

 

 

 

 

 

 

 

Listing 38: The CHexView class, a cloned CStringView class.

 

Resource Requirements

 

The two items below have been added to the Window menu in the IDR_MYMFC2TYPE menu resource.

 

Caption

Command ID

CMainFrame Function

New &String Window (replaces New Window item)

ID_WINDOW_NEW_STRING

CMDIFrameWnd::OnWindowNew (default when the program start)

New &Hex Window

ID_WINDOW_NEW_HEX

OnWindowNewHex

 

Table 2.

 

Adding and modifying menu items properties.

 

Figure 43: Adding and modifying menu items properties.

 

ClassWizard was used to add the command-handling function OnWindowNewHex() to the CMainFrame class.

 

Adding the command-handling function OnWindowNewHex() to the CMainFrame class.

 

Figure 44: Adding the command-handling function OnWindowNewHex() to the CMainFrame class.

 

CMymfc21DApp Class

 

In the application class header file, mymfc21D.h, the following data member and function prototype have been added:

 

public:

    CMultiDocTemplate* m_pTemplateHex;

 

Visual C++ code segment

 

Listing 39.

 

The implementation file, mymfc21D.cpp, contains the #include statements shown here:

 

#include "HexView.h"

 

Visual C++ code segment

 

Listing 40.

 

The CMymfc21DApp InitInstance() member function has the code shown below inserted immediately after the AddDocTemplate() function call.

 

m_pTemplateHex = new CMultiDocTemplate(

    IDR_MYMFC2TYPE,

    RUNTIME_CLASS(CPoemDoc),

    RUNTIME_CLASS(CChildFrame),

    RUNTIME_CLASS(CHexView));

 

 

Visual C++ code segment

 

Listing 41.

 

The AddDocTemplate() call generated by AppWizard established the primary document/frame/view combination for the application that is effective when the program starts. The template object above is a secondary template that can be activated in response to the New Hex Window menu item.

Now all you need is an ExitInstance() member function that cleans up the secondary template:

 

Adding an ExitInstance() member function that cleans up the secondary template.

 

Figure 45: Adding an ExitInstance() member function that cleans up the secondary template.

 

Entering the member function’s type and declaration.

 

Figure 46: Entering the member function’s type and declaration.

 

int CMymfc21DApp::ExitInstance()

{

    delete m_pTemplateHex;

    return CWinApp::ExitInstance(); // saves profile settings

}

 

Visual C++ code segment

 

Listing 42.

 

CMainFrame

 

The main frame class implementation file, MainFrm.cpp, has the CHexView class header (and the prerequisite document header) included:

 

#include "PoemDoc.h"

#include "HexView.h"

 

Visual C++ code segment

 

Listing 43.

 

The base frame window class, CMDIFrameWnd, has an OnWindowNew() function that is normally connected to the standard New Window menu item on the Window menu. The New String Window menu item is mapped to this function in MYMFC21D. The New Hex Window menu item is mapped to the command handler function below to create new hex child windows. The function is a clone of OnWindowNew(), adapted for the hex view-specific template defined in InitInstance().

 

void CMainFrame::OnWindowNewHex()

{

    CMDIChildWnd* pActiveChild = MDIGetActive();

    CDocument* pDocument;

    if (pActiveChild == NULL || (pDocument = pActiveChild->GetActiveDocument()) == NULL) {

        TRACE("Warning:  No active document for WindowNew command\n");

        AfxMessageBox(AFX_IDP_COMMAND_FAILURE);

        return; // Command failed

    }

 

    // Otherwise, we have a new frame!

    CDocTemplate* pTemplate = ((CMymfc21DApp*) AfxGetApp())->m_pTemplateHex;

    ASSERT_VALID(pTemplate);

    CFrameWnd* pFrame = pTemplate->CreateNewFrame(pDocument, pActiveChild);

    if (pFrame == NULL)

   {

        TRACE("Warning:  failed to create new frame\n");

        AfxMessageBox(AFX_IDP_COMMAND_FAILURE);

        return; // Command failed

    }

 

    pTemplate->InitialUpdateFrame(pFrame, pDocument);

}

 

Visual C++ code segment

 

Listing 44.

 

The function cloning above is a useful MFC programming technique. You must first find a base class function that does almost what you want, and then copy it from the \VC98\mfc\src subdirectory into your derived class, changing it as required. The only danger of cloning is that subsequent versions of the MFC library might implement the original function differently.

 

Testing the MYMFC21D Application

 

When you start the MYMFC21D application, a text view child window appears. Choose New Hex Window from the Window menu. The application should look like this.

 

MYMFC21D MDI program output with cascade view.

 

Figure 47: MYMFC21D MDI program output with cascade view.

 

Try the Cascade and Tile menus.

 

MYMFC21D MDI program output with tile view.

 

Figure 48: MYMFC21D MDI program output with tile view.

 

 

 

Further reading and digging:

  1. MSDN MFC 7.0 class library online documentation.

  2. MSDN What's New (MFC Feature Pack) - feature pack.

  3. Porting & Migrating your older programs.

  4. MSDN Library

  5. DCOM at MSDN.

  6. COM+ at MSDN.

  7. COM at MSDN.

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multi-byte character set: Story and program examples.

 

 

 

 

 


| Tenouk C & C++ | MFC Home | Splitter Windows & Multiple Views 4 | Context-Sensitive Help 1 | Download | Site Index |