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:
|
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.
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.
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();
}
----------------------------------------------------------------
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)));
}
Listing 26.
Change the CStringView (StringView.h) and CHexView (HexView.h) constructors from protected to public.
Listing 27.
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.
Figure 28: MYMFC21C program output with two new view menus. |
Figure 29: The CStringView view and the CHexView view of the document.
Continue on next module...part 4.
Further reading and digging:
MSDN What's New (MFC Feature Pack) - feature pack.
DCOM at MSDN.
COM+ at MSDN.
COM at MSDN.
Unicode and Multi-byte character set: Story and program examples.