开发中需要实现窗体文字滚动的效果。
思路:用Timer控件定时移动Label控件。父控件为Panel。详细代码如下:
privateSystem.Windows.Forms.TimertimeScroll;
/// <summary>
/// 滚动步长
/// </summary>
privateintpositon_falg =5;
/// <summary>
/// 设置滚动内容字体 时间间隔 步长
/// </summary>
privatevoidScrollSet()
{
this.lbl_Scroll.Font=newFont(“宋体”,20,FontStyle.Regular);
this.lbl_Scroll.Text = “滚动文字”;
timeScroll = newSystem.Windows.Forms.Timer();
timeScroll.Interval = 2000;
timeScroll.Tick += newEventHandler(timeScroll_Tick);
timeScroll.Start();
}
voidtimeScroll_Tick(objectsender, EventArgse)
{
if (this.lbl_Scroll.Location.X > 0 – this.lbl_Scroll.Width && this.lbl_Scroll.Location.X <= this.pl_Scroll.Width)
{
this.lbl_Scroll.Location = newPoint(this.lbl_Scroll.Location.X – positon_falg, this.lbl_Scroll.Location.Y);
}
else
{
this.lbl_Scroll.Location = newPoint(this.pl_Scroll.Size.Width, this.lbl_Scroll.Location.Y);
}
}