Recent

Author Topic: Use of PaintBox  (Read 529 times)

Davidous

  • Jr. Member
  • **
  • Posts: 67
Use of PaintBox
« on: July 03, 2019, 08:02:06 pm »
Hello,

I would like to ask you of how to use a PaintBox on my Form.
I can draw on the Form Canvas, but now I would like to draw a line on the Paintbox.
Could someone please provide me with a short sample code? :)

wp

  • Hero Member
  • *****
  • Posts: 6231
Re: Use of PaintBox
« Reply #1 on: July 03, 2019, 08:05:34 pm »
Not tested but it should give you the idea:
Code: Pascal  [Select]
  1. procedure TForm1.PaintboxPaint(Sender: TObject);
  2. begin
  3.   with Paintbox1 do begin
  4.     Canvas.Brush.Color := clWhite;
  5.     Canvas.FillRect(0, 0, Width, Height);
  6.     Canvas.Pen.Color := clRed;
  7.     Canvas.Line(0, 0, Width, Height);
  8.   end;
  9. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Davidous

  • Jr. Member
  • **
  • Posts: 67
Re: Use of PaintBox
« Reply #2 on: July 03, 2019, 09:05:59 pm »
Thank you, but how to call it.
For the canvas of the form there is an OnPaint event, but there isn't such for the PaintBox.

howardpc

  • Hero Member
  • *****
  • Posts: 3149
Re: Use of PaintBox
« Reply #3 on: July 03, 2019, 09:10:22 pm »
Thank you, but how to call it.
For the canvas of the form there is an OnPaint event, but there isn't such for the PaintBox.
Yes there is.

Why do you think otherwise?

Davidous

  • Jr. Member
  • **
  • Posts: 67
Re: Use of PaintBox
« Reply #4 on: July 03, 2019, 09:27:20 pm »
Oh yes, there is, sorry :)
But what I wanted to say, that I cannot type Paintbox.Paint for some reason. Why is that?
(For my form I can type Form.Paint and then the canvas is painted.)

VTwin

  • Hero Member
  • *****
  • Posts: 773
  • Former Turbo Pascal 3 user
Re: Use of PaintBox
« Reply #5 on: July 03, 2019, 09:41:08 pm »
You don't normally call Paint, it is called as necessary when the form or control is redrawn. Use Update if required to force it for some reason, or Invalidate:

https://stackoverflow.com/questions/1251009/whats-the-difference-between-refresh-update-and-repaint
« Last Edit: July 03, 2019, 09:45:03 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0 fixes svn 61930 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.4 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.4 (64 bit on VBox)
fpc 3.0.4

lucamar

  • Hero Member
  • *****
  • Posts: 2018
Re: Use of PaintBox
« Reply #6 on: July 03, 2019, 09:48:39 pm »
Oh yes, there is, sorry :)
But what I wanted to say, that I cannot type Paintbox.Paint for some reason. Why is that?
(For my form I can type Form.Paint and then the canvas is painted.)

Paint is a protected method in both classes. You can call it for the form because you're dealing with a derived class (MyForm = class(TForm)).

However, all the default Paint method does is call the OnPaint event if set, so you may as well call it directly; i.e. instead of calling
Code: [Select]
MyPaintBox.Paintinvoke the handler directly as, for example:
Code: [Select]
MyPaintBoxPaint(Self)(whatever it is called in your program, of course).
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Davidous

  • Jr. Member
  • **
  • Posts: 67
Re: Use of PaintBox
« Reply #7 on: July 03, 2019, 10:10:45 pm »
Thank you.
Sadly I cannot get it to work. I don't know why...

Davidous

  • Jr. Member
  • **
  • Posts: 67
Re: Use of PaintBox
« Reply #8 on: July 03, 2019, 10:18:03 pm »
I just created a whole new form and placed a paintbox on it. Now it works :D
Thank you for all the ideas! :)

lucamar

  • Hero Member
  • *****
  • Posts: 2018
Re: Use of PaintBox
« Reply #9 on: July 03, 2019, 10:50:49 pm »
I just created a whole new form and placed a paintbox on it. Now it works :D
Thank you for all the ideas! :)

How were you doing it before? Just curiousity.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Davidous

  • Jr. Member
  • **
  • Posts: 67
Re: Use of PaintBox
« Reply #10 on: July 03, 2019, 11:04:44 pm »
I have a very long procedure and a form. On this form I have many graphs.
It is still interesting, because if I place a PaintBox on this Form, then for some reason I cannot draw the line I want. If I give a PaintBox to an other (empty) form, then it works. So I still don't understand...

Right now I work with these 2 lines:

PaintBox1.Canvas.Pen.Color:=clBlack;  PaintBox1.Canvas.Line(PaintBox1.Left+20,PaintBox1.Top+PaintBox1.Height,PaintBox1.Left+20,PaintBox1.Top);

On 1 form it doesn't work, on the other it does...

lucamar

  • Hero Member
  • *****
  • Posts: 2018
Re: Use of PaintBox
« Reply #11 on: July 04, 2019, 01:52:09 am »
And you're doing that in the PaintBox's OnPaint handler? Strange; it should work the same wherever it is.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Davidous

  • Jr. Member
  • **
  • Posts: 67
Re: Use of PaintBox
« Reply #12 on: July 04, 2019, 09:02:39 am »
Yes :)