FunctionX Practical Learning Logo

Creating a Multiple Document Interface

 
 

Introduction

A multiple document interface (MDI) is an application that allows the user to open more than one document from the same application without having to purposely launch another instance of the application.

Creating an MDI is a multiple step task but not particularly difficult. For its functionality, an MDI needs two type of containers. The first form has the role of parent for each document that the user will need to open when using the application. Each document will have its own form and is therefore considered as a child of the main or parent form

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Multiple Document Interface
  6. In the Location combo box, accept the suggestion or type C:\Programs\MSVC.NET
  7. Click OK
  8. We will first create a child form
    On the main menu, click Project -> Add Class...
  9. In the Add Class dialog box, in the Categories section, make sure that Visual C++ is selected. In the Templates list, click Generic C++ Class and click Open
  10. In Generic C++ Class Wizard, in the Class Name, type COneForm
  11. In the Base Class, type Form and press Enter
  12. You may/should receive a message box stating that the base class is not found in the project and asking you if you want to continue. Click Yes
  13. To create one child object, change the header file as follows:
     
    #pragma once
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class COneChild : public Form
    {
    public:
    	COneChild(void);
    	~COneChild(void);
    };
  14. To implement the object, change the header file as follows:
     
    #include "onechild.h"
    #using <mscorlib.dll>
    
    COneChild::COneChild(void)
    {
    	this->Text = "Child";
    	this->Size = Drawing::Size(500, 340);
    }
    
    COneChild::~COneChild(void)
    {
    }
  15. Now we will first create the parent form
    On the main menu, click Project -> Add Class...
  16. In the Add Class dialog box, in the Categories section, make sure that Visual C++ is selected. In the Templates list, click Generic C++ Class and click Open
  17. In Generic C++ Class Wizard, in the Class Name, type CMainForm
  18. In the Base Class, type Form and press Enter
  19. To create the parent object, change the header file as follows:
     
    #pragma once
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class CMainForm : public Form
    {
    public:
    	CMainForm(void);
    	~CMainForm(void);
    private:
    	void FormLoad(Object *Sender, EventArgs *Args);
    };
  20. To implement the object, change the header file as follows:
     
    #include "mainform.h"
    #include "OneChild.h"
    #using <mscorlib.dll>
    
    CMainForm::CMainForm(void)
    {
    	this->Text = "Parent";
    	this->IsMdiContainer = true;
    	this->add_Load(new EventHandler(this, FormLoad));
    	this->Size = Drawing::Size(640, 480);
    }
    
    CMainForm::~CMainForm(void)
    {
    }
    
    void CMainForm::FormLoad(Object *Sender, EventArgs *Args)
    {
    	COneChild *OC = new COneChild();
    
    	OC->MdiParent = this;
    	OC->Show();
    }
  21. To create the application, on the main menu, click Project -> Add New Item...
  22. In the Categories tree, make sure that Visual C++ is selected. In the Templates list, click C++ File (.cpp). In the Name box, replace <Enter name> with Main and click Open.
  23. In the empty file, type the following:
     
    #include "MainForm.h"
    
    int __stdcall WinMain()
    {
    	CMainForm *MF = new CMainForm();
    	Application::Run(MF);
    
    	return 0;
    }
  24. Test the application:

Creating Documents

To allow the users to create their own documents, you can use a menu with an item such as New under the File menu.

  1. To create a main menu with an item to create a new document, change the header file of the CMainForm class as follows:
     
    #pragma once
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class CMainForm : public Form
    {
    public:
    	CMainForm(void);
    	~CMainForm(void);
    private:
    	// This is the main menu
    	MainMenu *MMenu;
    	// This method is simply used to create the main menu
    	// It doesn't do anything
    	void CreateTheMenu();
    	// This event fires when the form comes up
    	void FormLoad(Object *Sender, EventArgs *Args);
    	// This is the event that will handle the New menu
    	void OnFileNew(Object *Sender, EventArgs *Args);
    	// This event is for the File -> Exit menu
    	void OnFileExit(Object *Sender, EventArgs *Args);
    };
  2. Implement the source file as follows:
     
    #include "mainform.h"
    #include "OneChild.h"
    #using <mscorlib.dll>
    
    CMainForm::CMainForm(void)
    {
    	this->Text = "Parent";
    	this->IsMdiContainer = true;
    	this->add_Load(new EventHandler(this, FormLoad));
    	this->Size = Drawing::Size(640, 480);
    
    	CreateTheMenu();
    }
    
    CMainForm::~CMainForm(void)
    {
    }
    
    void CMainForm::FormLoad(Object *Sender, EventArgs *Args)
    {
    	// When the application comes up, behave as if the user had clicked File -> New
    	OnFileNew(Sender, Args);
    }
    
    void CMainForm::CreateTheMenu()
    {
    	MMenu = new MainMenu();
    	// Create a new main menu column
    	MenuItem *mnuFile = MMenu->MenuItems->Add("&File");
    	// Add a menu handler to create a new document
    	mnuFile->MenuItems->Add("&New", new EventHandler(this, OnFileNew));
    	// Add a menu handler to close the application
    	mnuFile->MenuItems->Add("E&xit", new EventHandler(this, OnFileExit));
    
    	// After creating the main menu, add it to the form
    	Menu = MMenu;
    }
    
    void CMainForm::OnFileNew(Object *Sender, EventArgs *Args)
    {
    	// Create an instance of the Child
    	COneChild *OC = new COneChild();
    
    	OC->Text = S"Child";
    	OC->MdiParent = this;
    	OC->Show();
    }
    
    void CMainForm::OnFileExit(Object *Sender, EventArgs *Args)
    {
    	// When the user clicks File -> Exit, close the application
    	Close();
    }
  3. Test the application
 

Home Copyright © 2002-2005 FunctionX, Inc.