_I_Play_Chess
11-08-2003, 09:30 AM
Your First MDI Delphi Project
-----------------------------------
Learn how to create a powerful "multiple document interface" application using Delphi.
Welcome to the eleventh chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
Learn how to create a "multiple document interface" application using Delphi.
It's time for your second Delphi project! This time we'll explore building MDI applications using Delphi. After you have created your first Delphi game, you could start developing your own set of Delphi games... Then, why not create one "big" Delphi application to host all your games!
Multiple Document Interface Applications
In simple words, in an MDI application, more than one document or child window can be opened within a single parent window. This is a common situation in applications such as spreadsheets or word processors - one window, usually called the MDI parent or MDI container, contains many other windows, usually called child forms.
In our scenario, MDI parent form would be the host to all your games developed as single form (window) MDI child forms.
What is MDI?
MDI stands for multiple document interface. In an MDI application, more than one document or child window can be opened within a single parent window. This is common in applications such as spreadsheets or word processors - one window, usually called the MDI parent or MDI container, contains many other windows, usually called child forms.
Every MDI application usually has three basic parts:
One (and only one) MDI container (parent) form,
One or more (usually more) MDI child forms,
The MDI main menu.
1- MDI "mother"
----------------------
In a MDI application we can have only one MDI container form to a project, and that form will, mostly, be the start up form.
To create the main window for an MDI application, follow these steps:
Start Delphi and select File | New Application... Delphi will create a new project with one form called form1, by default.
Assign a Name property such as frMain to the form.
Set the FormStyle property to fsMDIform.
Save this project (name the project as you like, e.g. prMDIExample), along with uMain.pas in a newly created folder.
As we can see, to make an MDI container form, we set the FormStyle property of the main form (i.e. any form that we want to be the MDI container) to be fsMDIform. Only one form per application can be fsMDIform.
2- MDI "children"
-----------------------
Every MDI "mother" needs at least one child. MDI child forms are just simple forms, but unlike windows as an AboutBox, MDI child windows are restricted to appearing inside the "mothers" window boundaries. Moreover, if the child form is minimized, its icon appears inside the MDI parent form, rather than in the Windows desktop ( only the parent window's icon appears in the task bar).
Now, we'll create some additional forms - MDI child forms, precisely. Simply choose File | New Form. This will create a new form object called form1, by default. With the Object Inspector change form1's Name property to frChild, and more important set FormStyle property to fsMDIChild. Save this form with it's associated unit as uchild.pas. Note: we can also turn an existing form into an MDI child form by adjusting this property.
Your application can include many MDI child forms of similar or different types.
Note: MDI application can also include standard, non-MDI forms that are not contained in the MDI form. A typical use of a standard form in an MDI application is to display a modal dialog box (such as about box, or custom file handling dialog).
As we can see, at design time both parent and child form look the same - it's hard to tell which one is "the boss".
A child widow in an MDI applications is like any other "normal" window in a non MDI (that is: SDI) application. Child forms can contain any components like Grids, Memos and Pictures. Traditionally, objects like status bars and toolbars usually appear in the MDI parent window.
Auto create -> Available
Next we'll make some adjustments to the project. Select Project | Options, this will open the Project Options dialog. Select the frChild from the left pane ("Auto-create forms"), and move it to the right one (Available forms). The right pane lists those forms that are used by your application but are not automatically created. In a MDI application, by default, all child forms are automatically created and are displayed inside their container. There will be nothing wrong if we don't perform this adjustment, however, standard Windows behavior is that MDI applications create their child window under program (user) control.
Create and show...
Once we have set that the Child form is not automatically created, we'll need some code that creates (one) instance of a frChild form object. The following CreateChildForm function needs to be placed inside the main (MDI parent) form's unit (along with the header in the interface's private part):
___________________
uses uchild;
...
procedure TfrMain.CreateChildForm
(const childName : string);
var Child: TfrChild;
begin
Child := TfrChild.Create(Application);
Child.Caption := childName;
end;
______________________
Note 1: For now, it's enough to know that this code will create one child form with childName caption.
Note 2: Don't forget that "uses uchild" part.
Close don't minimize!
Closing the child window in a MDI application only minimizes it in parent's client area. Therefore we have to provide an OnClose procedure, and set the Action parameter to caFree:
-------------------------------
procedure TfrChild.FormClose
(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
-------------------------------
Note: If a form is an MDI child form, and its BorderIcons property is biMinimize (default), then the default Action is caMinimize. If a MDI child form does not have these settings, the default Action is caNone, meaning that nothing happens when the user attempts to close the form.
3- MDI parent menu
-------------------------
Every MDI application should have a main menu with, if nothing more, window arrange/cascade.. options. As we have previously moved the child form ,from Auto-create forms to Available forms, we'll need some code that (menu item) will create child forms.
If you know how to work with TMainMenu component, than just drop one on the frMain (MDI parent) and let the menus look like:
-----------------------------------
Learn how to create a powerful "multiple document interface" application using Delphi.
Welcome to the eleventh chapter of the FREE online programming course:
A Beginner’s Guide to Delphi Programming.
Learn how to create a "multiple document interface" application using Delphi.
It's time for your second Delphi project! This time we'll explore building MDI applications using Delphi. After you have created your first Delphi game, you could start developing your own set of Delphi games... Then, why not create one "big" Delphi application to host all your games!
Multiple Document Interface Applications
In simple words, in an MDI application, more than one document or child window can be opened within a single parent window. This is a common situation in applications such as spreadsheets or word processors - one window, usually called the MDI parent or MDI container, contains many other windows, usually called child forms.
In our scenario, MDI parent form would be the host to all your games developed as single form (window) MDI child forms.
What is MDI?
MDI stands for multiple document interface. In an MDI application, more than one document or child window can be opened within a single parent window. This is common in applications such as spreadsheets or word processors - one window, usually called the MDI parent or MDI container, contains many other windows, usually called child forms.
Every MDI application usually has three basic parts:
One (and only one) MDI container (parent) form,
One or more (usually more) MDI child forms,
The MDI main menu.
1- MDI "mother"
----------------------
In a MDI application we can have only one MDI container form to a project, and that form will, mostly, be the start up form.
To create the main window for an MDI application, follow these steps:
Start Delphi and select File | New Application... Delphi will create a new project with one form called form1, by default.
Assign a Name property such as frMain to the form.
Set the FormStyle property to fsMDIform.
Save this project (name the project as you like, e.g. prMDIExample), along with uMain.pas in a newly created folder.
As we can see, to make an MDI container form, we set the FormStyle property of the main form (i.e. any form that we want to be the MDI container) to be fsMDIform. Only one form per application can be fsMDIform.
2- MDI "children"
-----------------------
Every MDI "mother" needs at least one child. MDI child forms are just simple forms, but unlike windows as an AboutBox, MDI child windows are restricted to appearing inside the "mothers" window boundaries. Moreover, if the child form is minimized, its icon appears inside the MDI parent form, rather than in the Windows desktop ( only the parent window's icon appears in the task bar).
Now, we'll create some additional forms - MDI child forms, precisely. Simply choose File | New Form. This will create a new form object called form1, by default. With the Object Inspector change form1's Name property to frChild, and more important set FormStyle property to fsMDIChild. Save this form with it's associated unit as uchild.pas. Note: we can also turn an existing form into an MDI child form by adjusting this property.
Your application can include many MDI child forms of similar or different types.
Note: MDI application can also include standard, non-MDI forms that are not contained in the MDI form. A typical use of a standard form in an MDI application is to display a modal dialog box (such as about box, or custom file handling dialog).
As we can see, at design time both parent and child form look the same - it's hard to tell which one is "the boss".
A child widow in an MDI applications is like any other "normal" window in a non MDI (that is: SDI) application. Child forms can contain any components like Grids, Memos and Pictures. Traditionally, objects like status bars and toolbars usually appear in the MDI parent window.
Auto create -> Available
Next we'll make some adjustments to the project. Select Project | Options, this will open the Project Options dialog. Select the frChild from the left pane ("Auto-create forms"), and move it to the right one (Available forms). The right pane lists those forms that are used by your application but are not automatically created. In a MDI application, by default, all child forms are automatically created and are displayed inside their container. There will be nothing wrong if we don't perform this adjustment, however, standard Windows behavior is that MDI applications create their child window under program (user) control.
Create and show...
Once we have set that the Child form is not automatically created, we'll need some code that creates (one) instance of a frChild form object. The following CreateChildForm function needs to be placed inside the main (MDI parent) form's unit (along with the header in the interface's private part):
___________________
uses uchild;
...
procedure TfrMain.CreateChildForm
(const childName : string);
var Child: TfrChild;
begin
Child := TfrChild.Create(Application);
Child.Caption := childName;
end;
______________________
Note 1: For now, it's enough to know that this code will create one child form with childName caption.
Note 2: Don't forget that "uses uchild" part.
Close don't minimize!
Closing the child window in a MDI application only minimizes it in parent's client area. Therefore we have to provide an OnClose procedure, and set the Action parameter to caFree:
-------------------------------
procedure TfrChild.FormClose
(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
-------------------------------
Note: If a form is an MDI child form, and its BorderIcons property is biMinimize (default), then the default Action is caMinimize. If a MDI child form does not have these settings, the default Action is caNone, meaning that nothing happens when the user attempts to close the form.
3- MDI parent menu
-------------------------
Every MDI application should have a main menu with, if nothing more, window arrange/cascade.. options. As we have previously moved the child form ,from Auto-create forms to Available forms, we'll need some code that (menu item) will create child forms.
If you know how to work with TMainMenu component, than just drop one on the frMain (MDI parent) and let the menus look like: