Delphi-捕获当前具有焦点的窗口/窗体 [英] Delphi - Capturing the Window/Form which has current focus

查看:292
本文介绍了Delphi-捕获当前具有焦点的窗口/窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个MDI子级(以及非MDI表单)的MDI应用程序,并且希望跟踪当前处于激活状态并始终具有焦点的表单.当用户在应用程序中从一种形式切换到另一种形式时,我想捕获窗口激活消息,并在后台将全局变量设置为活动形式的属性(此属性是从基类继承的).我最初将代码放在基类的OnActivate事件处理程序中(我的应用程序中的所有表单都使用该代码),但是注意到该事件并不总是引发.有任何想法吗?

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?

我正在使用Delphi 2006 BDS.

I am using Delphi 2006 BDS.

推荐答案

全局Screen变量可跟踪所有形式. Screen.ActiveCustomForm指向具有焦点的窗体,而Screen.OnActiveFormChange是每次焦点更改为另一种窗体时都会触发的事件.您可以在事件处理程序中更新属性:

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;

这篇关于Delphi-捕获当前具有焦点的窗口/窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆