【问题标题】:catch/create OnGetFocus/OnLostFocus events for TCustomControl Delphi为 TCustomControl Delphi 捕获/创建 OnGetFocus/OnLostFocus 事件
【发布时间】:2015-09-15 02:59:09
【问题描述】:

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

【问题讨论】:

  • 覆盖 DoEnterDoExit 方法。
  • 非常感谢@TLama,这正是我要找的东西
  • @TLama:这应该作为答案发布,而不是评论。

标签: delphi events focus components


【解决方案1】:

当控件接收或失去输入焦点时触发的事件是OnEnterOnExit,它们是从您作为组件开发人员应该重写的DoEnterDoExit 方法触发的:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2014-03-18
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多