user1651105
user1651105

Reputation: 1807

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

Upvotes: 4

Views: 3815

Answers (3)

K.Sandell
K.Sandell

Reputation: 1394

Here's a VCL compliant version (tested D2010)

unit Unit1;

interface

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

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

  TForm1 = class(TForm)
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Procedure OnMEnter(Sender: TObject);
    Procedure OnMLeave(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
     With TMyPanel.Create(Form1) do
     Begin
          Parent := Form1;
          Caption := 'Test';
          OnMouseEnter := OnMEnter;
          OnMouseLeave := OnMLeave;
     End;
end;

procedure TForm1.OnMEnter(Sender: TObject);
begin
     Form1.Caption := 'Entered';
end;

procedure TForm1.OnMLeave(Sender: TObject);
begin
     Form1.Caption := 'Left';
end;

{ TMyPanel }

procedure TMyPanel.CMMouseEnter(var Message: TMessage);
begin
     // Do whatever your want before the event
     Self.Caption := 'Custom Enter';
     // Call inhertied method handler
     Inherited;
end;

procedure TMyPanel.CMMouseLeave(var Message: TMessage);
begin
     // Do whatever your want before the event
     Self.Caption := 'Custom Left';
     // Call inhertied method handler
     Inherited;
end;

end.

Upvotes: 1

K.Sandell
K.Sandell

Reputation: 1394

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.

Upvotes: 9

Uwe Raabe
Uwe Raabe

Reputation: 47694

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.

Upvotes: 3

Related Questions