Solved

Create a new Document in MFC MDI application

Posted on 2007-11-15
2
2,168 Views
Last Modified: 2013-12-14
Hello,

I have a multiple-document interface (MDI) application written using MFC in Visual C++.NET 2003. The application is intended for Windows 2000 and later only. What I need to do is the following: When the user presses a button on the toolbar, the application show an Open File dialog and lets him select a file, let's say c:\movie.mpeg. Then it creates a new document that's not blank but contains the following code in the application-defined programming language

WIDNOW
AVI "c:\movie.mpeg"
AVIWAIT

My question is: how do I create a new blank document, get a pointer to it and fill in the code? I only found ways to create a new blank document somewhere in doc manager but no way to create and get the pointer.

Thanks a lot
0
Comment
Question by:bovlk
2 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 20289188

BOOL CzzDoc::OnNewDocument()
{
      if (!CDocument::OnNewDocument())
            return FALSE;

      // TODO: add reinitialization code here
      // (SDI documents will reuse this document)

   this;   <------------------------------------ here is a pointer to the new document.

      return TRUE;
}


This function would have been created for you as part of the new project (if you used the wizard to create the project that is)
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 20291162
A new document was created with OnFileNew handler function.

Check your application cpp file. It should have a message map (between BEGIN_MESSAGE_MAP and End_Message_MAP macros) where you should find

BEGIN_MESSAGE_MAP(MyMDIApp, CWinApp)
      ...
      ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
      ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP

The OnFileNew is supposed to create a new file from the scratch and the OnFileOpen should open existing files. So for your example the OnFileOpen should be used. The default CWinApp::OnFileOpen would show the explorer-like FileOpen dialog box where you can select the file. After selecting it calls the CYourDoc::OnOpenDocument() where the 'this' is a pointer to the newly created document. In case you have defined more than one document template in YourApp::InitInstance the default implementation in CWinApp::OnFileOpen would prompt you for the document 'type' if it cannot deduced from the file type. You can add own member functions CYourApp::OnFileNew and CYourApp::OnFileOpen and replace the functions defined in the message map.

Generally, the MFC framework uses a 'document template' to create a 'frame', a 'doc', and a 'view' when either choosing 'File new...' or 'File open... ' from the File menu.  You can use the doc template pointer(s) stored in your app class to create more 'triples' of frame, doc, view without user invention. You would do that by calling the 'CreateNewDocument', 'CreateNewFrame', and 'InitialUpdateFrame' member functions using the appropriate doc template pointer. If documents were created you can retrieve the doc pointers by CDocTemplate::GetFirstDocPosition and GetNextDoc functions.

Regards. Alex

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now