首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Winform 下無閃爍跑馬燈效果實現(二)

     編輯:關於C語言

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();
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved