//////////////////////////////////////// /////
/* 1. Review "InitInstance function "*/
//////////////////////////////////////// /////
BOOL CMyApp: InitInstance () // only lists Source Code related to the document view structure
{
// The document template is used as a link between the document, framework window, and view.
CMultiDocTemplate * pDocTemplate;
PDocTemplate = new CMultiDocTemplate (IDR_MyTYPE, // ------------------- 2
RUNTIME_CLASS (CMyDoc ),
RUNTIME_CLASS (CChildFrame), // customize the MDI sub-framework
RUNTIME_CLASS (CMyView ));
AddDocTemplate (pDocTemplate); // ---------------------------------------------------- 3
// Create the primary MDI frame window
CMainFrame * pMainFrame = new CMainFrame;
If (! PMainFrame-> LoadFrame (IDR_MAINFRAME) // ---------------------------- 4
Return FALSE;
M_pMainWnd = pMainFrame;
// Analyze standard shell commands, DDE, and command lines for file opening
CCommandLineInfo using info;
ParseCommandLine (partition info); // ---------------------------------------------------------- 5
// Schedule the command specified in the command line. If
// Use/RegServer,/Register,/Unregserver, or/Unregister to start the application, FALSE is returned.
If (! ProcessShellCommand (cmdInfo) // -------------------------------------------------- 6
Return FALSE;
// The main window is initialized, so it is displayed and updated.
Pmainframe-> showwindow (m_ncmdshow );
Pmainframe-> updatewindow ();
Return true;
}
//////////////////////////////////////// ////
/* 2. initialize the document template */
//////////////////////////////////////// ////
Analyze the following code:
Cmultidoctemplate * pdoctemplate;
Pdoctemplate = new cmultidoctemplate (idr_mytype,
Runtime_class (cmydoc ),
RUNTIME_CLASS (CChildFrame), // customize the MDI sub-framework
RUNTIME_CLASS (CMyView ));
The application first instantiates a CMultiDocTemplate object, which is also the initialization process for CMultiDocTemplate class data members.
Let's take a look at the parameter value passed by the InitInstance function to the constructor of CMultiDocTemplate.
It turns out to be some RUNTIME_CLASS macros. This is a difficult issue. This will involve another important technology of MFC-"type identification during the execution period", which is not described here
//////////////////////////////////////// ///
/* 3. Document Template queue */
//////////////////////////////////////// ///
After the document template Initialization is complete, the initinstance function calls the cwinapp: adddoctemplate (pdoctemplate) function to add the initialized document template to the document template linked list, and managed by cdocmanager class objects.
//////////////////////////////////////// ///////
/* 4. Create a main framework window */
//////////////////////////////////////// ///////
The application instantiates a cmainframe Class Object and calls the loadframe function to load window resources to create the main frame window.
The main code for creating a window is pmainframe-> loadframe (idr_mainframe). The loadframe function is a function encapsulated by MFC in the window creation process. When a child window is dynamically created later, it will also be mounted (but slightly different)
//////////////////////////////////////// //////////////
/* 4.1 register the main framework window class of the application */
//////////////////////////////////////// //////////////
In traditional Win32API programming, the common step for creating a window is to define a window class, register a window class, and call the: CreateWindow function to create a window. As mentioned above, the LoadFrame function encapsulates the window creation process of MFC, which means that the LoadFrame function is responsible for defining window classes and registering window classes.
In this step, LoadFrame calls AfxDeferRegisterClass (AFX_WNDFRAMEORVIEW_REG), CMainFrame: PreCreateWindow
//////////////////////////////////////// /////////
/* 4.2 The creation of the main framework window starts */
//////////////////////////////////////// /////////
Start the substantive stage of the framework creation window. What does the LoadFrame function do? It calls CFrameWnd: Create ==> CWnd: CreateEx ==> Win32API: createjavaswex, AfxHookWindowCreate (this function is related to message ing and command transfer ).
//////////////////////////////////////// //////
/* 5. Standard Shell Command Parsing */
//////////////////////////////////////// ///////
When the standard MDI application created by the MFC wizard is started, the application automatically starts a Child Window framework (actually a set of document templates)
CCommandLineInfo into Info; // ==> m_nShellCommand = FileNew;
ParseCommandLine (partition info); // ==> pass the command line parameter to ParseParam ()
If (! ProcessShellCommand (cmdInfo) // perform the corresponding operation based on the cmdInfo content. Because m_nShellCommand = FileNew, CWinApp: OnFileNew () is called.
Return FALSE;
//////////////////////////////////////// //////////
/* 6. A set of documents/views will be born soon */
//////////////////////////////////////// //////////
As mentioned above, CWinApp: OnFileNew () is called.
CWinApp: OnFileNew () ==> CDocManager: OnFileNew () ==> CMultiDocTemplate: OpenDocumentFile ()
CDocument * CMultiDocTemplate: OpenDocumentFile (LPCTSTR lpszPathName, BOOL bMakeVisible)
{
...
CDocument * pDocument =CreateNewDocument(); // Dynamic generation of sub-Documents
CFrameWnd * pFrame =CreateNewFrame(PDocument, NULL); // The Child Window framework is dynamically generated ==> CCreateContext context is created at the same time, and then LoadFrame is called
PDocument-> OnNewDocument ();
...
}
//////////////////////////////////////// //////
/*. Subview dynamic generation */
//////////////////////////////////////// //////
The key lies in CCreateContext context and LoadFrame in CreateNewFrame. The LoadFrame here contains the context Parameter
LoadFrame (..., & context) --> CFrameWnd: Create (..., & context) --> CWnd: CreateEx (..., & context) --> CWnd: createjavaswex // (a createstruct cs structure is created here as the message passing parameter)
CWnd: CreateEx
{
CREATESTRUCT cs; // The message structure created in the window
AfxHookWindowCreate (this); // hangs a message hook for CFrameWnd for message ing and command transmission. here this is the CFrameWnd created in CreateNewFrame.
HWND hWnd =: afxctxcreateappswex (cs); // create a window
ASSERT (hWnd = m_hWnd); // shold have been set in send msg hook
}
Void AFXAPI AfxHookWindowCreate (CWnd * pWnd)
{
_ AFX_THREAD_STATE * pThreadState = _ afxThreadState. GetData (); // pThreadState is a global object.
PThreadState-> m_pWndInit = pWnd; // pWnd is a CFrameWnd
}
: The createmediawex API function will generate the WM_CREATE message and pass the & context (which exists in the LPCREATESTRUCT. Note that the LPCREATESTRUCT is the structure pointer of CREATESTRUCT). CMainFrame: OnCreate will respond to the message, and cause a series of function calls
Int CMainFrame: OnCreate (region) ==> CFrameWnd: OnCreateHelper (CCreateContext) ==> CFrameWnd: OnCreateClient (CCreateContext) ==> CFrameWnd ::CreateView(CCreateContext)
Struct CCreateContext
{
CRuntimeClass * m_pNewViewClass; // for creating new views
CDocument * m_pCurrentDoc; // for creating MDI children (CMDIChildWnd: LoadFrame)
Cdoctemplate * m_pnewdoctemplate; // for sharing view/frame state from the original view/Frame
Cview * m_plastview;
Cframewnd * m_pcurrentframe;
};
Cframewnd: oncreate (lpcreatestruct lpcs)
{
Ccreatecontext * pcontext = (ccreatecontext *) lpcs-> lpcreateparams; // pay attention to ccreatecontext
}
Cwnd * cframewnd: createview (ccreatecontext * pcontext, uint NID)
{
Cwnd * pview = (cwnd *) pcontext-> m_pnewviewclass-> Createobject ();
Pview-> Create (null, null, afx_ws_default_view, crect (0, 0, 0), this, NID, pcontext); // => note that cwnd :: create->: The createmediawex API function generates the wm_create message to create a cview form.
}
// Cview receives the wm_create message and runs oncreate
Int cview: oncreate (lpcreatestruct lpcs)
{
Ccreatecontext * pcontext = (ccreatecontext *) lpcs-> lpcreateparams;
Pcontext-> m_pcurrentdoc-> addview (this); // Add the view to the View list of the document
}
Void cdocument: addview (cview * pview)
{
Pview-> m_pdocument = This; // point to the document of the current view
}
/////////////////////////////////////////
/* 7. Final work */
/////////////////////////////////////////
So far, a complete set of Document/ChildFrame/View structure is generated. The "three groups" belong to the same set of Document templates, if you want to define another set of different document templates, You need to define another set of different "Three-port groups" (ChildFrame can use the same one ). Call AddDocTemplate to add the document template to the document template list of the application. For example:
CMultiDocTemplate * pOtherDocTemplate;
POtherDocTemplate = new CMultiDocTemplate (idr_mythertype,
RUNTIME_CLASS (CMyOtherDoc ),
RUNTIME_CLASS (CChildFrame), // customize the MDI sub-framework
RUNTIME_CLASS (CMyOtherView ));
AddDocTemplate (pOtherDocTemplate );
After the "Three-port group" is generated, the program calls ShowWindow. UpdateWindow displays the main window of the application in front of you.
Note: When you select new in the File menu or click new In the toolbar, the application selects the current default Document Template and dynamically generates the Document/ChildFrame/View "Three-port group" Based on it. The generation process is generally the same as described above.
From
Http://www.chinaitpower.com/A/2001-10-06/879.html