Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In my custom component, how can I augment the mouse-enter and -leave events?

I am making a custom Panel component which derives TPanel.

I want for my new component to have some code executed on the OnMouseEnter and OnMouseLeave events, however, i do not know how to implement it.

I see that TPanel has published properties OnMouseEnter, OnMouseLeave.

How do i override those and add some of my own code?

The example of my idea:
Default behaviour of TMyPanel which should be in component itself.

on event OnMouseEnter do: Color := NewColor;
on event OnMouseLeave do: Color := OldColor;

And then, i want to be able to assign some function to these events at run time. This assignment is done in the application.

.. TButton1.Click ..
begin
    MyPanel1.OnMouseEnter := DoSomethingMore;
    MyPanel1.OnMouseLeave := DoSomethingElse;
end;

so in the end, when mouse is over new panel, it should change color AND do some other actions written in DoSomethingMore procedure.

Thanks

like image 389
user1651105 Avatar asked Dec 14 '22 00:12

user1651105


2 Answers

Anoher approach is to handle the windows messages yourself:

type
  TMyPanel = class(TPanel)
  private
    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  published
  end;

implementation

{ TMyPanel }

procedure TMyPanel.CMMouseEnter(var Message: TMessage);
begin
     // Do whatever your want before the event
     if Assigned(OnMouseEnter) then OnMouseEnter(Self);
end;

procedure TMyPanel.CMMouseLeave(var Message: TMessage);
begin
     // Do whatever your want before the event
     if Assigned(OnMouseLeave) then OnMouseLeave(Self);
end;

EDIT: See below for better VCL compliant version.

like image 187
K.Sandell Avatar answered Dec 28 '22 10:12

K.Sandell


If they are available, you should override DoMouseEnter and DoMouseLeave. Otherwise, catch the corresponding messages, like the other answer demonstrates. Don't forget to call inherited, as this will call the events.

like image 36
Uwe Raabe Avatar answered Dec 28 '22 09:12

Uwe Raabe