paintbox doesnt paint from timer method c++ builder borland

﹥>﹥吖頭↗ 提交于 2019-12-24 22:01:14

问题


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();

回答1:


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();


来源:https://stackoverflow.com/questions/31976785/paintbox-doesnt-paint-from-timer-method-c-builder-borland

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!