In my custom component, how can I increase I / O and -leave events?

I am creating a custom Panel component that displays a TPanel .

I want my new component to have some code executed on the OnMouseEnter and OnMouseLeave events , however I don’t know how to implement it.

I see that TPanel has published the properties OnMouseEnter , OnMouseLeave .

How to override them and add some of my own codes?

An example of my idea:
The default behavior is TMyPanel , which should be in the 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 runtime. This assignment is performed in the application.

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

therefore, at the end, when the mouse is over a new panel, it should change color and perform some other actions written in the DoSomethingMore procedure.

thanks

+4
source share
3 answers

An anonymous approach is to process 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 the best version compatible with VCL.

+9
source

If available, you must override DoMouseEnter and DoMouseLeave. Otherwise, catch the appropriate messages, for example, as another answer shows . Remember to call the inherited, as this will trigger events.

+3
source

Here is the VCL compatible version (tested by 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. 
0
source

Source: https://habr.com/ru/post/1300019/


All Articles