You can paint on a TPaintbox anywhere from within your program where the paintbox is in scope.
Try the following - this will draw random lines or circles, depending on which button you click:
Create a new project. On Form1, put a TPaintbox, a TColorListBox, a Button1 and a Button2.
Change caption for Button1 to 'Random Line', and caption for Button2 to 'Random Circle'.
Assign the following code to the OnClick events of the buttons:
procedure TForm1.Button1Click(Sender: TObject);
var x1,y1,x2,y2,c:integer;
aColor:TColor;
begin
x1:=random(PaintBox1.Width);
x2:=random(PaintBox1.Width);
y1:=random(PaintBox1.Height);
y2:=random(PaintBox1.Height);
c:=random(colorlistbox1.Items.Count);
aColor:=ColorListBox1.Colors[c];
With Paintbox1 do
begin
canvas.pen.Color :=aColor;
canvas.Line(x1,y1,x2,y2);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var x,y,r,c:integer;
aColor:TColor;
begin
x:=random(PaintBox1.Width);
y:=random(PaintBox1.Height);
r:=random(PaintBox1.Height div 3);
c:=random(colorlistbox1.Items.Count);
aColor:=ColorListBox1.Colors[c];
With Paintbox1 do
begin
canvas.pen.Color :=aColor;
canvas.Brush.Color := aColor;
canvas.EllipseC(x,y,r,r) ;
end;
end;
Compile & Run. Each time you click a button, something will happen on the paintbox. I was just lazy and used the TColorListBox to get a random range of colours.
You can even add a timer, and in it's OnTimer event add the same.
Not a very practical example, but it might help you.