?
Solved

Create a new Document in MFC MDI application

Posted on 2007-11-15
2
Medium Priority
?
2,276 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 2000 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
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.
Suggested Courses

770 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