问题
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.
回答1:
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;
回答2:
Is the ActiveMDIChild property what you are looking for?
来源:https://stackoverflow.com/questions/6495824/delphi-capturing-the-window-form-which-has-current-focus