Recent

Author Topic: How to create a PaintBox in runtime?  (Read 12068 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 827
    • Blog personal
How to create a PaintBox in runtime?
« on: April 11, 2011, 09:41:58 pm »
Hello, I trying to create a TPaintBox in runtime, but It's doesn't work. Here is my code:
Code: Pascal  [Select][+][-]
  1. var
  2.   P :TPaintBox ;
  3. begin
  4.  
  5.   P := TPaintBox.Create(Form1);
  6.   P.Top :=10;
  7.   P.Left :=10;
  8.   P.Height :=50;
  9.   P.Width := 100;
  10.   P.Parent := Form1;
  11.   p.Color :=clRed;
  12.   P.Canvas.Ellipse(10,10,20,20);
  13. end;  
  14.  

Any idea?

/BlueIcaro

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: How to create a PaintBox in runtime?
« Reply #1 on: April 11, 2011, 10:13:33 pm »
Your TPaintbox P is a local variable and probably is lost when the function is done.
Make it a global variable or a variable in Form1.
1.0/2.6.0  XP SP3 & OS X 10.6.8

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to create a PaintBox in runtime?
« Reply #2 on: April 11, 2011, 10:46:13 pm »
I can reproduce this.

Lazarus 0.9.30 r29749 FPC 2.4.2 i386-win32-win32/win64

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to create a PaintBox in runtime?
« Reply #3 on: April 11, 2011, 10:52:36 pm »
Yes, Arbee is right, P should be global
and yes, code does not work.
Here on Qt widgetset I can paint on TPaintBox only inside OnPaint method.
You can use OnPaint for drawing or if you want paint in other methods then use TImage instead of TPaintBox.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: How to create a PaintBox in runtime?
« Reply #4 on: April 11, 2011, 11:19:48 pm »
It's better to paint in the OnPaint event.

Code: Pascal  [Select][+][-]
  1. ...
  2. p.Color :=clRed;
  3. p.OnPaint:=@PPaint;
  4. end;
  5.  
  6. procedure TForm1.PPaint(Sender:TObject);
  7. begin
  8.   P.Canvas.Ellipse(10,10,20,20);
  9. end;
  10.  

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: How to create a PaintBox in runtime?
« Reply #5 on: April 12, 2011, 12:19:34 am »
Or even better:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PPaint(Sender: TObject);
  2. begin
  3.   with Sender as TPaintBox do
  4.   begin
  5.     Color := clRed;
  6.     Canvas.Ellipse(10,10,20,20);
  7.   end;
  8. end;
This eliminates the necessity for having a global (worst case!!) or form variable.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 827
    • Blog personal
Re: How to create a PaintBox in runtime?
« Reply #6 on: April 12, 2011, 10:58:01 am »
Hello, I create a Global Var, and used de Paint Event, and it doesn't work.  %)
/BLueIcaro
P.D I using Lazarus 0.9.30

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: How to create a PaintBox in runtime?
« Reply #7 on: April 12, 2011, 11:26:40 am »
OK ... let me give a small program that *should* work.  Then you can perhaps figure out how to implement that in your situation.

I have a simple application with only a button.  The paintbox is displayed when you click the button.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
     procedure FormCreate(Sender: TObject);
  private
    { private declarations }
     P : TPaintBox;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  P := TPaintBox.Create(Form1);
  P.Top :=10;
  P.Left :=10;
  P.Height :=50;
  P.Width := 100;
  P.Parent := Form1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  p.Canvas.Pen.Color :=clRed;
  P.Canvas.Ellipse(10,10,20,20);
end;

end.

Note that I have changed the way you set the color.
1.0/2.6.0  XP SP3 & OS X 10.6.8

BlueIcaro

  • Hero Member
  • *****
  • Posts: 827
    • Blog personal
Re: How to create a PaintBox in runtime?
« Reply #8 on: April 12, 2011, 04:50:53 pm »
It's work!!
/BlueIcaro

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to create a PaintBox in runtime?
« Reply #9 on: April 12, 2011, 05:27:35 pm »
This works too:

Code: [Select]
  P := TPaintBox.Create(Form1);
  P.Top :=10;
  P.Left :=10;
  P.Height :=50;
  P.Width := 100;
  P.Parent := Form1;

  Application.ProcessMessages;

  p.Canvas.Pen.Color :=clRed;
  P.Canvas.Ellipse(10,10,20,20);

 

TinyPortal © 2005-2018