如何让我的自定义控件在他的表单或应用程序收到并失去焦点时得到通知?
How to make my custom control be notified when his form or application receives and loses focus?
我希望我的控件仅在其父窗体(不是面板或其他东西,只是此控件的主要窗体)接收和失去焦点时接收不同的通知。无论焦点是从另一种形式的应用程序切换还是在我的应用程序和其他应用程序之间切换,这两种情况都必须收到。可能吗?我想在他的表单未激活时暂停控件的某些更新,并在激活表单时恢复更新。
编辑:换句话说,控件必须捕获 (TForm.OnActivate
+ TApplication.OnActivate
) 和 (TForm.OnDeactivate
+ TApplication.OnDeactivate
)
Edit2:如果两者都不可能,至少我可以让控件捕获来自 TApplication 的事件。它比来自 TForm 的更重要。
I want to suspend some updates of the control when his form is not active and resume the updates when the form is activated.
如果这些更新是连续完成的,或者是由计时器或操作触发的,那么您可以完成:
type
TMyControl = class(TControl)
private
procedure PerformUpdate;
end;
procedure TMyControl.PerformUpdate;
begin
if Application.Active and HasParent and GetParentForm(Self).Active then
//...
else
//...
end;
...at least if I can make the control catch the events from the application
使用 TApplicationEvents
组件捕捉 TApplication.OnActivate
和 TApplication.OnDeactivate
非常容易:
uses
Vcl.AppEvnts;
type
TMyControl = class(TControl)
private
FActive: Boolean;
FAppEvents: TApplicationEvents;
procedure ApplicationActiveChanged(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
end;
procedure TMyControl.ApplicationActiveChanged(Sender: TObject);
begin
FActive := Application.Active;
end;
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAppEvents := TApplicationEvents.Create(Self);
FAppEvents.OnActivate := ApplicationActiveChanged;
FAppEvents.OnDeactivate := ApplicationActiveChanged;
end;
...it's more important than those from the form
可以在 Application.OnIdle
中捕获(取消)激活育儿表格。所有这些结合起来可能会导致这样的结果:
type
TMyControl = class(TControl)
private
FActive: Boolean;
FAppEvents: TApplicationEvents;
FParentForm: TCustomForm;
procedure ApplicationActiveChanged(Sender: TObject);
procedure ApplicationIdle(Sender: TObject; var Done: Boolean);
procedure UpdateActive;
protected
procedure SetParent(AParent: TWinControl); override;
public
constructor Create(AOwner: TComponent); override;
end;
procedure TMyControl.ApplicationActiveChanged(Sender: TObject);
begin
UpdateActive;
end;
procedure TMyControl.ApplicationIdle(Sender: TObject; var Done: Boolean);
begin
UpdateActive;
Done := True;
end;
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAppEvents := TApplicationEvents.Create(Self);
FAppEvents.OnActivate := ApplicationActiveChanged;
FAppEvents.OnDeactivate := ApplicationActiveChanged;
end;
procedure TMyControl.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
FParentForm := GetParentForm(Self);
end;
procedure TMyControl.UpdateActive;
var
SaveActive: Boolean;
begin
SaveActive := FActive;
FActive := Application.Active and (FParentForm <> nil) and FParentForm.Active;
if Application.Active then
FAppEvents.OnIdle := ApplicationIdle
else
FAppEvents.OnIdle := nil;
if FActive <> SaveActive then
Invalidate;
end;
因为使用 Application.OnIdle
是一种非常严格的方法,所以像我上面那样通过只在必要时分配它来避免它的使用,并通过缓存函数结果来加速它的实现,比如 GetParentForm
.
我希望我的控件仅在其父窗体(不是面板或其他东西,只是此控件的主要窗体)接收和失去焦点时接收不同的通知。无论焦点是从另一种形式的应用程序切换还是在我的应用程序和其他应用程序之间切换,这两种情况都必须收到。可能吗?我想在他的表单未激活时暂停控件的某些更新,并在激活表单时恢复更新。
编辑:换句话说,控件必须捕获 (TForm.OnActivate
+ TApplication.OnActivate
) 和 (TForm.OnDeactivate
+ TApplication.OnDeactivate
)
Edit2:如果两者都不可能,至少我可以让控件捕获来自 TApplication 的事件。它比来自 TForm 的更重要。
I want to suspend some updates of the control when his form is not active and resume the updates when the form is activated.
如果这些更新是连续完成的,或者是由计时器或操作触发的,那么您可以完成:
type
TMyControl = class(TControl)
private
procedure PerformUpdate;
end;
procedure TMyControl.PerformUpdate;
begin
if Application.Active and HasParent and GetParentForm(Self).Active then
//...
else
//...
end;
...at least if I can make the control catch the events from the application
使用 TApplicationEvents
组件捕捉 TApplication.OnActivate
和 TApplication.OnDeactivate
非常容易:
uses
Vcl.AppEvnts;
type
TMyControl = class(TControl)
private
FActive: Boolean;
FAppEvents: TApplicationEvents;
procedure ApplicationActiveChanged(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
end;
procedure TMyControl.ApplicationActiveChanged(Sender: TObject);
begin
FActive := Application.Active;
end;
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAppEvents := TApplicationEvents.Create(Self);
FAppEvents.OnActivate := ApplicationActiveChanged;
FAppEvents.OnDeactivate := ApplicationActiveChanged;
end;
...it's more important than those from the form
可以在 Application.OnIdle
中捕获(取消)激活育儿表格。所有这些结合起来可能会导致这样的结果:
type
TMyControl = class(TControl)
private
FActive: Boolean;
FAppEvents: TApplicationEvents;
FParentForm: TCustomForm;
procedure ApplicationActiveChanged(Sender: TObject);
procedure ApplicationIdle(Sender: TObject; var Done: Boolean);
procedure UpdateActive;
protected
procedure SetParent(AParent: TWinControl); override;
public
constructor Create(AOwner: TComponent); override;
end;
procedure TMyControl.ApplicationActiveChanged(Sender: TObject);
begin
UpdateActive;
end;
procedure TMyControl.ApplicationIdle(Sender: TObject; var Done: Boolean);
begin
UpdateActive;
Done := True;
end;
constructor TMyControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAppEvents := TApplicationEvents.Create(Self);
FAppEvents.OnActivate := ApplicationActiveChanged;
FAppEvents.OnDeactivate := ApplicationActiveChanged;
end;
procedure TMyControl.SetParent(AParent: TWinControl);
begin
inherited SetParent(AParent);
FParentForm := GetParentForm(Self);
end;
procedure TMyControl.UpdateActive;
var
SaveActive: Boolean;
begin
SaveActive := FActive;
FActive := Application.Active and (FParentForm <> nil) and FParentForm.Active;
if Application.Active then
FAppEvents.OnIdle := ApplicationIdle
else
FAppEvents.OnIdle := nil;
if FActive <> SaveActive then
Invalidate;
end;
因为使用 Application.OnIdle
是一种非常严格的方法,所以像我上面那样通过只在必要时分配它来避免它的使用,并通过缓存函数结果来加速它的实现,比如 GetParentForm
.