Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC
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.
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 3-Apr-13 1:38am
Comments
Richard MacCutchan at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Vineeth.B at 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 at 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)

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100