Delphi - Capturing the Window/Form which has current focus

元气小坏坏 提交于 2019-12-11 03:15:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!