首页 > 技术文章 > WinForm 实现文字滚动显示

czzl 2021-09-14 22:48 原文

问题:系统登录后,提示消息长度不固定,但空间有限

方案:用Timer控件定时移动Label控件。父控件为Panel。

关键点:控件的Location属性,是相对于容器的位置。

 

private int positon_flag = 1;
private void timer_Tick(object sender, EventArgs e)
{
//当消息内容小于容器长度,timer停止工作,文字固定
if (lac_loginName.Width <= panel7.Width- Math.Abs(lac_loginName.Location.X))
{
  timer.Stop();
}

if (lac_loginName.Location.X > ( - lac_loginName.Width) && this.lac_loginName.Location.X <= this.lac_loginName.Width)
{
  this.lac_loginName.Location = new Point(this.lac_loginName.Location.X - positon_flag, this.lac_loginName.Location.Y);
}
else
{
//当消息尾移动到容器左边缘,设置消息坐标至容器最右侧
  this.lac_loginName.Location = new Point(panel7.Width, this.lac_loginName.Location.Y); } }

 

参考文章:https://www.codebye.com/winform-c-to-achieve-the-text-scrolling-display.html

 

推荐阅读