The basic steps to create a new MDI application with a child window without using a wizard are
- Create a main window form (MDI parent window).
- Create a child window form.
- Have the main window create the child window under user control.
- Write the event handler code to close the child window.
- Create the main menu and commands.
- Add the event handlers for the commands.
- Compile and run the application.
To create the main window form
- Choose FileNewOtherDelphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer is displayed.
- In the Object Inspector, set the FormStyle property to fsMDIForm.
- Enter a more descriptive name such as frMain for the Name property.
- Save the unit file with a more descriptive name, such as uMain.pas (Delphi) or uMain.cpp (C++).
To create a child window
- Choose FileNewForm
- In the Object Inspector, set the FormStyle property to fsMDIChild.
- Enter a more descriptive name such as frChild for the Name property.
- Save the unit file as uChild.pas (Delphi) or uChild.cpp (C++).
To have the main window create the child window
- Choose ProjectOptionsForms. The Project Options dialog box appears.
- Select frChild from Auto-create forms list and click [>] to move it to the Available forms list and click OK.
- Select the frMain form to activate it; then switch to the Code view.
- For Delphi, scroll to the uses section and add uChild to the list. For C++, add #include “uChild.h” to uMain.h.
- For Delphi, scroll to the private declarations section and enter this procedure declaration:
procedure CreateChildForm(const childName: string);
For C++, add the following function declaration to the private: declarations of TfrMain.
void __fastcall CreateChildForm( const AnsiString childName );
- For Delphi, scroll to the implementation section, and enter the code below:
procedure TfrMain.CreateChildForm (const childName: string);
var Child: TfrChild;
begin
Child := TfrChild.Create(Application);
Child.Caption := childName;
end;
For C++, add the following function definition to uMain.cpp:
void __fastcall TfrMain::CreateChildForm( const AnsiString childName )
{
TfrChild *Child = new TfrChild( Application );
Child->Caption = childName;
}
To write the event handler code to close the child window
- If necessary, activate the frMain form; then select the Events tab in the Object Inspector.
- Double-click the OnClose event. The Code Editor displays with the cursor in the TfrMain.FormClose (Delphi) or TfrMain::FormClose (C++) event handler block.
- For Delphi, enter the following code:
For C++, enter the following code:
To create the main menu and commands
- From the Standard page on the Tool Palette, place a TMainMenu component on the main form.
- Double-click the TMainMenu component. The Menu designer (frMain.MainMenu1) displays with the first blank menu item highlighted.
- In the Object Inspector on the Properties tab, enter mnFile for the Name property and &File for the Caption property; then press ENTER. In the Menu designer, File displays as the name of the first menu item.
- In the Menu designer, select File. A blank command field displays in the File group. Select the blank command.
- In the Object Inspector, enter mnNewChild for the Name property and &New child for the Caption property; then press ENTER. In the Menu designer, New child displays as the name of the first file command, and a blank command field displays just beneath New child.
- Select the blank command.
- In the Object Inspector, enter mnCloseAll for the Name property and &Close All for the Caption property; then press ENTER. In the Menu designer, Close All displays as the name of the second file command.
To add event handlers for the New child and Close All commands
- If necessary, open the Menu designer and select New child.
- In the Object Inspector, double-click the OnClick event on the Events tab. The Code Editor displays with the cursor in the TfrMain.mnNewChildClick (Delphi) or TfrMain::mnNewChildClick (C++) event handler block.
- For Delphi, enter the following code:
CreateChildForm('Child '+IntToStr(MDIChildCount+1));
For C++, enter the following code:
CreateChildForm( “Child “ + IntToStr( MDIChildCount + 1 ) );
- In the Menu designer, select Close All.
- In the Object Inspector, double-click the OnClick event on the Events tab. The Code Editor displays with the cursor in the TfrMain.mnCloseAllClick (Delphi) or TfrMain::mnCloseAllClick (C++) event handler block.
- For Delphi, enter the following code:
for i:=0 to MDIChildCount - 1 do
MDIChildren[i].Close;
For C++, enter the following code:
for( int i = 0; i < MDIChildCount; i++ ) {
MDIChildren[i]->Close();
}
- For Delphi, declare the local variable i. The first two lines of the event handler code should appear as shown here when you are done:
procedure TfrMain.mnCloseAllClick(Sender: TObject);
var i: integer;
Note: The event handler minimizes the child window in the main window. To close the child window, you must add an OnClose procedure to the child form (next).
To close the child window
- Activate the child form.
- In the Object Inspector, double-click the OnClose event on the Events tab. The Code Editor displays with the cursor in the TfrChild.FormClose (Delphi) or TfrChild::FormClose (C++) event handler block.
- For Delphi, enter the following statement:
For C++, enter the following statement:
To compile and run the MDI application
- Choose RunRun.
- The application executes, displaying the File command.
- Choose FileNew child one or more times. A child window displays with each New child command.
- Choose FileClose All. The child windows close.