Recent

Author Topic: Caching mouse event on your own component  (Read 7889 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 818
    • Blog personal
Caching mouse event on your own component
« on: April 03, 2011, 12:46:02 pm »
Hello, I trying to make my own component. Well In my first test I wrote :
Code: Pascal  [Select][+][-]
  1. TIndicador = Class (TCustomPanel)
  2.  
  3. Private
  4.   pp : TMouseMoveEvent ;
  5.  Public
  6.  Constructor Create (Propietario:TComponent;ColorFondo:TColor);Overload;
  7.  Procedure MouseDown (Sender :TObject ; Button :TMouseButton ;
  8.       Shift :TShiftState ; X ,Y :Integer );  Virtual;
  9. end;
  10. implementation
  11.  
  12. { TIndicador }
  13.  
  14. constructor TIndicador .Create (Propietario :TComponent ; ColorFondo :TColor );
  15. begin
  16.  inherited Create (Propietario);
  17.  Color := ColorFondo;
  18. end;
  19.  
  20. procedure TIndicador .MouseDown (Sender :TObject ; Button :TMouseButton ;
  21.   Shift :TShiftState ; X ,Y :Integer );
  22. begin
  23.  Color := clBlack;
  24. end;
  25.  
  26.  

The component works fine, except for I the mouse event. I was reading the wiki and the web, but I couldn't make it works. I read some thing about TNotify event, but I couldn't make it works.

Anyone can help me to know how can I put events for my own components?

Thanks

/BlueIcaro
« Last Edit: April 03, 2011, 12:50:28 pm by BlueIcaro »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Caching mouse event on your own component
« Reply #1 on: April 03, 2011, 01:15:20 pm »
I see error there -  constructor must be override; not overload;
Code: [Select]
Constructor Create (Propietario:TComponent;ColorFondo:TColor); Override;
IMO the same should be with MouseDown, and there is no Sender: TObject in this overriden procedure:
Code: [Select]
Procedure MouseDown (Button :TMouseButton; Shift :TShiftState ; X ,Y :Integer );  override; 
and in code, the mousedown should be also inherited; similarly to constructor:
Code: [Select]
procedure TIndicador.MouseDown (Button :TMouseButton ;  Shift :TShiftState ; X ,Y :Integer ); 
begin
  inherited MouseDown(Button, Shift, X, Y);
 Color := clBlack; 
end;

Not tested  :)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BlueIcaro

  • Hero Member
  • *****
  • Posts: 818
    • Blog personal
Re: Caching mouse event on your own component
« Reply #2 on: April 03, 2011, 01:32:06 pm »
Hello Blaazen, thanks for your fast answer, your correction about the mouse event it's works fine. But If I declare the constructor, ussing OverRide, I got the following error when I try to compile:
Quote

unit2.pas(20,14) Error: There is no method in an ancestor class to be overridden: "constructor TIndicador.Create(TComponent,TGraphicsColor);"


Thanks

/BlueIcaro

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Caching mouse event on your own component
« Reply #3 on: April 03, 2011, 01:40:08 pm »
I didn't see this.
You are overriding constructor of TCustomPanel (which is ancestor of your component) so it must be declared:
Code: [Select]
constructor Create(TheOwner: TComponent); override; 
and in code:
Code: [Select]
constructor TIndicador .Create (TheOwner: TComponent); 
begin 
 inherited Create (TheOwner); 
 Color := ColorFondo; 
end; 

Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

circular

  • Hero Member
  • *****
  • Posts: 4397
    • Personal webpage
Re: Caching mouse event on your own component
« Reply #4 on: April 03, 2011, 03:57:30 pm »
As far as I know, constructors cannot be overriden because a constructor is not a virtual procedure. When you call the constructors, it won't search for a virtual method, it just search for the methods through the object hierarchy.

If you put a constructor with same parameters as an inherited one, it will have a higher priority compared to the inherited constructor when you try to call TDerivedClass.Create(...). No need to specify the it overrides the other.
Conscience is the debugger of the mind

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Caching mouse event on your own component
« Reply #5 on: April 03, 2011, 04:22:27 pm »
To be fair, I am not the greatest expert for OOP.
In hierarchy of components is very first TComponent declared like:
Code: [Select]
constructor Create(AOwner: TComponent); virtual;  and all successor are delcared like:
Code: [Select]
constructor Create(AOwner: TComponent); override;All successors contains in code:
Code: [Select]
constructor TAnySuccessor. Create(AOwner: TComponent); override;
begin
  ... some code

  inherited Create(AOwner);

  ... some other  code

end;
IMO you don't need declare constructor at all if you don't need to do some special code there.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

circular

  • Hero Member
  • *****
  • Posts: 4397
    • Personal webpage
Re: Caching mouse event on your own component
« Reply #6 on: April 03, 2011, 08:24:13 pm »
Ok, so it depends if the constructor is virtual or not. It this case, there is no virtual constructor declared with these parameters, that's it's neither overload nor override.

So what's the difference when not declaring a constructor as virtual ?

Maybe if you instanciate an object from a TClass value.
« Last Edit: April 03, 2011, 08:30:13 pm by circular »
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018