zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#实现跑马灯效果的示例代码分享给大家

c#代码 实现 示例 分享 效果 大家 跑马灯
2023-06-13 09:14:41 时间

字幕效果功能效果大家都应该知道就是当我们的文字太长,整个页面放不下的时候通常公告等),它可以自动来回滚动,让客户可以看到完整信息虽然需要时间有点)。

对于Winform这种技术来说,要实现任何动态效果比较麻烦的。而且一般都需要用定时器当然这个时候写的字幕效果一样的,用System。计时器。定时器的实现关于其他定时器和用法以前文章有写过,有兴趣可以转一下

public partial class CustomLable : Label

{

System.Timers.Timer timer = new System.Timers.Timer(200);

int offset = 5;//偏移量

PointF textPoint;

public CustomLable()

{

InitializeComponent();

textPoint = new PointF(this.Width, 0);

timer.Elapsed += (s, e) =>

{

try

{

if (!IsDisposed)

{

Graphics g = CreateGraphics();

SizeF textSize = g.MeasureString(Text, Font);

textPoint.X -= offset;

if (textPoint.X <= -textSize.Width)

{

textPoint.X = Width;

}

g.Clear(BackColor);

g.DrawString(Text,Font, new SolidBrush(ForeColor), textPoint);

}

}

catch { }

};

}

protected override void OnPaint(PaintEventArgs pe)

{

base.OnPaint(pe);

}

private bool _IsMarquee;

[Browsable(true)]

[Description("是否以跑马灯效果显示")]

public bool IsMarquee

{

get { return _IsMarquee; }

set

{

_IsMarquee = value;

Marquee();

}

}

public void Marquee()

{

if (IsMarquee)

{

timer.Start();

}

else

{

timer.Stop();

textPoint = new PointF(0, 0);

try

{

if (!IsDisposed)

{

Graphics g = CreateGraphics();

g.Clear(BackColor);

g.DrawString(Text, Font, new SolidBrush(ForeColor), textPoint);

}

}

}

}

}