Solved

Create a new Document in MFC MDI application

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 91
Insert several multiple frames into a single mainframe 3 64
x-bar in Google Sheets 2 54
Capture logon name 13 73
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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

772 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