Recent

Author Topic: Canvas Autodraw doesn't work  (Read 1612 times)

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Canvas Autodraw doesn't work
« on: March 05, 2024, 08:03:35 am »
Hello everybody!
I have a Panel on a Form, I'm trying to draw some lines, circles, and texts,... on the Canvas:

For example:
Code: Pascal  [Select][+][-]
  1. Form1.Panel1.Canvas.TextOut(10,10,'abcdefgh');
The texts and lines are dynamic and I need to draw it based on some dynamic information. That's why I can't put all information in the Form1.Panel1Paint procedure.

So, whenever the Form is resizing, all texts, and drawn lines get lost! everything.

How to keep all content of a Canvas during window resizing?

Thank you!


dsiders

  • Hero Member
  • *****
  • Posts: 1077
Re: Canvas Autodraw doesn't work
« Reply #1 on: March 05, 2024, 08:31:24 am »
Hello everybody!
I have a Panel on a Form, I'm trying to draw some lines, circles, and texts,... on the Canvas:

For example:
Code: Pascal  [Select][+][-]
  1. Form1.Panel1.Canvas.TextOut(10,10,'abcdefgh');
The texts and lines are dynamic and I need to draw it based on some dynamic information. That's why I can't put all information in the Form1.Panel1Paint procedure.

So, whenever the Form is resizing, all texts, and drawn lines get lost! everything.

How to keep all content of a Canvas during window resizing?

Thank you!

Use the OnPaint event handler to perform drawing operations. Paint/OnPaint get called when the panel is resized.

https://lazarus-ccr.sourceforge.io/docs/lcl/extctrls/tpanel.html
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Re: Canvas Autodraw doesn't work
« Reply #2 on: March 05, 2024, 03:10:51 pm »
Thank you,
I can't use Form1.Panel1Paint procedure as I mentioned.

I am adding drawings to the Canvas real-time and dynamically.

The OnPaint event is good when we are aware of drawings during coding and before running the program.

Are there any other components that I can use as a Canvas to draw on it but handle everything by itself.

AlexTP

  • Hero Member
  • *****
  • Posts: 2399
    • UVviewsoft
Re: Canvas Autodraw doesn't work
« Reply #3 on: March 05, 2024, 03:54:21 pm »
No, OnPaint must be used on Qt5 and on macOS - otherwise no painting will happen.

In real-time you can paint on 'buffers' (TBitmap) but you should copy these buffers to Canvas in the OnPaint.
« Last Edit: March 05, 2024, 04:04:13 pm by AlexTP »

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Re: Canvas Autodraw doesn't work
« Reply #4 on: March 05, 2024, 05:16:12 pm »
Oh,....that's not good :(

What is your recommendation? what component should I choose to write a program like MS Paint? for drafting/drawing?

TRon

  • Hero Member
  • *****
  • Posts: 2496
Re: Canvas Autodraw doesn't work
« Reply #5 on: March 05, 2024, 07:54:59 pm »
Thank you,
I can't use Form1.Panel1Paint procedure as I mentioned.

I am adding drawings to the Canvas real-time and dynamically.

The OnPaint event is good when we are aware of drawings during coding and before running the program.
That tastes like baloney to me... and I'm not even hungry   ;D

Are you really saying that in your onpaint event you are not capable of doing something like:
Code: Pascal  [Select][+][-]
  1. begin
  2.   if CanPaintDynamicallyCreatedText then PaintDynamicallyCreatedText;
  3.   if MotionPathIsFullyCalculated then DrawUpdatedAnimatredImage;
  4.   etc.
  5.   etc.
  6. end;
  7.  

Not to mention that you can paint off screen in a bitmap and blit that to any canvas any time and any which way you want to.

But usually you would use a paintbox or an image. Here is a wiki article with some graphic related topics.

edit: perhaps better see this Delphi article (applies to Lazarus as well).
« Last Edit: March 05, 2024, 08:11:46 pm by TRon »

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Canvas Autodraw doesn't work
« Reply #6 on: March 05, 2024, 11:30:49 pm »
Use a Timage and update that when ever?
The only true wisdom is knowing you know nothing

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Re: Canvas Autodraw doesn't work
« Reply #7 on: March 06, 2024, 04:00:22 am »
Thank you,

Yes, TImage works! there is no need to put everything in the OnPaint procedure.

During the resizing of TImage, it repaints existing drawings on its Canvas automatically.

There is just one issue:
When the size of TImage changes, (due to resizing of the Form1), the size of the Canvas is still the original value and never changes:

(Form1.Image1.Width, Form1.Image1.Height): these two automatically change due to resizing.
(Form1.Image1.Canvas.Width, Form1.Image1.Canvas.Height): these two are the same and never change.

I tried to set the dimension of the Image1.Canvas equal to Image1 dimension in the OnResizing event, however, it didn't work!!

Any idea how to keep the size of the Canvas equal to its component size during the resizing of the component(TImage)?

PS: I guess this is a bug and should be reported. The Canvas size remains as the original size despite the TImage resizing.
« Last Edit: March 06, 2024, 05:20:11 pm by Aqdam1978@yahoo.com »

TRon

  • Hero Member
  • *****
  • Posts: 2496
Re: Canvas Autodraw doesn't work
« Reply #8 on: March 06, 2024, 07:10:04 pm »
Thank you,
You're more than welcome  :)

Quote
There is just one issue:
When the size of TImage changes, (due to resizing of the Form1), the size of the Canvas is still the original value and never changes:
Stackoverflow is able to answer that better than I am capable of   ;)

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Re: Canvas Autodraw doesn't work
« Reply #9 on: March 06, 2024, 07:40:08 pm »
I applied the codes as your mentioned website said:

Code: Pascal  [Select][+][-]
  1.   Form1.Image1.Picture.Bitmap.Width:=Form1.Image1.Width;
  2.   Form1.Image1.Picture.Bitmap.Height:=Form1.Image1.Height;

However, after running the program, it didn't work and Canvas sizes changed to 0.

When I watch the variable it shows an error for the above code as follow:
Code: Pascal  [Select][+][-]
  1. Form1.Image1.Picture.Picture.Bitmap.Width = <Error: Member not found: Picture.Bitmap>
  2. public
  3. property
  4.  TGraphic.Width:Integer = longint
  5. C:\lazarus\lcl\graphics.pp(875,14)

Please note that I have not loaded any pictures to the TImage. I have the TImage on my form just to use it as a Canvas to draw on it. (lines, texts, shapes,...)

TRon

  • Hero Member
  • *****
  • Posts: 2496
Re: Canvas Autodraw doesn't work
« Reply #10 on: March 06, 2024, 07:47:07 pm »

Code: Pascal  [Select][+][-]
  1. Form1.Image1.Picture.Picture.Bitmap.Width = <Error: Member not found: Picture.Bitmap>
  2. public
  3. property
  4.  TGraphic.Width:Integer = longint
  5. C:\lazarus\lcl\graphics.pp(875,14)
That is actually so confusing to me that I would say: no guts, no glory (meaning: please post your form unit code)

edit: attached small resize image experiment
« Last Edit: March 06, 2024, 08:03:41 pm by TRon »

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Re: Canvas Autodraw doesn't work
« Reply #11 on: March 06, 2024, 09:14:27 pm »
Sure,
Please see the sample TImage/Canvas demonstration.

Thank you

TRon

  • Hero Member
  • *****
  • Posts: 2496
Re: Canvas Autodraw doesn't work
« Reply #12 on: March 06, 2024, 09:50:15 pm »
Please see the sample TImage/Canvas demonstration.
I see.

Your example did not follow the rules of TImage, see here (make sure to read all the subchapters that belong to it's parent topic).

Image1.canvas is only applicable in the OnPaint event (and /only/ in the OnPaint event). Outside the OnPaint event you need to use the canvas of the picture.bitmap (but only after you resized the bitmap, for which you could also use setsize(<widht>, <height>) btw).

The thing that is confusing (even for me) is the paintbackground event. I would have thought that the canvas is applicable there as well (but in practice for me on Linux it doesn't seem to be).

edit: I have attached a modified (working as you wanted ?) version of your example with some additional comments (please forgive me for the typo's)
« Last Edit: March 06, 2024, 10:34:17 pm by TRon »

Aqdam1978@yahoo.com

  • New Member
  • *
  • Posts: 27
Re: Canvas Autodraw doesn't work
« Reply #13 on: March 06, 2024, 10:44:49 pm »
Thank you TRon for your help and time.
I appreciate it.

The problem is I can't use OnPaint (Image1Paint) to draw. because it is useful only during coding.
I want to draw items on the Canvas during the runtime of the program. (imagine AutoCAD to have some idea of what I'm looking for). A drawing window where I can draw anything anytime within any subroutines of my program. That's why I can't use OnPaint.

I just want to set size of canvas same as size of TImage. It solves my problem! I still think this a bug.
« Last Edit: March 06, 2024, 10:56:52 pm by Aqdam1978@yahoo.com »

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Canvas Autodraw doesn't work
« Reply #14 on: March 06, 2024, 10:53:50 pm »
Yes you can. The thing is that you need to rethink your logic.

See Lazpaint. It uses a single bgravirtualscreen that redraw only on paint.

 

TinyPortal © 2005-2018