I'm following up on the need for deprecating CS.DB and Control.DoubleBuffered's getter and will post here.
總之保險起見,還是判一下版本號,下面是判斷代碼
Version v = System.Environment.Version;
if (v.Major < 2)
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
}
else
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
2、刷新區域
原代碼中刷新區域是這樣設置的
private void Tick(object sender, EventArgs e)
{
//update rectangle to include where to paint for new position
//lastKnownRect.X -= 10;
//lastKnownRect.Width += 20;
lastKnownRect.Inflate(10, 5);
//create region based on updated rectangle
Region updateRegion = new Region(lastKnownRect);
//repaint the control
Invalidate(updateRegion);
Update();
}
lastKnownRect是文字的整個區域,如果文字較長,這個刷新區域就會比較大,但實際上我們只需要刷新控件顯示范圍內的區域就可以了。
所以這裡改動如下:
//Controls the animation of the text.
private void Tick(object sender, EventArgs e)
{
//update rectangle to include where to paint for new position
//lastKnownRect.X -= 10;
//lastKnownRect.Width += 20;
lastKnownRect.Inflate(10, 5);
//get the display rectangle
RectangleF refreshRect = lastKnownRect;
refreshRect.X = Math.Max(0, lastKnownRect.X);
refreshRect.Width = Math.Min(lastKnownRect.Width + lastKnownRect.X, this.Width);
refreshRect.Width = Math.Min(this.Width - lastKnownRect.X, refreshRect.Width);
//create region based on updated rectangle
//Region updateRegion = new Region(lastKnownRect);
Region updateRegion = new Region(refreshRect);
//repaint the control
Invalidate(updateRegion);
Update();
}