Recent

Author Topic: Drawing on Panel  (Read 3894 times)

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Drawing on Panel
« on: October 17, 2021, 10:07:59 am »
Drawing on Panel .. asked several times again and again, yes ..

Why not ? It's a decendent of a TWinControl.


I solved it - for a upcoming component - by overwriting procedure Paint


Any comments ?
Is this the wrong way to go ?


usually using latest Lazarus release version with Windows 10

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Re: Drawing on Panel
« Reply #1 on: October 17, 2021, 11:04:51 am »
.. now using bgrabitmap-11.4 from

https://github.com/bgrabitmap/bgrabitmap/releases
« Last Edit: October 17, 2021, 11:11:42 am by PeterX »
usually using latest Lazarus release version with Windows 10

circular

  • Hero Member
  • *****
  • Posts: 3743
    • Personal webpage
Re: Drawing on Panel
« Reply #2 on: October 17, 2021, 11:38:45 am »
Yes, overriding Paint is the way.  :)

I wonder about always calling the inherited Paint, in case something in the base control is done. But that's probably unnecessary.

Using CanvasBGRA is also the way.  :)
Conscience is the debugger of the mind

Okoba

  • Full Member
  • ***
  • Posts: 249
Re: Drawing on Panel
« Reply #3 on: October 17, 2021, 11:49:49 am »
Your way is correct. As Circular mentioned, using CanvasBGRA seems a good idea, or even Canvas2d.
One other note is that if you are trying to be more efficient or using this way for big controls, you can create and destroy the Bitmap with controls Create and destroy, and fill it with transparent pixels every time. This way is more efficient as you do not make and allocate memory for each refresh of the control.

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Re: Drawing on Panel
« Reply #4 on: October 17, 2021, 12:35:24 pm »
I wonder about always calling the inherited Paint, in case something in the base control is done. But that's probably unnecessary.
Yes, sometimes "hidden" things happen in the overwritten Component / procedure ..  :o
usually using latest Lazarus release version with Windows 10

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Re: Drawing on Panel
« Reply #5 on: October 17, 2021, 12:43:48 pm »
Your way is correct. As Circular mentioned, using CanvasBGRA seems a good idea, or even Canvas2d.
I've never used BGRA, just discovered it today.
What does Canvas2D do different to CanvasBGRA ?

One other note is that if you are trying to be more efficient or using this way for big controls, you can create and destroy the Bitmap with controls Create and destroy, and fill it with transparent pixels every time. This way is more efficient as you do not make and allocate memory for each refresh of the control.
Yes, I also thought "why is the bitmap always created and destroyed ?"
But I picked it up from some example code, indeed should be done more efficiently,
and Bitmap then must be resized "OnResize" of the TPanel
( .. or whatever Component I use as basis component)

What is "transparent pixels" in BGRA ?
usually using latest Lazarus release version with Windows 10

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Re: Drawing on Panel
« Reply #6 on: October 17, 2021, 01:03:22 pm »
The text, drawn by Canvas.TextOut() , looks better than the one drawn by BGRA ..
( the upper one is from BGRA, the lower one from Lazarus Canvas )

Probably a Font Settings issue only,
or because it's different size, but ..

No problem to finally draw the text onto the Canvas, after drawing the Bitmap  ..
« Last Edit: October 17, 2021, 01:05:46 pm by PeterX »
usually using latest Lazarus release version with Windows 10

winni

  • Hero Member
  • *****
  • Posts: 2792
Re: Drawing on Panel
« Reply #7 on: October 17, 2021, 01:03:40 pm »
What does Canvas2D do different to CanvasBGRA ?

Hi

Have a look at

https://wiki.lazarus.freepascal.org/BGRABitmap_tutorial_14


Quote
What is "transparent pixels" in BGRA ?

Code: Pascal  [Select][+][-]
  1. TBGRAPixel = packed record
  2.     red, green, blue, alpha: byte;
  3.     end;

alpha is the amount of transparency.
255 means it is opaque
0 means total transparency

Winni
« Last Edit: October 17, 2021, 01:05:27 pm by winni »

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Re: Drawing on Panel
« Reply #8 on: October 17, 2021, 01:11:46 pm »
Hi

Have a look at

https://wiki.lazarus.freepascal.org/BGRABitmap_tutorial_14

Thanks a lot for the Link !   8)

I have to get deeper into it after I managed to create
the first working, basic, CustomComponent for my app ..
usually using latest Lazarus release version with Windows 10

circular

  • Hero Member
  • *****
  • Posts: 3743
    • Personal webpage
Re: Drawing on Panel
« Reply #9 on: October 17, 2021, 03:08:15 pm »
The text, drawn by Canvas.TextOut() , looks better than the one drawn by BGRA ..
( the upper one is from BGRA, the lower one from Lazarus Canvas )
The problem comes from the fact that the bitmap is not cleared with an opaque color.

You use the function Rectangle to do that, but that only draw the edges. You can use FillRectangle instead. Or just Fill to fill everything.
Conscience is the debugger of the mind

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Re: Drawing on Panel
« Reply #10 on: October 17, 2021, 07:07:24 pm »
You use the function Rectangle to do that, but that only draw the edges. You can use FillRectangle instead. Or just Fill to fill everything.
With Brush.Color:= xxx   procedure Rectangle() also fills the Rect.


Now I tried with yellow .. and

Code: Pascal  [Select][+][-]
  1.         // Draws the background
  2.         GUIBitmap.CanvasBGRA.Pen.Color:= clBlack;
  3.         GUIBitmap.CanvasBGRA.Brush.Color:= clYellow;  // clWhite;
  4.         GUIBitmap.CanvasBGRA.Rectangle( 0, 0, Width, Height);
  5.         //GUIBitmap.CanvasBGRA.FloodFill( 0, 0);
  6.  

but no difference, from my point of view, for the example text with BGRA.

( Floodfill does the same, but - no question - without the black border .. )
« Last Edit: October 17, 2021, 07:20:51 pm by PeterX »
usually using latest Lazarus release version with Windows 10

circular

  • Hero Member
  • *****
  • Posts: 3743
    • Personal webpage
Re: Drawing on Panel
« Reply #11 on: October 21, 2021, 09:01:59 am »
Oh yeah I got confused, I thought about the Rectangle function outside of CanvasBGRA like GUIBitmap.Rectangle, but indeed inside CanvasBGRA it can fill.

Maybe Brush.Style is not set to bsSolid and thus shape is not filled.

In your second screenshot, the result is better, font is rendered properly. So actually filling makes a difference.

As said before you can do GUIBitmap.Fill(clYellow) for example.

Now, the font appears bolder, it seems there isn't ClearType effect. I suggest you try to play with the Font.Quality property, for example setting it to fqFineClearType.
Conscience is the debugger of the mind

PeterX

  • Sr. Member
  • ****
  • Posts: 398
Re: Drawing on Panel
« Reply #12 on: October 21, 2021, 08:59:50 pm »
.. I suggest you try to play with the Font.Quality property, for example setting it to fqFineClearType.
Instead of always redrawing the whole Bitmap in Paint()
I will draw my "background" once on Create()  ( and on resize if required .. )
and just map this static Bitmap in Paint().  This saves CPU time.

As the shown Text changes and needs to be redrawn regulary
I can draw it with the default Canvas TextOut().
The advantage for me is, the Text appearance / look
is exactly like in any other Lazarus Component.
« Last Edit: October 21, 2021, 09:17:13 pm by PeterX »
usually using latest Lazarus release version with Windows 10

 

TinyPortal © 2005-2018