//线程执行此方法
//以下代码是移动一个button,默认向左移动,当移到窗体边缘 开始向右移动
private void ControlMove()
{
int num = 1;
bool IsLeftRight = false; //false 左移动 / true 右移动
while (true)
{
Size size = this.Size;
Thread.Sleep(30);
int left = this.button1.Left;
int right = size.Width - this.button1.Width - left;
if (!IsLeftRight)
{
if (left > 0 && left >= num)
{
this.Invoke((MethodInvoker)delegate
{
this.button1.Left = this.button1.Left - num;
});
}
else
{
IsLeftRight = true;
}
}
else
{
if (right > 0 && right >= num)
{
this.Invoke((MethodInvoker)delegate
{
this.button1.Left = this.button1.Left + num;
});
}
else
{
IsLeftRight = false;
}
}
}
}