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
- Start Microsoft Visual Studio .NET
- On the Start Page, click New Project (alternatively, on the main menu, you can
click File -> New -> Project...)
- On the New Project dialog box, in the Project Types tree list, click
Visual C++ Projects
- In the Templates list, click Managed C++ Empty Project
- In the Name edit box, replace the <Enter name> content with Multiple
Document Interface
- In the Location combo box, accept the suggestion or type C:\Programs\MSVC.NET
- Click OK
- We will first create a child form
On the main menu, click Project -> Add Class...
- 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
- In Generic C++ Class Wizard, in the Class Name, type COneForm
- In the Base Class, type Form and
press Enter
- 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
- 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);
};
|
- 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)
{
}
|
- Now we will first create the parent form
On the main menu, click Project -> Add Class...
- 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
- In Generic C++ Class Wizard, in the Class Name, type CMainForm
- In the Base Class, type Form and
press Enter
- 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);
};
|
- 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();
}
|
- To create the application, on the main menu, click Project -> Add
New Item...
- 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.
- In the empty file, type the following:
#include "MainForm.h"
int __stdcall WinMain()
{
CMainForm *MF = new CMainForm();
Application::Run(MF);
return 0;
}
|
- Test the application:
|
|