catch/create OnGetFocus/OnLostFocus TCustomControl 事件 Delphi

catch/create OnGetFocus/OnLostFocus events for TCustomControl Delphi

我创建了一个继承自 TCustomControl 的 Delphi 组件。该组件可以从 TWinControl 继承,但我需要 "highlight" 当它获得焦点并在它失去焦点时更改一些属性。 正如 Delphi 文档所说,TCustomControl 没有继承的 OnFocus 事件,所以我需要捕获事件(?)并实现我自己的 OnGetFocus/OnLostFocus 事件处理程序(?)。 当组件 get/lose 成为焦点时,如何捕捉事件?

控件接收或失去输入焦点时触发的事件是 OnEnter and OnExit and are fired from the DoEnter and DoExit 作为组件开发人员应覆盖的方法:

type
  TMyControl = class(TCustomControl)
  protected
    procedure DoEnter; override;
    procedure DoExit; override;
  end;

implementation

{ TMyControl }

procedure TMyControl.DoEnter;
begin
  inherited;
  // the control received the input focus, so do what you need here; note
  // that it's recommended to call inherited inside this method (which as
  // described in the reference should only fire the OnEnter event now)
end;

procedure TMyControl.DoExit;
begin
  inherited;
  // the control has lost the input focus, so do what you need here; note
  // that it's recommended to call inherited inside this method (which as
  // described in the reference should only fire the OnExit event now)
end;