Show: Delphi C++
Display Preferences

Building a VCL Forms MDI Application Without Using a Wizard

From RAD Studio XE3
Jump to: navigation, search

Go Up to Developing Applications with VCL Components

The basic steps to create a new MDI application with a child window without using a wizard are:

  1. Create a main window form (MDI parent window).
  2. Create a child window form.
  3. Have the main window create the child window under user control.
  4. Write the event handler code to close the child window.
  5. Create the main menu and commands.
  6. Add the event handlers for the commands.
  7. Compile and run the application.

Contents

To create the main window form

  1. Choose File > New > Other > Delphi Projects or C++Builder Projects and double-click the VCL Forms Application icon.The VCL Forms Designer is displayed.
  2. In the Object Inspector, set the FormStyle property to fsMDIForm.
  3. Enter a more descriptive name such as frMain for the Name property.
  4. Save the unit file with a more descriptive name, such as uMain.pas (Delphi) or uMain.cpp (C++).

To create a child window

  1. Choose File > New > Form
  2. In the Object Inspector, set the FormStyle property to fsMDIChild.
  3. Enter a more descriptive name such as frChild for the Name property.
  4. Save the unit file as uChild.pas (Delphi) or uChild.cpp (C++).

To have the main window create the child window

  1. Choose Project > Options > Forms .The Project Options dialog box appears.
  2. Select frChild from Auto-create forms list and click [>] to move it to the Available forms list and click OK.
  3. Select the frMain form to activate it; then switch to the Code view.
  4. For Delphi, scroll to the uses section and add uChild to the list. For C++, add #include "Child.h" to uMain.h.
  5. 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 );
  1. 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

  1. If necessary, activate the frMain form; then select the Events tab in the Object Inspector.
  2. Double-click the OnClose event.The Code Editor displays with the cursor in the TfrMain.FormClose (Delphi) or TfrMain::FormClose (C++) event handler block.
  3. For Delphi, enter the following code:
 Action := caFree;

For C++, enter the following code:

 Action = caFree;

To create the main menu and commands

  1. From the Standard page on the Tool Palette, place a TMainMenu component on the main form.
  2. Double-click the TMainMenu component.The Menu designer (frMain.MainMenu1) displays with the first blank menu item highlighted.
  3. 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.
  4. In the Menu designer, select File.A blank command field displays in the File group. Select the blank command.
  5. 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.
  6. Select the blank command.
  7. 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

  1. If necessary, open the Menu designer and select New child.
  2. 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.
  3. For Delphi, enter the following code:
CreateChildForm('Child '+IntToStr(MDIChildCount+1));

For C++, enter the following code:

CreateChildForm( "Child " + IntToStr( MDIChildCount + 1 ) );
  1. In the Menu designer, select Close All.
  2. 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.
  3. 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();
}
  1. 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

  1. Activate the child form.
  2. 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.
  3. For Delphi, enter the following statement:
Action := caFree;
For C++, enter the following statement:
Action = caFree;

To compile and run the MDI application

  1. Choose Run > Run .
  2. The application executes, displaying the File command.
  3. Choose File > New child one or more times.A child window displays with each New child command.
  4. Choose File > Close All .The child windows close.

See Also

Personal tools
Previous Versions
In other languages