程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Winform下無閃爍走馬燈效果實現(2)

Winform下無閃爍走馬燈效果實現(2)

編輯:關於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