OnMouseEnter and OnMouseLeave in a TImage  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 05/02/00

Tip accessed 646 times

 


There are times that we would like to have events OnMouseEnter and OnMouseLeave in some of our controls, or in the standard controls of Delphi, to be able to change the aspect of the component when the user passes the cursor of the mouse over it.
In all the descendants of TComponent we will be able to add these two new events easily. We will only have to capture the messages CM_MOUSEENTER and CM_MOUSELEAVE, and two events that we have added in the component.

For example, here you have a descendant of TImage in which I have added these two events:

 unit Image2;

 interface

 uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   ExtCtrls;

 type
   TEntrarEvent=procedure(Sender:TObject) of object;
   TSalirEvent =procedure(Sender:TObject) of object;
   TImage2 = class(TImage)
   private
     { Private declarations }
     FOnEnter:TEntrarEvent;
     FOnExit :TSalirEvent;
     procedure CMMouseEnter(var msg:TMessage); message CM_MOUSEENTER;
     procedure CMMouseLeave(var msg: TMessage); message CM_MOUSELEAVE;
   protected
     { Protected declarations }
   public
     { Public declarations }
   published
     { Published declarations }
     property OnMouseEnter: TEntrarEvent read FOnEnter write FOnEnter;
     property OnMouseLeave: TSalirEvent read FOnExit write FOnExit;

   end;

 procedure Register;

 implementation

 procedure TImage2.CMMouseEnter(var msg:TMessage);
 begin
     inherited;
     {Cuando el raton entra...}
     If Assigned(FOnEnter) then FOnEnter(Self);
 end;

 procedure TImage2.CMMouseLeave(var msg: TMessage);
 begin
     inherited;
     {Cuando el raton sale}
     If Assigned(FOnExit) then FOnExit(self);
 end;

 procedure Register;
 begin
   RegisterComponents('TMania', [TImage2]);
 end;

 end.