Winform 下无闪烁走马灯效果实现
作者:肖波
    最近需要在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 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.


I'm following up on the need for deprecating CS.DB and Control.DoubleBuffered's getter and will post here.

总之保险起见,还是判一下版本号,下面是判断代码
Winform 下无闪烁走马灯效果实现            Version v = System.Environment.Version;
Winform 下无闪烁走马灯效果实现
Winform 下无闪烁走马灯效果实现            
if (v.Major < 2)
            }

    2、刷新区域
    原代码中刷新区域是这样设置的
Winform 下无闪烁走马灯效果实现        private void Tick(object sender, EventArgs e)
        }

lastKnownRect是文字的整个区域,如果文字较长,这个刷新区域就会比较大,但实际上我们只需要刷新控件显示范围内的区域就可以了。
所以这里改动如下:

Winform 下无闪烁走马灯效果实现        //Controls the animation of the text.
Winform 下无闪烁走马灯效果实现
        private void Tick(object sender, EventArgs e)
        }

    2、修改Enabled属性
   
当Enabled设置为false时,原控件依然会滚动,觉得还是不让它滚动更好一些。
    修改代码如下:
Winform 下无闪烁走马灯效果实现        [
Winform 下无闪烁走马灯效果实现        Browsable(
true),
Winform 下无闪烁走马灯效果实现        CategoryAttribute(
"Behavior"),
Winform 下无闪烁走马灯效果实现        Description(
"Indicates whether the control is enabled")
Winform 下无闪烁走马灯效果实现        ]
Winform 下无闪烁走马灯效果实现        
new public bool Enabled
        }

下面给出修改后完整的控件代码,代码原作者为jconwell,原始代码见前面提到的控件出处

Winform 下无闪烁走马灯效果实现using System;
Winform 下无闪烁走马灯效果实现
using System.Collections;
Winform 下无闪烁走马灯效果实现
using System.ComponentModel;
Winform 下无闪烁走马灯效果实现
using System.Drawing;
Winform 下无闪烁走马灯效果实现
using System.Drawing.Drawing2D;
Winform 下无闪烁走马灯效果实现
using System.Data;
Winform 下无闪烁走马灯效果实现
using System.Windows.Forms;
Winform 下无闪烁走马灯效果实现
Winform 下无闪烁走马灯效果实现
namespace ScrollingTextControl

注意事项
如果要调整滚动速度,可以通过设置以下两个属性的值来实现
TextScrollSpeed 和 TextScrollDistance
TextScrollSpeed 其实是设置刷新频率,单位是毫秒,这个值越小,滚动速度越快。但刷新频率越高,CPU占用率越高。
TextScrollDistance 是指每次刷新移动的像素点,这个值越大,速度越快,但如果太大,文字滚动看起来就不是特别连贯。
所以在实际应用中我们需要同时调整这两个值,以找到最佳的平衡点



相关文章: