using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Timer = System.Timers.Timer; namespace WindowsFormsApp1.Frm { /// <summary> /// 跑马灯 /// </summary> public partial class RunHorse : Form { private static Timer timer = new Timer(); public RunHourse() { InitializeComponent(); timer.Interval = 50; } private void RunHorse_Load(object sender, EventArgs e) { timer.Elapsed += Timer_Elapsed; //初始化lbl位置 { var pWidth = this.panel1.Width; this.label1.Left = pWidth; this.label2.Left = pWidth; } //初始化lbl位置 { var pWidth = this.panel2.Width; this.label3.Left = pWidth; this.label4.Left = pWidth; } timer.Start(); } private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { timer.Stop(); //循环跑 this.panel1.InvokeOnUiThreadIfRequired(() => { var pWidth = this.panel1.Width; ; //lbl2准备好了,且lbl1还没跑完 if (this.label2.Left == this.panel1.Width && this.label1.Right > 0) this.label1.Left -= 10; else this.label1.Left = pWidth; //lbl1准备好了,且lbl2还没跑完 if (this.label1.Left == this.panel1.Width && this.label2.Right > 0) this.label2.Left -= 10; else this.label2.Left = pWidth; }); //即将结束时开始下一个 this.panel2.InvokeOnUiThreadIfRequired(() => { var pWidth = this.panel2.Width; if (this.label3.Right < 0) this.label3.Left = pWidth; if (this.label4.Right < 0) this.label4.Left = pWidth; //lbl1即将消失,且 lbl2准备好了 if (this.label3.Right < 80 && this.label4.Right > 0) { this.label3.Left -= 10; this.label4.Left -= 10; } //lbl2即将消失,且 lbl1准备好了 if (this.label4.Right < 80 && this.label3.Right > 0) { this.label3.Left -= 10; this.label4.Left -= 10; } //lbl2准备好了,且lbl1还没跑完 if (this.label4.Left == pWidth && this.label3.Right > 0) this.label3.Left -= 10; //lbl1准备好了,且lbl2还没跑完 if (this.label3.Left == pWidth && this.label4.Right > 0) this.label4.Left -= 10; }); } catch (Exception ex) { Console.WriteLine(ex + ""); } finally { timer.Start(); } } private void button1_Click(object sender, EventArgs e) { this.label1.Text = "0123456789"; } private void button2_Click(object sender, EventArgs e) { this.label2.Text = "ABCDEFGHIJK"; } } }