1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Your First MDI Delphi Project
Learn how to create a powerful "multiple document interface" application using Delphi.
 More of this Feature
• Part 1: New MDI Project
• Part 2: MDI Graphics Viewer

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
 Related Resources
• A Beginner's Guide to Delphi Programming.TOC

• Free Source Delphi Projects
• Icons and Glyphs
• Birth, life and death of a Delphi form
• Working with Delphi Forms, Dialogs, MDI, SDI
• Scaling forms on different screen resolutions
• Handling picture file formats from 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.

To start working with MDI interfaces using Delphi, first read the "MDI Development in Delphi" article. Inside the article you'll first find the definition of an MDI application. The article then describes constructing multiple document interface application with Delphi. It also provides a discussion on MDI parent/child relationship, menus and some most important MDI parent form properties.

When you a ready, you can proceed to "Creating a multiple document interface graphic file viewer". This article extends the topics described in the first part - letting you expand your MDI application with some real code to open and display most commonly used graphic files (pictures).

Extending your MDI development knowledge
Ok. By now (I suppose you have read the above two articles!) you should be familiar with the key topics involved in developing MDI applications using Delphi.
Another task, you might be interested in, is how to store an MDI child in a DLL.

Now, I'll provide two more code samples you could find useful in an MDI application.

• Create New or Show Already Created Child Form?
Assume, for the minute, that you have an MDI application with more than one type of child forms. What I mean, is that you can have your Delphi MDI parent form host one child form designed to be able to display graphic files (TGraphicChildForm) and another form (TMemoChildForm) designed to display some text inside, for example, a Memo component. Now, think of a situation when you have more than a dozen of various types of MDI child forms.
What you might want to have in your application, is the ability to decide whether a new instance of the specified child form is to be created or the previous instance should be brought to front - if one exists.

The following code snippet solves the above "problem":

procedure TMainForm.CreateMDIChild(frm : TFormClass);
 sMDIChildFormCreated = 
  'Form "%s" already created. Select OK to 
  switch to it, Yes to create a new form instance.';
  for i:= 0 to MainForm.MDIChildCount-1 do
    if MainForm.MDIChildren[i] is frm then
      f:=MainForm.MDIChildren[i] as frm ;

  if not bCreated then
    case MessageDlg(Format(sMDIChildFormCreated,
                  [f.caption]), mtConfirmation, 
                  [mbOk, mbYes, mbCancel],0) of
     mrOK: f.Show;
     mrYes: frm.Create(Application);
     mrCancel: //do notingg
    end; //case

To call the above procedure you can use the following code:


Let's see what the above code does... First, you send to the CreateMDIChild procedure the name of the child form class as the parameter. Then, in the CreateMDIChild procedure, the code looks for an already created MDI child form of the provided class, if the one is not found, one gets created. If, on the other hand, the instance (at least one) of the child form is already created, the message dialog asks the user whether to create a new instance or to bring to front that previously created form.
Again, more or less a not_for_beginner type of code, but you should be able to understand the way it works.

• Hide MDI child form when minimized
As you know by now, when you try to close an MDI child form, the form only gets minimized. You are however, able to set the Action parameter of the OnClose event method to caFree to really close it; or even set to caNone - to "mark" that the form cannot be closed.

procedure TMDIChild.FormClose
(Sender: TObject; var Action: TCloseAction);

However, as stated, the default behavior for MDI child form OnClose event is to become minimized. Now, if you want to hide the form when it gets minimized you can use the next trick:

procedure TMDIChild.WMSize(var M:TWMSIZE); 
 if M.SizeType=Size_Minimized then 

Note 1: You'll need to add the procedure header to the private part of the form type declaration:

  procedure WMSize(var M : TWMSIZE); Message WM_Size;

Note 2: Even though this procedure relies on Windows Messages and handling them, don't be afraid to use it. The truth is that you are *beginner* developer, but you are trying to become an expert ;)

Some exercises for you...
As in all the chapters of this course, I'll provide an idea for you to try to use the gathered knowledge in a real situation. This is what you could do: build an MDI application and make your Delphi TicTacToe game from the previous chapter to be the child form of the created MDI project.

   To the next chapter: A Beginner's Guide to Delphi Programming
This is the end of the eleventh chapter, in the next chapter, we'll deal with ... well I don't know yet (probably with the Human vs. Computer extension to the Tic Tac Toe Game).

If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

A Beginner's Guide to Delphi Programming: Next Chapter >>
>> Chapter 12: Win a copy of Mastering Delphi 7

Related Video
iDVD New Project

©2016 About.com. All rights reserved.