开发者

Delphi - Capturing the Window/Form which has current focus

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 i开发者_如何学Gos 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.


Is the ActiveMDIChild property what you are looking for?


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜