最近需要在Winform项目中实现一个走马灯的效果,一开始用了一些办法比如移动Label控件,效果总是不太好,移动文本时总有闪烁的现象。后来找了一个国外的开源控件,应用了一下,效果还不错。仔细阅读了一下代码,发现还有一些地方值得改进,现把代码以及改动说明贴出来,和大家分享。
控件出处:http://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 combinationof 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 considerun-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. |