private void Runaway(object button) { ButtonAndMouse buttonAndMouse = (ButtonAndMouse)button; Button btn = buttonAndMouse.button; MouseEventArgs e = buttonAndMouse.mouse; int btnX = btn.Location.X + btn.Width / 2; int btnY = btn.Location.Y + btn.Height / 2; double len = Math.Sqrt((e.X - btnX) * (e.X - btnX) + (e.Y - btnY) * (e.Y - btnY)); if (len < distance) { int newX = (int)((btnX - e.X) / len * distance + e.X) - btn.Width / 2; int newY = (int)((btnY - e.Y) / len * distance + e.Y) - btn.Height / 2; if (newX >= this.ClientSize.Width - btn.Width) { newX = 0; } if (newX < 0) { newX = ClientSize.Width - btn.Width; } if (newY >= this.ClientSize.Height - btn.Height) { newY = 24; } if (newY < 24) { newY = ClientSize.Height - btn.Height; } btn.Location = new Point(newX, newY); } }
private void Form1_MouseMove(object sender, MouseEventArgs e) { for (int i = 0; i < buttons.Count; i++) { ButtonAndMouse buttonAndMouse = new ButtonAndMouse { button = buttons[i], mouse = e }; Thread thread = new Thread(Runaway); thread.Start(buttonAndMouse); } }