0

I am using Borland C++Builder 6.

I have two methods of the form:

void __fastcall FDisplay::PaintBox1Paint(TObject *Sender)
void __fastcall FDisplay::TimerLabelsViewTimer(TObject *Sender)

In the first method I draw the coordinate system.

and in the second method I did:

    PaintBox1->Canvas->MoveTo(693,201);
    PaintBox1->Canvas->LineTo(770,187);

and the line doesn't appear on the coordinate system.

my second question, how can I erase the line and return to the base paint? Should I do this?

PaintBox1->Invalidate();
PaintBox1->Update();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user687459
  • 143
  • 5
  • 17
  • PaintBox does not have Undo/Redo capability so you need to 1. implement it your self by copying the base gfx into some bitmap and then use that as start point for each redraw 2. repaint all in single draw routine (usually have single draw routine is better and also more safe) 3. if your line does not cross anything then you can simply draw the same line with background color before drawing the next one – Spektre Aug 13 '15 at 06:22
  • Invalidate and Update calls are not usually used directly. They are used by VCL to handle its own stuff. They are used to schedule/force `OnPaint` event and possibly more. – Spektre Aug 13 '15 at 06:26

1 Answers1

0

You must do ALL of the drawing inside of the OnPaint event handler. That includes your line drawing. Your OnTimer event handler cannot draw directly on the PaintBox, the drawing will be lost the next time the PaintBox is painted for any reason.

What you can do instead is have the OnTimer handler store the desired coordinates for the line drawing and then Invalidate() the PaintBox to signal a repaint. The OnPaint event can then draw the line at the stored coordinates. To erase the line, Invalidate() the PaintBox and simply don't draw the line.

For example:

private:
    TPoint lineStartPos;
    TPoint lineEndPos;

...

void __fastcall FDisplay::PaintBox1Paint(TObject *Sender)
{
    //...

    if (!lineStartPos.IsEmpty() && !lineEndPos.IsEmpty())
    {
        PaintBox1->Canvas->MoveTo(lineStartPos.x, lineStartPos.y);
        PaintBox1->Canvas->LineTo(lineEndPos.x, lineEndPos.y);
    }

    //...
}

void __fastcall FDisplay::TimerLabelsViewTimer(TObject *Sender)
{
    //...
    PaintBox1->Invalidate();
}

To draw the line:

lineStartPos = Point(693,201);
lineEndPos = Point(770, 187);
PaintBox1->Invalidate();

To erase the line:

lineStartPos = TPoint();
lineEndPos = TPoint();
PaintBox1->Invalidate();
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770