Solved

catching events from mdi Child window

Posted on 2002-05-27
11
332 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
listening
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
nick, the listener ? :-)
0
 
LVL 2

Expert Comment

by:freshman3k
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to all for the advice, but Kretzschmar was the quickest and also exactly what i Needed !

Thanks a lot,

Yuri
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now