The EX20D Example -- A Multiple View Class MDI Application

The final example, EX20D, uses the previous document and view classes to create a multiple view class MDI application without a splitter window. The logic is different from the logic in the other multiple view class applications. This time the action takes place in the application class in addition to the main frame class. As you study EX20D, you'll gain more insight into the use of CDocTemplate objects.

This example was generated with the AppWizard Context-Sensitive Help option. In Chapter 21, you'll activate the context-sensitive help capability.

If you're starting from scratch, use AppWizard to generate an ordinary MDI application with one of the view classes. Then add the second view class to the project and modify the application class files and main frame class files as described in the following sections.

Resource Requirements

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

CaptionCommand IDCMainFrame Function
New &String Window (replaces New Window item)ID_WINDOW_NEW_STRINGCMDIFrameWnd::OnWindowNew
New &Hex WindowID_WINDOW_NEW_HEX OnWindowNewHex

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

CEx20dApp

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

public:     CMultiDocTemplate* m_pTemplateHex;

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

#include "PoemDoc.h" #include "StringView.h" #include "HexView.h"

The CEx20dApp InitInstance member function has the code shown below inserted immediately after the AddDocTemplate function call.

m_pTemplateHex = new CMultiDocTemplate(     IDR_EX20DTYPE,     RUNTIME_CLASS(CPoemDoc),     RUNTIME_CLASS(CChildFrame),     RUNTIME_CLASS(CHexView));

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:

int CEx20dApp::ExitInstance() {     delete m_pTemplateHex;     return CWinApp::ExitInstance(); // saves profile settings }

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"

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 EX20D. 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 =         ((CEx20dApp*) 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); }

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 EX20D Application

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

click to view at full size.



Programming Microsoft Visual C++
Programming Microsoft Visual C++
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net