問題:系統登錄后,提示消息長度不固定,但空間有限
方案:用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