Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger an event when the mouse leaves my control?

How do I create an OnMouseLeave event?

like image 745
Little Helper Avatar asked Apr 27 '11 14:04

Little Helper


People also ask

Which event can be generated when the mouse leaves an element?

The onmouseout event occurs when the mouse pointer is moved out of an element, or out of one of its children.

Which event occurs when mouse moves over any control?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children.

Which event is triggered when mouse enters a specific region?

The mouseenter event is fired at an Element when a pointing device (usually a mouse) is initially moved so that its hotspot is within the element at which the event was fired.


1 Answers

Another alternative to the Andreas solution, is use the CM_MOUSELEAVE VCL Message which is already defined in delphi 7.

check this sample using a interposer class for the TButton

type
  TButton = class(StdCtrls.TButton)
  private
    FOnMouseLeave: TNotifyEvent;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  protected
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  end;


  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
  private
     procedure ButtonMouseLeave(Sender: TObject);
  public
  end;

//handle the message and call the event handler
procedure TButton.CMMouseLeave(var Message: TMessage);
begin
  if (Message.LParam = 0) and Assigned(FOnMouseLeave) then
      FOnMouseLeave(Self);
end;


procedure TForm1.ButtonMouseLeave(Sender: TObject);
begin
   //your code goes here   
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //assign the event
  Button1.OnMouseLeave:=ButtonMouseLeave;
end;
like image 160
RRUZ Avatar answered Nov 15 '22 06:11

RRUZ