设计的一个图形组件需要当鼠标离开组件的时候响应一个外部事件,应该如何实现?

解决方案 »

  1.   

    从TControl继承下来的类有CM_MOUSEENTER和CM_MOUSELEAVE消息即是对应于进入到离开的时候。blog.csdn.net/linzhengqun
      

  2.   

    http://cache.baidu.com/c?word=delphi%2Cvar%2Ccm%3B%5F%3Bmouseleave%2Cmessage&url=http%3A//www%2Eevget%2Ecom/articles/evget%5F399%2Ehtml&b=0&a=90&user=baidu给控件加入声明:
    FOnMouseLeave: TNotifyEvent;
    procedure CMMouseLeave(var Msg:TMessage);message CM_MouseLeave;
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;实现procedure TCustomLabel.CMMouseLeave(var Message: TMessage);
    begin
      inherited;
      if Assigned(FOnMouseLeave) then
        FOnMouseLeave(Self);
    end;
      

  3.   

    有楼上的高手为你指点,错不了。
    aiirii(ari-淘金坑) 曾帮过我好多次。
      

  4.   

    //鼠标移动到 Image ,实现抖动效果
    //yeeyeeunit MyImage;interfaceuses
      SysUtils, StdCtrls, Classes, Controls, ExtCtrls, Messages, Graphics;const cnstLEFTSTEP=4;
    const cnstTOPSTEP=3;type
      TMyImage = class(TImage)
      private
        { Private declarations }
        FMoved:boolean;
        FLabel:TLabel;
        FOnMouseLeave: TNotifyEvent;
        FOnMouseEnter: TNotifyEvent;
        procedure SetMoved(const AValue:boolean);
        procedure SetLabel(AValue:TLabel);    procedure LabelMouseEnter(Sender: TObject);
        procedure LabelMouseLeave(Sender: TObject);
      protected
        { Protected declarations }
        procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
        procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
      published
        { Published declarations }
        property YMoved: boolean read FMoved write SetMoved;
        property YLabel: TLabel read FLabel write SetLabel;
        property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
        property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
      end;//procedure Register;implementation
    {
    procedure Register;
    begin
      RegisterComponents('Yeeyee', [TMyImage]);
    end;
    }
    constructor TMyImage.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FMoved:=False;
    end;procedure TMyImage.SetMoved(const AValue:boolean);
    begin
      if FMoved<>AValue then
      begin
        FMoved:=AValue;
      end;
    end;procedure TMyImage.SetLabel(AValue:TLabel);
    begin
      if FLabel<>AValue then
      begin
        FLabel:=AValue;
        if Assigned(FLabel) then
        begin
          FLabel.Left:=Left+(Width-FLabel.Width) div 2;
          //FLabel.Top:=Top+Height-FLabel.Height;
          FLabel.Top:=Top+Height+FLabel.Height;
          FLabel.Font.Name:='宋体';
          FLabel.Font.Size:=10;
          FLabel.OnMouseEnter:=LabelMouseEnter;
          FLabel.OnMouseLeave:=LabelMouseLeave;
        end;
      end;
    end;procedure TMyImage.LabelMouseEnter(Sender: TObject);
    begin
      if Assigned(FLabel) then
      begin
        FLabel.Font.Color:=clBlue;
        FLabel.Cursor:=crHandPoint;    if FMoved then
        begin
          Left:=Left-cnstLEFTSTEP;
          Top:=Top-cnstTOPSTEP;
          FLabel.Left:=FLabel.Left-cnstLEFTSTEP;
          FLabel.Top:=FLabel.Top-cnstTOPSTEP;
        end;
      end;
    end;procedure TMyImage.LabelMouseLeave(Sender: TObject);
    begin
      if Assigned(FLabel) then
      begin
        FLabel.Font.Color:=clBlue;
        FLabel.Cursor:=crHandPoint;    if FMoved then
        begin
          Left:=Left+cnstLEFTSTEP;
          Top:=Top+cnstTOPSTEP;
          FLabel.Left:=FLabel.Left+cnstLEFTSTEP;
          FLabel.Top:=FLabel.Top+cnstTOPSTEP;
        end;
      end;
    end;procedure TMyImage.CMMouseEnter(var Message: TMessage);
    begin
      inherited;
      if FMoved then
      begin
        Left:=Left-cnstLEFTSTEP;
        Top:=Top-cnstTOPSTEP;
        FLabel.Left:=FLabel.Left-cnstLEFTSTEP;
        FLabel.Top:=FLabel.Top-cnstTOPSTEP;
      end;
      if Assigned(FLabel) then
      begin
        FLabel.Font.Color:=clBlue;
        FLabel.Cursor:=crHandPoint;
      end;
      Cursor:=crHandPoint;
      if Assigned(FOnMouseEnter) then
        FOnMouseEnter(Self);
    end;procedure TMyImage.CMMouseLeave(var Message: TMessage);
    begin
      inherited;
      if FMoved then
      begin
        Left:=Left+cnstLEFTSTEP;
        Top:=Top+cnstTOPSTEP;
        FLabel.Left:=FLabel.Left+cnstLEFTSTEP;
        FLabel.Top:=FLabel.Top+cnstTOPSTEP;
      end;    
      if Assigned(FLabel) then
      begin
        FLabel.Font.Color:=clWindowText;
        FLabel.Cursor:=crDefault;
      end;
      Cursor:=crDefault;
      if Assigned(FOnMouseLeave) then
        FOnMouseLeave(Self);
    end;end.
      

  5.   

    (aiirii(ari-淘金坑) 这个方法真不错,谢谢!
    以后还请各位多多关照。