Solved

Create a new Document in MFC MDI application

Posted on 2007-11-15
2
2,224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 187
string initialization in java 11 117
Delphi : could not find program, '...exe' 2 217
How to make the Angular JS application work in JetBrains WebStorm 11 2 106
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
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.

730 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