Solved

catching events from mdi Child window

Posted on 2002-05-27
11
337 Views
Last Modified: 2010-08-05
Hi All,

I have a mdi app with several mdichild forms. On the mdi parent window i have a toolbar with a button that creates the child window and disables the button :

procedure TFrmMain.ToolButton1Click(Sender: TObject);
begin
 FrmCompanySearch := TFrmCompanySearch.Create(Self);
 FrmCompanySearch.Show;
 ToolBar1.Buttons[0].Enabled := false;
end;

The childwindow has his own onclose handler :

procedure TFrmCompanySearch.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
  FrmCompanySearch := nil;
end;

Now the Question : How do i enable the toolbarbutton after the onclose event. So how can i catch the childwindow on close in my mdi parent form !

I hope that its a bit clear,
Thanks in advance,

Yuri
0
Comment
Question by:muldeyur
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7038551
you could send a wm_user+xxx message to your mainform
and handle the message there,
or
you could access directly the mainform

in your child-closemethod

meikl ;-)
0
 

Author Comment

by:muldeyur
ID: 7038607
how do i access the Mainform directly?
do you have a code example?

regards

Yuri
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 7038626
simplified (from head)

mainform:

type tform1 ....

     public
       Procedure Handle_ChildClose(AChild : TForm);
    ....

implementation

procedure tform1.Handle_ChildClose(AChild : TForm);
begin
  if (aform is TFrmCompanySearch) then
    ToolBar1.Buttons[0].Enabled := true;
  //..maybe more childclasses to handle
end;

------------

childform

...

implementation
uses mainformunit; //!include this with your real unit name

....
procedure TFrmCompanySearch.FormClose(Sender: TObject;
 var Action: TCloseAction);
begin
 Action := caFree;
 FrmCompanySearch := nil;
 mainformname.handle_ChildClose(self);
end;

------

hope this helps

meikl ;-)


0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 7038635
oops there is a typo

this line
if (aform is TFrmCompanySearch) then

should be
if (aChild is TFrmCompanySearch) then
   
;-)
   
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7038737
listening
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7038748
nick, the listener ? :-)
0
 
LVL 2

Expert Comment

by:freshman3k
ID: 7040505
hello!

This should help you:

private
 { Private declarations }
 FClientInstance : TFarProc;
 FPrevClientProc : TFarProc;
 procedure ClientWndProc(var aMessage: TMessage);

implementation

procedure TfrmMForm.FormShow(Sender: TObject);
begin
FPrevClientProc := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
FClientInstance := MakeObjectInstance(ClientWndProc);
SetWindowLong(ClientHandle, GWL_WNDPROC, LongInt(FClientInstance));
end;

procedure TfrmMForm.FormClose(Sender: TObject);
begin
SetWindowLong(ClientHandle, GWL_WNDPROC, LongInt(FPrevClientProc));
end;

procedure TfrmMForm.ClientWndProc(var aMessage: TMessage);

procedure DoDefault;
 begin
    with aMessage do
      Result := CallWindowProc(FPrevClientProc,ClientHandle, Msg, wParam,lParam);
 end;

begin
  if aMessage.Msg = WM_MDIDESTROY then begin
     
{aMessage.Child is handle of the Mdi Child being closed}

    if aMessage.Child = FrmCompanySearch.Handle then begin    
        ToolBar1.Buttons[0].Enabled := true;
        end;
   
    if aMessage.Child = OtherMdiChildForm.Handle then
begin  
        ToolBar1.Buttons[1].Enabled := true;          
        end;
   
    DoDefault; {do defualt processing}
   end;
  else
    DoDefault;  {do defualt processing}
  end;
end;


Good Luck! ;-)
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7041013
Don't have time to answer all the time . So I "mark" :P the question and return latter . If is necesary .

So ...

const
  WM_DelChild = WM_User + 300;

type
  TMainForm = class(TForm)
...................................
private
    procedure CreateChild(const Name: string);
    procedure OnDelChild(var msg: TMessage); message WM_DelChild;  
    procedure OnChildDestroy(Sender: TObject);
.................

procedure TMainForm.OnDelChild(var msg: TMessage);
begin
  ShowMessage(' Just destroy me :( ');
end;

procedure TMainForm.OnChildDestroy(Sender: TObject);
begin
  PostMessage(TForm(MainForm).handle, WM_DelChild, 0, Integer(PChar('ChildName')));
end;

procedure TMainForm.CreateChild(const Name: String);
var
  Child: TChildForm;
begin
  Child := TChildForm.Create(Application);
  Child.Caption := Name;
  Child.OnDestroy := OnChildDestroy;
end;

0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7041163
Sorry :

1. Child unit

const
  WM_CloseWin = WM_User + 300;

type
  TChildForm = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  end;

var
  ChildForm: TChildForm;

implementation

{$R *.DFM}

procedure TChildForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
var Buffer: array [0..99] of Char;
    s:string;
begin
  GetWindowText(TForm(Sender).Handle,buffer,100);
  s:=Buffer;
  PostMessage(Application.MainForm.handle, WM_CloseWin, 0, Integer(PChar(s)));
  Action := caFree;
end;

end.

2. Main unit

const
  WM_CloseWin = WM_User + 300;

type
  TMain = class(TForm)
...............
  private
    procedure OnCloseChild(var msg: TMessage); message WM_CloseWin;
    procedure CreateChild(const Name: string);
...............


procedure TMain.OnCloseChild(var msg: TMessage);
begin
  ShowMessage(Strpas(Pointer(msg.lparam)));
end;


0
 

Author Comment

by:muldeyur
ID: 7041170
Wow folks,

I'am going to try this when i get home,
Thanks to all that give advice,
will give the points as soon as i tried it!!

Regards,

Yuri
0
 

Author Comment

by:muldeyur
ID: 7042492
Thanks to all for the advice, but Kretzschmar was the quickest and also exactly what i Needed !

Thanks a lot,

Yuri
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 65
Delphi 2 62
PHP preg_replace code convert to Delphi 14 57
How to make Sign in, using Clientdataset? 1 19
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question