| Tenouk C & C++ | MFC Home | Splitter Windows & Multiple Views 2 | Splitter Windows & Multiple Views 4 | Download | Site Index |


 

 

 

 

 

 

Module 14b:

Splitter Windows and Multiple Views 3

 

 

 

 

 

 

 

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. The MYMFC21C Example: Switching View Classes Without a Splitter

  2. Resource Requirements

  3. CMainFrame Class

  4. Testing the MYMFC21C Application

 

 

 

The MYMFC21C Example: Switching View Classes Without a Splitter

 

Sometimes you just want to switch view classes under program control and you don't want to be bothered with a splitter window. The MYMFC21C example is an SDI application that switches between CStringView and CHexView in response to selections on the View menu. Starting with what AppWizard generates, all you need to do is add two new menu commands and then add some code to the CMainFrame class. You also need to change the CStringView and CHexView constructors from protected to public. We will use MYMFC21B project. Copy the MYMFC21B project folder to another folder as a backup and use the original one for this exercise. You can copy Visual C++ project (directory) to any other directory or computer, then you can edit and run the project as usual.

 

Resource Requirements

 

The following two items have been added to the View menu in the IDR_MAINFRAME menu resource.

 

Caption

Command ID

CMainFrame Function

St&ring View

ID_VIEW_STRINGVIEW

OnViewStringView()

&Hex View

ID_VIEW_HEXVIEW

OnViewHexView()

 

Table 1

 

Adding and modifying menu items properties.

 

Figure 26: Adding and modifying menu items properties.

 

ClassWizard was used to add the command-handling functions (and corresponding update command UI handlers) to the CMainFrame class.

 

Adding the command-handling functions and corresponding update command UI handlers.

 

Figure 27: Adding the command-handling functions and corresponding update command UI handlers.

 

CMainFrame Class

 

The CMainFrame class gets a new private helper function, SwitchToView(), which is called from the two menu command handlers. The enum parameter tells the function which view to switch to. Here are the two added items in the MainFrm.h header file, added manually:

 

private:

    enum eView { STRING = 1, HEX = 2 };

    void SwitchToView(eView nView);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Listing 24.

 

The SwitchToView() function (in MainFrm.cpp) makes some low-level MFC calls to locate the requested view and to activate it. Don't worry about how it works. Just adapt it to your own applications when you want the view- switching feature. Add the following code manually:

 

void CMainFrame::SwitchToView(eView nView)

{

    CView* pOldActiveView = GetActiveView();

    CView* pNewActiveView = (CView*) GetDlgItem(nView);

    if (pNewActiveView == NULL) {

        switch (nView) {

        case STRING:

            pNewActiveView = (CView*) new CStringView;

            break;

        case HEX:

            pNewActiveView = (CView*) new CHexView;

            break;

        }

        CCreateContext context;

        context.m_pCurrentDoc = pOldActiveView->GetDocument();

        pNewActiveView->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, this, nView, &context);

        pNewActiveView->OnInitialUpdate();

    }

    SetActiveView(pNewActiveView);

    pNewActiveView->ShowWindow(SW_SHOW);

    pOldActiveView->ShowWindow(SW_HIDE);

    pOldActiveView->SetDlgCtrlID(

        pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CStringView) ? STRING : HEX);

    pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);

    RecalcLayout();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

Visual C++ code segment

 

Listing 25.

 

Finally, here are the menu command handlers and update command UI handlers that ClassWizard initially generated along with message map entries and prototypes. The update command UI handlers test the current view's class.

 

void CMainFrame::OnViewStringView()

{

    SwitchToView(STRING);

}

 

void CMainFrame::OnUpdateViewStringView(CCmdUI* pCmdUI)

{

    pCmdUI->Enable(!GetActiveView()->IsKindOf(RUNTIME_CLASS(CStringView)));

}

 

void CMainFrame::OnViewHexView()

{

    SwitchToView(HEX);

}

 

void CMainFrame::OnUpdateViewHexView(CCmdUI* pCmdUI)

{

    pCmdUI->Enable(!GetActiveView()->IsKindOf(RUNTIME_CLASS(CHexView)));

}

 

Visual C++ code segment

 

Listing 26.

 

Change the CStringView (StringView.h) and CHexView (HexView.h) constructors from protected to public.

 

Visual C++ code segment

 

Listing 27.

 

Visual C++ code segment

 

Listing 28.

 

Testing the MYMFC21C Application

 

The MYMFC21C application initially displays the CStringView view of the document. You can toggle between the CStringView and CHexView views by choosing the appropriate command from the View menu. Both views of the document are shown side by side in Figure 28 and 29.

 

MYMFC21C program output with two new view menus.

 

Figure 28: MYMFC21C program output with two new view menus.

 

 

The CStringView view and the CHexView view of the document.

 

Figure 29: The CStringView view and the CHexView view of the document.

 

 

Continue on next module...part 4.

 

 

 

 

Further reading and digging:

  1. MSDN MFC 7.0 class library online documentation.

  2. MSDN MFC 9.0 class library online documentation - latest version.

  3. MSDN Library

  4. Windows data type.

  5. Win32 programming Tutorial.

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

  7. Unicode and Multibyte character set: Story and program examples.

 

 

 

 


 

| Tenouk C & C++ | MFC Home | Splitter Windows & Multiple Views 2 | Splitter Windows & Multiple Views 4 | Download | Site Index |