Friends, wrote the program "Racing buttons", tell me what to do, so that each button follows a trace (line):
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; namespace gonki { public delegate void Helper(Button btn); //ΠΠΎΠΌΠΎΡΠ½ΠΈΠΊ Π΄Π»Ρ ΠΌΠ°Π½ΠΈΠΏΡΠ»ΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡΠ°ΠΌΠΈ ΠΈΠ· ΡΠ°Π·Π½ΡΡ
ΠΏΠΎΡΠΎΠΊΠΎΠ² public partial class MainForm : Form { Thread t1; // ΠΏΠΎΡΠΎΠΊ Π΄Π»Ρ Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΡ 1 ΠΊΠ½ΠΎΠΏΠΊΠΈ Thread t2; // ΠΏΠΎΡΠΎΠΊ Π΄Π»Ρ Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΡ 2 ΠΊΠ½ΠΎΠΏΠΊΠΈ Thread t3; // ΠΏΠΎΡΠΎΠΊ Π΄Π»Ρ Π΄Π²ΠΈΠΆΠ΅Π½ΠΈΡ 3 ΠΊΠ½ΠΎΠΏΠΊΠΈ Random r; // Π΄Π»Ρ ΡΠ»ΡΡΠ°ΠΉΠ½ΠΎΠΉ ΡΠΊΠΎΡΠΎΡΡΠΈ ΠΊΠ½ΠΎΠΏΠΎΠΊ Button[] button; //ΠΠ°ΡΡΠΈΠ² ΠΊΠ½ΠΎΠΏΠΎΠΊ Helper helper; public MainForm() { InitializeComponent(); button = new ButtonCompare[] { first_btn, second_btn, third_btn }; helper = new Helper(Motion); r = new Random(); } private void start_Click(object sender, EventArgs e) { pause.Enabled = true; stop.Enabled = true; ((Button)sender).Enabled = false; if (t1 != null) //ΠΡΠ»ΠΈ ΠΏΠΎΡΠΎΠΊΠΈ ΡΡΡΠ΅ΡΡΠ²ΡΡΡ, Π½ΠΎ ΠΏΡΠΈΠΎΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½Ρ Π²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ { t1.Resume(); t2.Resume(); t3.Resume(); return; } t1 = new Thread(Movement1); t2 = new Thread(Movement2); t3 = new Thread(Movement3); t1.IsBackground = t2.IsBackground = t3.IsBackground = true; t1.Start(); t2.Start(); t3.Start(); } void Motion(Button button) { Going(button); Lider(); Finish(button); } private void Going(Button button) { button.Location = new Point(button.Location.X + r.Next(10), button.Location.Y); } private void Finish(Button button) { if (button.Location.X > pictureBox1.Location.X - button.Width) { pause_Click(new object { }, new EventArgs { }); start.Enabled = false; } } private void Lider() { Array.Sort(button); button[0].BackColor = Color.Yellow; for (int i = 1; i < button.Length; i++) button[i].BackColor = SystemColors.Control; } public void Movement1() { while(true) { Thread.Sleep(100); Invoke(helper, first_btn); } } public void Movement2() { while (true) { Thread.Sleep(100); Invoke(helper, second_btn); } } public void Movement3() { while (true) { Thread.Sleep(100); Invoke(helper, third_btn); } } private void pause_Click(object sender, EventArgs e) { pause.Enabled = false; start.Enabled = true; t1.Suspend(); t2.Suspend(); t3.Suspend(); } private void stop_Click(object sender, EventArgs e) { pause.Enabled = false; stop.Enabled = false; pause_Click(sender,e); Reset(); } private void Reset() { first_btn.Location = new Point(100, first_btn.Location.Y); second_btn.Location = new Point(100, second_btn.Location.Y); third_btn.Location = new Point(100, third_btn.Location.Y); foreach (ButtonCompare btn in button) btn.BackColor = SystemColors.Control; } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { stop_Click(sender, new EventArgs()); } private void pictureBox1_Click(object sender, EventArgs e) { } }
}
GraphicalOverlay
component will help you, as described on codeproject.com/Articles/26071/Draw-Over-WinForms-Controls ? - Artyom