Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید








The Ex18d Example: A Multiple View Class MDI Application


The final example, Ex18d, 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 Ex18d, you'll gain more insight into the use of CDocTemplate objects.

This example was generated with the Context-Sensitive Help option on the Advanced Features page of the MFC Application Wizard. If you're starting from scratch, use the wizard 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


Two items have been added to the Window menu in the IDR_Ex18dTYPE menu resource:
















Caption


Command ID


CMainFrame Function


New &String Window (replaces New Window item)


ID_WINDOW_NEWSTRINGWINDOW


CMDIFrameWnd::OnWindowNew


New &Hex Window


ID_WINDOW_NEWHEXWINDOW


OnWindowNewhexwindow


Class View's Properties window was used to add the command-handling function OnWindowNewhexwindow to the CMainFrame class.



CEx18dApp


In the application class header file,

Ex18d.h , the following data member and function prototype have been added:

public:
CMultiDocTemplate* m_pTemplateHex;

The implementation file,

Ex18d.cpp , contains the #include statements shown here:

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

The CEx18dApp InitInstance member function has the code shown below inserted immediately after the AddDocTemplate function call:

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

The AddDocTemplate call generated by the MFC Application Wizard 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 command.

Now all you need is an ExitInstance member function, which overrides the WinApp::ExitInstance to clean up the secondary template:

int CEx18dApp::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 command on the Window menu. The New String Window command is mapped to this function in Ex18d. The New Hex Window command 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::OnWindowNewhexwindow() 
{
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 =
((CEx18dApp*) 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);
}





Note

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 \Vc7\atlmfc\src\mfc subdirectory into your derived class, changing it as required. The only danger with cloning is that subsequent versions of the MFC library might implement the original function differently.




Testing the Ex18d Application


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




/ 319