Dynamically create a template, view, and document object
I don't understand, use this statement: CMYDOC MYDOC or PMYDOC = New CMYDOC can create a document class object well, or create a view class object or frame window object with the same method, why make a dynamic creation concept? Turn over the source code, maybe there will be an answer. When I derive my own class from the document class, frame window, and view class, I usually have to add Declare_DyncReate () in the class, and then add IMPLEMENT_DYNCREATE (). In the previous article I already know what these two macros have brought to me. Now, I will find out what you have exactly what you have come. To simplify problems, let's take a look at how to use the Declare_Dyncreate macro and _AFXDLL is defined after the document, the view, and the framework is dynamically created. Let's take a look at the contents of the CruntimeClass structure. As long as you use the declare_dyncreate () macro in the class, you have a static CRUNTIMECLASS type member variable in your class. The member variable has these members: lpcstr m_lpszclassname: This is the class name of your class, which is an ASCII code character. INT m_nObjectsize: This is your size, indicated by byte. If you have a pointer in the class, the size of the place is not included. This value is set by SIZEOF (the name of your class). UINT M_WSCHEMA: This stuff is something like a version number. When you use Declare_Serial and Implement_Serial, this value is useful, otherwise it can be set to -1. COBJECT * (Pascal * m_pfncreateObject) (): This is a function pointer. It is NULL when _afxdll is not defined. Otherwise it points to CreateObject members in your class. do you remember? After using the declare_dyncreate () macro, you have a member function in your class: CreateObject (). CruntimeClass * (Pascal * m_pfn_getbaseclass (): This is also a function pointer, and the member is only existing after _afxdll is defined. Its value is & class_name :: _ getBaseClas S, that is, you use the address of the member function_getbase class () in your class in your class after the Declare_Dyncreate macro. CruntimeClass * m_pbaseclass: This member is only available when _AFXDLL is not defined. Suppose your base class of your class is CBASE, then this value is Runtime_Class (CBASE). COBJECT * CREATEOBJECT (); Oh, this is not that CreateObject in your class. But seeing the source code, this CreateObject is ultimately called the CR EateObject function in your class. It seems that there is not much content of the CruntimeClass class. All of these members of this static member variable in your class are completely set in Implement_DyncReate () macro. Details can be viewed on the file AFX.H. To clarify the relationship between documents, views, and templates, you will start with the constructor of the document template.
In a single-document program where, InitInstance function there will always have to use a program like this sentence: CSingleDocTemplate * pDocTemplate; pDocTemplate = new CSingleDocTemplate (IDR_MAINFRAME, RUNTIME_CLASS (CMysdiDoc), RUNTIME_CLASS (CMainFrame), // main SDI frame window RUNTIME_CLASS (CMysdiView) ); AddDocTemplate (pDocTemplate); see the source code, CDocTemplate there are several member variables: UINT m_nIDResource; CRuntimeClass * m_pDocClass; CRuntimeClass * m_pFrameClass; CRuntimeClass * m_pViewClass; CSingleDocTemplate class constructor to pass its four parameters were put Enter these member variables. Namely: the UINT m_nIDResource = IDR_MAINFRAME, CRuntimeClass * m_pDocClass = RUNTIME_CLASS (CMysdiDoc) CRuntimeClass * m_pFrameClass = RUNTIME_CLASS (CMainFrame) CRuntimeClass * m_pViewClass = RUNTIME_CLASS (CMysdiView)) that is constructed out of document templates, document by document template objects can tell Class name, frame window, and view class class name, size, you can also access these three class CreateObject () functions. After addDdhemplate (PDOCTemplate), the member M_PDoc Manager in the program class object will point to a CDOCManager object, which has a document template list in the object, which is a two-way linked list, and PDOCTemplate will be added to this list. The names and types of this list are cptrlist m_templatelist; now start tracking the documentation, framework and view class objects of the document, and the creation of the graphics object from the CWINAPP onfilenew function. 1. The creation assumption of the new document class object We are the first run single document program, then the single document program's ONFileNew function creates the program's main frame window, and the frame window of the accommodation document and the view, but also creates document objects and view objects. . CWINApp :: OnFileNew function Simply calls the cdocmanager :: onfilenew function, if there are multiple document templates, the function will prompt to select a template. Finally, use the NULL parameter to call the openDocumentFile function of the CSINGLEDEMPLATE class. This function calls the cdoctemplate :: Cr EateNewDocument () function Create a new document, the function uses the following statement to create a new document object: cdocument * pdocument = (cdocument *) m_pdocclass-> createObject (); cdo ctemplase member m_pdocclass above pass. Let's talk about the dynamic creation of the document object or use the new operator, you can check the ultimate implementation of CreateObject, which is this: {Return New Your document class class name;} 2. The new main frame window Object and the main frame window Create Next CDOCTemplate :: CreateNewDocument () function Create a program owner window window, the single document program is an inclusive documentation and view of the frame window. This process is to call the CDOCTEMPLA TE :: CreateNewDocument () function.