Recent

Author Topic: Use of PaintBox  (Read 5352 times)

Davidous

  • Full Member
  • ***
  • Posts: 107
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: 11923
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;

Davidous

  • Full Member
  • ***
  • Posts: 107
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: 4144
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

  • Full Member
  • ***
  • Posts: 107
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: 1215
  • 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

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Davidous

  • Full Member
  • ***
  • Posts: 107
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

  • Full Member
  • ***
  • Posts: 107
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: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Davidous

  • Full Member
  • ***
  • Posts: 107
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: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Davidous

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

 

TinyPortal © 2005-2018