Recent

Author Topic: Is there a tutorial to create a visual component for Lazarus?  (Read 13468 times)

garlar27

  • Hero Member
  • *****
  • Posts: 652
Is there a tutorial to create a visual component for Lazarus?
« on: November 29, 2010, 04:22:50 pm »
Hi,
   I'm trying to create a simple component to display a number and I don't don't have pretty clear which methods i have to override in TGraphicControl or TWinControl to paint my control.



garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #2 on: November 29, 2010, 10:23:53 pm »
Thank you very much!!

Another question:
   Do you know how to redefine the paint method of a TWinControl descendant?

   The component I'm creating contains a TLabel, and some drawings (lines, polygons, and hopefully a glyph). Also, some component's areas must remain transparent.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #3 on: November 30, 2010, 07:08:42 am »
Quote
Do you know how to redefine the paint method of a TWinControl descendant?
Code: [Select]
TMyControl = class
public
  procedure EraseBackground(DC: HDC); override;
  procedure Paint; override;
end;

...

procedure EraseBackground(DC: HDC); override;
begin
  inherited EraseBackground(DC); // default, change as necessary
end;

procedure TMyControl.Paint;
begin
  ... // you control drawing code
  inherited Paint;
end;

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #5 on: November 30, 2010, 02:51:17 pm »
I can't find the Paint method in TWinControl what I found where:

Code: [Select]
  TWinControl = class(TControl)
  protected
    procedure PaintControls(DC: HDC; First: TControl); // apparently draws child controls..
    procedure PaintHandler(var TheMessage: TLMPaint);
    procedure PaintWindow(DC: HDC); virtual;
  public
    procedure EraseBackground(DC: HDC); virtual;
    procedure PaintTo(DC: HDC; X, Y: Integer); virtual; overload;
    procedure PaintTo(ACanvas: TCanvas; X, Y: Integer); overload;
  end;

I think that PaintWindow is the one I need to override. Is that true?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #6 on: December 01, 2010, 03:23:29 am »
Sorry, you should override if your control descends from TCustomControl. TWinControl itself doesn't have any paint method and relies on the built in widgetset functionality to draw itself.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #7 on: December 01, 2010, 01:26:01 pm »
   Thank you!! Now I can draw and paint in my control!!

   But I can't make it with transparent background... I wonder what am I missing? I've overridden the EraseBackground method but some how it keeps drawing a gray rectangle in the background..

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #8 on: December 01, 2010, 01:32:09 pm »
Quote
But I can't make it with transparent background... I wonder what am I missing?
I'm not sure about how it's implemented in LCL. Perhaps you can open forms.pp and look for AlphaBlendValue. See how it's used.
Quote
I've overridden the EraseBackground method but some how it keeps drawing a gray rectangle in the background..
Did you still call inherited EraseBackground? Where? Before or after your own?

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #9 on: December 01, 2010, 05:07:00 pm »
Quote
I'm not sure about how it's implemented in LCL. Perhaps you can open forms.pp and look for AlphaBlendValue. See how it's used.
I will take a look at it..

Quote
Did you still call inherited EraseBackground? Where? Before or after your own?
Nope, EraseBackground is empty and I'm not calling it, never the less it is called in somewhere else.

The code for EraseBackground:
Code: [Select]
procedure TNewCmpnt.EraseBackground(DC: HDC);
begin
   //WriteLn('TNewCmpnt.EraseBackground(DC: HDC);'); // To see in console if it's called...
   //inherited EraseBackground(DC);
end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #10 on: December 01, 2010, 05:34:27 pm »
A quick forum search leads me to this:
http://www.lazarus.freepascal.org/index.php?topic=11019.0

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is there a tutorial to create a visual component for Lazarus?
« Reply #11 on: December 01, 2010, 06:42:44 pm »
Thanks again,

TCustomControl currently does no support transparency, you just have to draw the entire area in which it is located. You can, however:

* immitate the color of the parent control.
and
* Set an irregulare shape for the control, using TWinControl.SetShape(TRegion)

I was thinking to do a CopyRect of the area beneath the control and paint it in the control's background. Although, I have doubts about it, I will try to do it if I get the Parent's Canvas or image ...

« Last Edit: December 01, 2010, 10:08:22 pm by garlar27 »

 

TinyPortal © 2005-2018