最近需要在 Winform 專案中實現一個跑馬燈的效果,一開始用了一些辦法比如移動 Label 控制項,效果總是不太好,移動文字時總有閃爍的現象。後來找了一個國外的開源控制項,應用了一下,效果還不錯。仔細閱讀了一下程式碼,發現還有一些地方值得改進,現把程式碼以及改動說明貼出來,和大家分享。
控制項出處:https://www.codeproject.com/KB/miscctrl/ScrollingTextControlArtic.ASPx
我的改動:
1、DoubleBuffer 的設定
原程式碼中用的是 this.SetStyle(ControlStyles.DoubleBuffer, true); 但一些網友反映這個旗標在 .net 2.0 以上版本無效。說句老實話,我也不是特別確信,MSDN 上也沒有說明這一點。在我的 .Net 2.0 系統中,兩種設定的效果似乎沒有太多區別。在一個國外網站上找到他們的區別,下面是原文:
ControlStyles == CS
AllPaintingInWMPaint == APWMP
OptimizedDoubleBuffer = ODB
DoubleBuffer = DB
An earlier permutation of the design called for ODB to simply be a combination of DB, APWMP and UserPaint. Through several design changes, the two control styles are nearly synonymous, but they still have differences. Now that we've broken that, we may consider un-deprecating CS.DB to retain. Here is a more complete summary of the current design:
Mechanism Side effects Other flags required to work Require APWMP? Notes
ControlStyle.DB none APWMP, UserPaint Yes We are considering NOT deprecating this flag because ODB isn't an exact replacement for DB.
ControlStyle.ODB none none No Works, but without APWMP set you'll buffer foreground and background separately and will still see flicker.
Control.DoubleBuffered sets CS.ODB, CS.APWMP none No Works for most mainstream buffering needs. Getter is peculiar in that it only checks CS.ODB.