Recent

Author Topic: Mouse Events for all components  (Read 3603 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Mouse Events for all components
« on: October 24, 2021, 08:23:47 pm »
Hello, how can I implement events for all TPanel components, I mean onMouseEnter, onMouseLeave events
I wrote a loop but I have a problem with the MouseEnter and MouseLeave events
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.  
  4.   const
  5.   _clMouseEntered = clRed;
  6.   _clMouseLeaved = clHighlight;
  7.  
  8. implementation
  9.  
  10. {$R *.frm}
  11.  
  12. { TForm1 }
  13.  
  14. procedure TForm1.FormCreate(Sender: TObject);
  15. var
  16.   i: Integer;
  17. begin
  18.    for i := 0 to ComponentCount-1 do
  19.     if Components[i] is TPanel then with TPanel(Components[i]) do
  20.     begin
  21.       OnMouseEnter :=
  22.       OnMouseLeave :=
  23.     end;
  24. end;    


Handoko

  • Hero Member
  • *****
  • Posts: 5132
  • My goal: build my own game engine using Lazarus
Re: Mouse Events for all components
« Reply #1 on: October 24, 2021, 08:54:51 pm »
Hello Pe3s,
Welcome to the forum.

It should be something like this:

Code: Pascal  [Select][+][-]
  1.   { TForm1 }
  2.  
  3.   TForm1 = class(TForm)
  4.      .
  5.      .
  6.   private
  7.     procedure MyPanelMouseEnter(Sender: TObject);
  8.     procedure MyPanelMouseLeave(Sender: TObject);
  9.      .
  10.      .
  11.   end;
  12.      .
  13.      .
  14.  
  15. implementation
  16.  
  17. {$R *.lfm}
  18.  
  19. { TForm1 }
  20.  
  21. procedure TForm1.FormCreate(Sender: TObject);
  22. var
  23.   i: Integer;
  24. begin
  25.    for i := 0 to ComponentCount-1 do
  26.     if Components[i] is TPanel then with TPanel(Components[i]) do
  27.     begin
  28.       OnMouseEnter := @MyPanelMouseEnter;
  29.       OnMouseLeave := @MyPanelMouseLeave;
  30.     end;
  31. end;

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Mouse Events for all components
« Reply #2 on: October 25, 2021, 05:39:02 pm »
I tried to change the color of the tPanel component, but it does not work

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Panel1: TPanel;
  16.     Panel2: TPanel;
  17.     Panel3: TPanel;
  18.     Panel4: TPanel;
  19.     procedure FormCreate(Sender: TObject);
  20.   private
  21.     procedure MyPanelMouseEnter(Sender: TObject);
  22.     procedure MyPanelMouseLeave(Sender: TObject);
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. var
  39.   i: Integer;
  40. begin
  41.    for i := 0 to ComponentCount-1 do
  42.     if Components[i] is TPanel then with TPanel(Components[i]) do
  43.     begin
  44.       OnMouseEnter:=@MyPanelMouseEnter;
  45.       OnMouseLeave:=@MyPanelMouseLeave;
  46.     end;
  47. end;
  48.  
  49. procedure TForm1.MyPanelMouseEnter(Sender: TObject);
  50. begin
  51.  
  52. end;
  53.  
  54. procedure TForm1.MyPanelMouseLeave(Sender: TObject);
  55. begin
  56.  
  57. end;
  58.  
  59. end.              

I have a problem with completing the procedures

procedure TForm1.MyPanelMouseEnter(Sender: TObject);
begin
     Color:= clRed;
end;

procedure TForm1.MyPanelMouseLeave(Sender: TObject);
begin
    Color:= clLime;

end;   

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Mouse Events for all components
« Reply #3 on: October 25, 2021, 05:52:05 pm »
Reusing the transtyping \ typecasting (like jamie showed), you should call TPanel(Sender).Color:= clRed; in your @MyPanelMouseEnter and @MyPanelMouseLeave commun events shared between the panels.
« Last Edit: October 25, 2021, 09:25:05 pm by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Mouse Events for all components
« Reply #4 on: October 25, 2021, 05:57:03 pm »
Reusing the transtyping \ typecasting (like @jamie showed), you should call TPanel(Sender).Color:= clRed; in your @MyPanelMouseEnter and @MyPanelMouseLeave commun events shared between the panels.

Thank you :)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Mouse Events for all components
« Reply #5 on: October 25, 2021, 06:01:54 pm »
problem solved  :)

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Mouse Events for all components
« Reply #6 on: October 25, 2021, 08:53:01 pm »
TForm has an enumerator for components. In my opinion the following code looks better:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   C: TComponent;
  4. begin
  5.   for C in Self do
  6.     if C is TPanel then
  7.     begin
  8.       TPanel(C).OnMouseEnter := @MyPanelMouseEnter;
  9.       TPanel(C).OnMouseLeave := @MyPanelMouseLeave;
  10.     end;
  11. end;

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Mouse Events for all components
« Reply #7 on: October 26, 2021, 04:53:49 pm »
Thank you  :)

 

TinyPortal © 2005-2018