Solved

Create a new Document in MFC MDI application

Posted on 2007-11-15
2
2,212 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

809 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