Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Using MDI technology while TMDIForm is not the mainform of an applicationGo to Uros Gaber's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 5.x (or higher)
Category:
GUI
Skill Level:
Scoring:
Last Update:
07/12/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet MDI mainform application TMDIForm TMDIChild
Times Scored:
5
Visits:
4362
Uploader: Uros Gaber
Company: PowerCom d.o.o.
Reference: PowerCom Internet
 
Question/Problem/Abstract:
How to use MDI technology if you don't want to use TMDIForm as your mainform of the application?
You want to use TMDIForm that you open/create somewhere during runtime and is not the mainform of your application, but Delphi doesn't allow creating TMDIChild if the TMDIForm is not the mainform of your application.
Answer:



When I wanted to use TMDIForm that wasn't the mainform of the project and then create TMDIChild in that form, I got an error. Delphi didn't allow creation of TMDIChild as long as the mainform wasn't the TMDIForm. So I tweaked the FORMS.PAS a little bit.

1st. You have to add a global variable in Forms.PAS.

----- snip -----
interface

....

var uMDIForm : TForm;

implementation

...
----- snip -----

2nd. You have to change CreateWindowHandle and DestroyWindowHandle procedures a little bit.
Here are the code posts of the changed procedures.

----- snip -----
procedure TCustomForm.CreateWindowHandle(const Params: TCreateParams);
var
  CreateStruct: TMDICreateStruct;
begin
  if (FormStyle = fsMDIChild) and not (csDesigning in ComponentState) then
  begin
(* add the check if the uMDIForm is set and if it is TMDIForm
   if it is not, then raise an exception *)
    if ((uMDIForm = nil) or (uMDIForm.ClientHandle = 0)) and
       ((Application.MainForm = nil) or (Application.MainForm.ClientHandle = 0)) then
      raise EInvalidOperation.Create(SNoMDIForm);
    with CreateStruct do
    begin
      szClass := Params.WinClassName;
      szTitle := Params.Caption;
      hOwner := HInstance;
      X := Params.X;
      Y := Params.Y;
      cX := Params.Width;
      cY := Params.Height;
      style := Params.Style;
      lParam := Longint(Params.Param);
    end;
(* check if uMDIForm is set if not then use the MAINFORM of the application
   as TMDIForm *)
    if uMDIForm = nil then
      WindowHandle := SendMessage(Application.MainForm.ClientHandle,
                                  WM_MDICREATE, 0, Longint(@CreateStruct))
(* else use the form specified in uMDIForm *)
    else
      WindowHandle := SendMessage(uMDIForm.ClientHandle,
                                  WM_MDICREATE, 0, Longint(@CreateStruct));
    Include(FFormState, fsCreatedMDIChild);
  end else
  begin
    inherited CreateWindowHandle(Params);
    Exclude(FFormState, fsCreatedMDIChild);
  end;
end;

procedure TCustomForm.DestroyWindowHandle;
begin
  if fsCreatedMDIChild in FFormState then
  begin
(* again check if uMDIForm is not set and if then destroy TMDIChild from
   mainform *)
    if uMDIForm=nil then
      SendMessage(Application.MainForm.ClientHandle, WM_MDIDESTROY, Handle, 0)
    else
(* else destroy the TMDIChild in uMDIForm *)
      SendMessage(uMDIForm.ClientHandle, WM_MDIDESTROY, Handle, 0);
  end else
    inherited DestroyWindowHandle;
  FClientHandle := 0;
end;
----- snip -----

Now before you create any TMDIChild forms you have to set uMDIForm variable to that form that you have set as TMDIForm.

Hope this is usefull to anyone.

If someone has any idea how this could be done by passing a new variable to TApplication or something similar, please contact me.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Another way
    Yoav Abrahami (Jul 16 2002 3:50PM)

You can get the same affect without changing the forms.pas unit - using another method of hacking...

var
  main:TSomeForm;
  SaveFrm:TForm;
begin
  main:=TSomeForm.Create(Application);
  with main do
  try
    SaveFrm:=Application.MainForm;
    TForm((@Application.MainForm)^) := main ;
    try
      ShowModal;
    finally
      TForm((@Application.MainForm)^) := SaveFrm ;
    end;
  finally
    Free;
  end;
end

The way this hack works is by changing the readonly Application.MainForm property. This property is used by the forms unit to create the MDI parent - child relation.
Basically, I do the same thing as you, only the details are different.
Respond

RE: Another way
Robert Shanbaum (Jul 16 2002 5:15PM)

Just curious - isn't

TForm(Application.MainForm)

the same thing as

TForm((@Application.MainForm)^)

?


Respond

RE: RE: Another way
Ken Wilcox (Jul 17 2002 5:23PM)

Looks like they'd be the same but I think you just need to do it this way to fake out Delphi since Application.MainForm cannot be assigned to since it is a read only property.


Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Maes
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)