Certainly you need to remove the call to Form2->Refresh
. That asks the form to repaint itself immediately. That cannot help.
Secondly, your code handles the OnPaint
event of a TPaintBox
control. You are expected to paint on the canvas of TPaintBox
rather than the form's canvas. Change all references to this->Canvas
to instead refer to PaintBox1->Canvas
. You may also need to correct the coordinates used to when painting.
Alternatively you could keep your existing code, and remove the paint box altogether. In which case take your current code and attach it to the form's OnPaint
handler instead.
As for double buffering, you could let the VCL do it for you. Set the form's DoubleBuffered
property to true
. That is all you need to do, although be warned that it can have undesirable consequences on the visual appearance of certain control in certain themes. Be alert for any problems.
If you want to do the double buffering your self it is easy enough. Create a TBitmap
. Set its size appropriately. Draw your lines to the Canvas
of the bitmap. Then draw the bitmap onto the paint box canvas, or the form's canvas.