Tuesday, October 6, 2009

MDI Application Tutorial

How about creating a MDI(Multiple Document Interface) application?
For this you need to
- start Delphi
- create a new VCL forms application
- change main form's FormStyle property to fsMDIForm
- create a new form via main menu File -> New -> Form
- go to main menu Project -> View Source
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2); (* remove this line! or comment *)
  Application.Run;
end.
it should look like this after modification
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
we don't want form2 to be created at startup.
Now switch to form2 and change FormStyle to fmMDIChild, go to Events in object inspector double click the OnClose property and write Action := caFree; so that the form will be freed from memory when it's closed
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;
now add a TMainMenu component from Standard tab, add a File menu item and a New Form(or whatever you want to call it) menu item to File and on New Form's event add this code
procedure TForm1.NewForm1Click(Sender: TObject);
var
  NewMDIChild: TForm2;
begin
  NewMDIChild := TForm2.Create(Application);
end;
all you have to do now is to hit F9 to run the application and have fun with it, you can download source code of this tutorial fallowing this link.

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)