Customization of OnfileNew ()
1, question
In applications that are automatically created using MFC, provide better support for new and open openings of the document. But when our application requires a variety of document types (note is not a multi-document) - only the default (very simple) of the MFC when we want to create a new document (also very simple) CNewTypedLG dialogue Select the type of document we want to create in the box. The default CNewTypedLG Chinese document template name is only 6 characters, and the information given is very small. It is very difficult to choose to general users (if your document has the same top 6 characters, then you are likely Even I don't know which one you should choose.
2, the principle of problem
The relationship between application classes and documents, graph box, and document view is shown in the following figure:
The process of creating a new document is:
CWinAPP :: OnFileNew () → CDocManager :: OnFileNew () → CDocManager :: OpenDocumentFile () → CDocTemplate :: OpenDocumentFile () (which is a pure virtual function, implemented in CMultiDocTemplate / CSingleDocTemplate, where calls CreateNewDocument () and CreateNewFrame () cdocument :: OnNewDocument ()).
3, solve the problem
// Don't say, message mapping
Begin_Message_Map (CMY_MULTIDOCAPP, CWINAPP)
// {{AFX_MSG_MAP (CMY_MULTIDOCAPP)
ON_COMMAND (ID_FILE_NEW_TYPE1, ONFILENEWTYPE1)
//}} AFX_MSG_MAP
END_MESSAGE_MAP ()
/ / There should be no problem, add a document template
{// Add Second Document Template
CMULTIDEMPLATE * PDOCTEMPLATE;
PDOCTEMPLATE = New CMULTIDOCTEMPLATE
IDR_ISE1TYPE,
Runtime_class (CISE_MULTIDOC2),
Runtime_class (cchildframe), // Custom MDI Child Frame
Runtime_class (CISE_MULTIVIEW2));
AddDDOCTemplate (pdoctemplate);
}
// This is the document that creates the first document template.
Void CMY_MULTIDOCAPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP: ONFILENEWTYPE1 ()
{
// Todo: add your command handler code here
// Get a list of document templates
Position POS = this-> getfirstdoCtemplatePosition ();
// Get a document template, I will take the first, you can go to any template
CDOCTemplate * pdoctempl = this-> GetNextDocTemplate (POS);
// Create a new document using the document template
PDOCTEMPL-> OpenDocumentFile (NULL);
}
Of course, this is not complete. If so, when we run the program, the main frame has not been displayed, and the CNEWTYPEDLG dialog we want to avoid will "very timely". So we have to join in the BOOL C **** app :: InitInstance () function:
IF (cmdinfo.m_nshellcommand == ccommandlineinfo :: filenew)
CMDINFO.M_NSHELLCOMMAND = ccommandlineinfo :: filenothing; not telling the program, do not appear filenew dialogs at the beginning.