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) { } } 

}

  • Maybe the GraphicalOverlay component will help you, as described on codeproject.com/Articles/26071/Draw-Over-WinForms-Controls ? - Artyom
  • @Artyom has already invented, use the DrawLine () method; Where we indicate as the starting coordinates the current position of the button, and the final current + offset. Decision from the same area, thanks) - Dmitry Ushkevich
  • @DmitryUshkevich if you found the solution yourself, you can write the answer found in the answer and take it after a certain time. Just do not limit yourself to the text of your own comment, give a more detailed answer. - rdorn

0