C++ Builder - Problem with TShape component
I have this piece of code:
TShape* T[256];
/* Other code ... */
for (int i = 255; i > 0; i--) {
T[i]->Brush->Color = T[i - 1]->Brush->Color;
T[i]->Pen->Color = T[i - 1]->Pen->Color;
};
The cycle is executed by a TTimer each 100 milliseconds and the Color of the first TShape change each 100 milliseconds.
开发者_高级运维During this cycle, I see a blinking white horizontal lines, because before receiving the color of the other TShape, each TShape is invalidated and becomes white.
Is there a way to avoid this behaviour? Maybe, I must override some method?
I think double buffering is the key to your problem. If you are using C++Builder 2009 or newer probably setting property Controls::TWinControl::DoubleBuffered for your current frame will be enough.
TShape invalidates itself every time you change its Brush and Pen properties, so your loop is double-invalidating each TShape. As a workaround, try temporarily removing the OnChange event handlers that TShape assigns internally, and then Invalidate() the TShape only once after you have finished updating it. For example:
for (int i = 255; i > 0; i--)
{
TNotifyEvent OldBrushChange = T[i]->Brush->OnChange;
T[i]->Brush->OnChange = NULL;
TNotifyEvent OldPenChange = T[i]->Pen->OnChange;
T[i]->Pen->OnChange = NULL;
T[i]->Brush->Color = T[i - 1]->Brush->Color;
T[i]->Pen->Color = T[i - 1]->Pen->Color;
T[i]->Brush->OnChange = OldBrushChange;
T[i]->Pen->OnChange = OldPenChange;
T[i]->Invalidate();
};
精彩评论