争怎路由网/网站教程/内容

给控件添加OnMouseLeave事件

网站教程2024-07-16 阅读
需要在鼠标离开的时候得到通知吗?看看以下的代码,也许会有帮助。你可以把TImage换成其它控件。关键在于对CMMouseLeave消息的处理。

unit Main;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  ExtCtrls;
type
  TMyImage = class(TImage)
  private
    FOnMouseLeave: TNotifyEvent;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  protected
  public
  published
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TMyImage]);
end;

procedure TMyImage.CMMouseLeave(var Message: TMessage);
begin
  inherited;
  if Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
end;

end. 

……

相关阅读