Board index » delphi » Can't paint in TPaintBox

Can't paint in TPaintBox

Hi,
    I'm trying to paint into a TPaintBox created at runtime using the
following code.  The line I'm drawing appears for a fraction of a
second, and then disappears!  Why?  Everything works fine when I define
the paintbox at design time.

procedure TForm1.Button1Click(Sender: TObject);

var Box:  TPaintBox;

begin
    Box := TPaintBox.Create(self);
    Box.Parent := Form1;
    Box.Top := 100;
    Box.Left := 100;
    Box.Width := 102;
    Box.Height := 77;
    Box.Canvas.MoveTo(10,10);
    Box.Canvas.LineTo(50,50);
end;

Cheers,
Simon

 

Re:Can't paint in TPaintBox


Simon,
  I cannot say why this is happening, but I can tell you that swapping out a
Timage for the TpaintBox seems to be a viable alternative.

Good Luck!!
    -Dave
Inprise Certified Delphi 4 Client/Server developer
http://www.erols.com/dparsons

Quote
Simon Figg wrote in message <36EFE2B9.FDBB8...@iol.ie>...
>Hi,
>    I'm trying to paint into a TPaintBox created at runtime using the
>following code.  The line I'm drawing appears for a fraction of a
>second, and then disappears!  Why?  Everything works fine when I define
>the paintbox at design time.

>procedure TForm1.Button1Click(Sender: TObject);

>var Box:  TPaintBox;

>begin
>    Box := TPaintBox.Create(self);
>    Box.Parent := Form1;
>    Box.Top := 100;
>    Box.Left := 100;
>    Box.Width := 102;
>    Box.Height := 77;
>    Box.Canvas.MoveTo(10,10);
>    Box.Canvas.LineTo(50,50);
>end;

>Cheers,
>Simon

Re:Can't paint in TPaintBox


Quote
On Wed, 17 Mar 1999 17:13:29 +0000, Simon Figg <sf...@iol.ie> wrote:
>    I'm trying to paint into a TPaintBox created at runtime using the
>following code.  The line I'm drawing appears for a fraction of a
>second, and then disappears!  Why?  Everything works fine when I define
>the paintbox at design time.

To paint in a paint box, you must put the paint code in its OnPaint
handler. That's how TPaintBox works.
--
Ray Lischner  (http://www.bardware.com)
co-author (with John Doyle) of Shakespeare for Dummies

Re:Can't paint in TPaintBox


Very simple...
Tip of the day (to you and a lot of newbies here) : it's time to learn a bit
the underlying OS you're developing on...
(not offevensive remark, just to help you).

Under Windows, each windows must know how to paint itself.
When a window covers another one, and when the top window is moved, how do
think Windows does to display again the part of the previously hidden window
?
Do you really think Windows keeps a bitmap of all displayed windows ? On a
1027x768 screen, with 16,7 million colors, if it was acting like this, 10
open windows will eat all the memory of a basic PC !
(And remember Windows for first design to work with 1 Mo under 286
processor, but perhaps are you too young to know this).

So, each window must be able to draw itself in answering to the WM_Paint
message. all visible components have a Paint method that is called when this
message is sent to their window handle.

Now, if you directly draw some lines onto a canvas (paintbox or anything
else), this is your responsability to redraw all the stuff when Windows ask
for it.

Other Threads