Recent

Author Topic: Custom Button  (Read 1240 times)

dtpfl

  • Newbie
  • Posts: 2
Custom Button
« on: September 22, 2021, 05:50:31 pm »
Hello,

i wanna try to create my own component.
It should be a rounded button (like attachment).

1. Whats the best way and where should i inherited from? TComponent?
2. I guess that i need to paint the component by my self. Is there a easy way to paint a round rectangle with
 antialiasing?

thanks!

Zoran

  • Hero Member
  • *****
  • Posts: 1675
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Custom Button
« Reply #1 on: September 22, 2021, 07:10:54 pm »
1. Whats the best way and where should i inherited from? TComponent?

If you want your button to be able to receive focus (so that it can be accessed by keyboard, like TButton), inherit from TCustomControl.
If you don't want the focus (like TSpeedButton, which cannot be accesed by keyboard), inherit from TGraphicControl.
Both these classes provide the canvas you can paint on. Both classes have virtual method named Paint, and you should override it and do painting on canvas there.

2. I guess that i need to paint the component by my self. Is there a easy way to paint a round rectangle with
 antialiasing?

You can use TCanvas methods to paint, but I don't know about antialiasing, for that perhaps you can use bgra library.

Some wiki links which might be useful:
https://wiki.freepascal.org/Graphics_-_Working_with_TCanvas
https://wiki.freepascal.org/TCanvas
https://wiki.lazarus.freepascal.org/Developing_with_Graphics

devEric69

  • Hero Member
  • *****
  • Posts: 599
Re: Custom Button
« Reply #2 on: September 22, 2021, 08:22:40 pm »
As said, basically, the structure of the code to be implemented should look like this:

Code: Pascal  [Select][+][-]
  1.  
  2. TRoundedButton = class(TCustomControl)
  3.   ...\...
  4.   protected
  5.     procedure Paint; override;
  6.   ...\...
  7.  
  8.   ...\...
  9. procedure TRoundedButton.Paint(Sender: TObject);
  10. begin
  11.   with Self.Canvas do begin
  12.     { color the shape }
  13.     Brush.Color:= clActiveCaption;
  14.     RoundRect(0, 0, 50, 25);
  15.     { draw its borders }
  16.     Pen.Color := clActiveBorder;
  17.     ...\...
  18. end;

Here's an example: https://stackoverflow.com/questions/46651543/delphi-7-how-to-fill-a-rounded-rectangle-with-brush .
« Last Edit: September 22, 2021, 08:25:31 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.

 

TinyPortal © 2005-2018