WinForm 实现文字滚动显示

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

方案:用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

 

posted @   我是四哥  阅读(1606)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示