2
votes

I have an MDI application with numerous MDI Children (and also non MDI forms) and would like to track which form is currently activate and has the focus at all times. When a user switches from one form to another within the application I would like to trap the window activation message and in the background set a global variable to a property of the form which is active (this property is inherited from a base class). I originally put code in the OnActivate event handler for the base class (which all the forms in my application use) but have noticed that this event does not always get raised. Any ideas?

I am using Delphi 2006 BDS.

2
What exactly do you mean by focus. If you mean input focus then you can call Windows.GetFocus, pass the result to FindControl and finally walk up the Parent hierarchy until you reach a form.David Heffernan
@David: I'd say until you find the top of the hierarchy (Parent = nil). Forms can be embedded within frames within forms within frames within forms...Marjan Venema
another point, dropping down a menu should not change input focus or active formDavid Heffernan
Thanks for your input guys, I have edited my original question to hopefully make it less vague...PDM

2 Answers

3
votes

The global Screen variable keeps track of all forms. Screen.ActiveCustomForm points to the form which has the focus and Screen.OnActiveFormChange is the event that is fired every time the focus changes to another form. You could update your property in its event handler:

type
  TMainForm = class(TForm)
    ...
  private
    procedure ActiveFormChanged(Sender: TObject);
  end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  Screen.OnActiveFormChange := ActiveFormChanged;
end;

procedure TMainForm.ActiveFormChanged(Sender: TObject);
begin
  { Do what you want to do }
end;
5
votes

Is the ActiveMDIChild property what you are looking for?