Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to create a SDI application with dynamic views.

I found that normally the view is created inside the function InitInstance() of application class using the code below.
C++
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
	IDR_MAINFRAME,
	RUNTIME_CLASS(CMyAppDoc),
	RUNTIME_CLASS(CMyMainFrame),       // main SDI frame window
	RUNTIME_CLASS(CMyAppView));
if (!pDocTemplate)
	return FALSE;
AddDocTemplate(pDocTemplate);

if (!ProcessShellCommand(cmdInfo))
	return FALSE;


I want to create the view based on the user input...for eg, on selecting an item in combobox.

How should i create a view outside the InitInstance() function???

Please help..
Posted
Comments
Richard MacCutchan 3-Apr-13 7:06am    
You can move this code elsewhere so it becomes dynamic. If memory serves there is a sample somewhere on MSDN that shows how to do it.

1 solution

The view is not created here in InitInstance(). The view will be created by the CMyAppDoc class when a new (empty) document is created or an exiting one is opened. In your code this is initiated by calling ProcessShellCommands().

If you need different kinds of views, you can add multiple templates in InitInstance() which must be of type CMultiDocTemplate and use one of them to create the frame window (which is the parent window of the view) from within your CMyAppDoc class.

While your application is then still SDI, this requires some work in your CMyAppDoc class. To create a frame (e.g. from within OnNewDocument() and OnOpenDocument() of your document class), call the CreateNewFrame() and InitialUpdateFrame() member functions of the selected CMultiDocTemplate passing the pointer to your document class.
 
Share this answer
 
Comments
Vineeth.B 3-Apr-13 8:09am    
Thanks for the solution.
Let me tell you onething. there will be only one view will be active at a time. So i don't think that CMultiDocTemplate need to be used. My application is SDI. Also, the main window should be created without a view initially. When the user select one from the combobox, an apprpriate view must be created. That's my requirement.
Jochen Arndt 3-Apr-13 8:26am    
CMultiDocTemplate must be used when using different templates for different views even when only one view is used at time. SDI means single document. It is still SDI when using different types of views or even multiple views for one document (e.g. multiple tables from one database document).

To avoid creation of an empty document at startup, just remove the call to ProcessShellCommands() or call it only when a file has been passed on the command line (cmdInfo.m_nShellCommand == CCommandLineInfo::FileOpen).

Instead show a dialog box to select the type of view. Select the template according to the type and store the pointer in a member variable that can be accessed from your document class. Then call CWinApp::OpenDocumentFile(). This will call OnOpenDocument() (or OnNewDocument() when the file name is NULL) of your document class. There get the pointer to the template and create the frame which contains the view.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900