代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (button1.Top > Form1.ActiveForm.Size.Height || button1.Bottom < 0)
label1.Top =label1.Top-50;
else { label1.Top += 50; }
} private void button2_Click(object sender, EventArgs e)
{
if (button1.Left > Form1.ActiveForm.Size.Width || button1.Right < 0)
label1.Left -=50;
else { label1.Left += 50; }
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (button1.Top > Form1.ActiveForm.Size.Height || button1.Bottom < 0)
label1.Top =label1.Top-50;
else { label1.Top += 50; }
} private void button2_Click(object sender, EventArgs e)
{
if (button1.Left > Form1.ActiveForm.Size.Width || button1.Right < 0)
label1.Left -=50;
else { label1.Left += 50; }
}
if (label.Left > this.Width) label.Left = 0;
bool moveBottom = true;
private void button1_Click(object sender, EventArgs e)
{
if (moveBottom)
{
label1.Top += 50;
if (label1.Top > this.Height)
moveBottom = false;
}
else
{
label1.Top -= 50;
if (label1.Top < 0)
moveBottom = true;
}
}左右移动的仿着写写
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;namespace cc
{
public partial class Form1 : Form
{
static bool bFlag; //false是向下移,true是向上移 public Form1()
{
InitializeComponent();
bFlag = false; //初始时向下移
} private void btnStart_Click(object sender, EventArgs e)
{
int nTop; //label的x位置
int nHeith; //lagel的高度
nTop = lab1.Top;
nHeith = lab1.Height; if ( !bFlag)
{
//当前标识是false,向下移
if (nTop + nHeith + 20 > this.Height)
{
//如果这次移动后将超出窗体的下部,则改为向上移
nTop -= 20;
lab1.Top = nTop;
bFlag = true;
}
else
{
nTop += 20;
lab1.Top = nTop;
}
}
else
{
//当前标识是true,向上移
if (nTop - 20 < 0)
{
//本次向上移,将会超出窗体的上部,则改为向下移
nTop += 20;
lab1.Top = nTop;
bFlag = false;
}
else
{
nTop -= 20;
lab1.Top = nTop;
} } this.Text = "Form1" + this.Height.ToString() + " ; lab:" + nTop.ToString() + " ; "; }
}
}
{
Button btnStar;
Button btnStop;
Label lbl; int MoveSpeed = 12;
bool Direction = true;//true is right,false is left
bool IsRun = true; public Form1()
{
InitializeComponent();
this.Controls.Add(btnStar = new Button { Text = "Star" });
this.Controls.Add(btnStop = new Button { Text = "Stop", Top = 25 });
this.Controls.Add(lbl = new Label
{
Width = 20,
Height = 20,
Top = this.Height / 2,
Left = this.Width / 2,
BackColor = Color.Red
}); btnStop.Click += (s, e) => IsRun = false; btnStar.Click += (s, e) =>
{
IsRun = true;
new Thread(() =>
{
while (IsRun)
{
Thread.Sleep(60);
this.Invoke(new MethodInvoker(() => lbl.Left = Direction ? lbl.Left + MoveSpeed : lbl.Left - MoveSpeed)); if (lbl.Left < 0)
Direction = true;
if (lbl.Left + lbl.Width > this.Width)
Direction = false;
}
}).Start();
};
}
}
{
if (button1.Top > Form1.ActiveForm.Size.Height || button1.Bottom < 0)
label1.Top =label1.Top-50;
else { label1.Top += 50; }
}至少标红的地方 应该为label1移动的方向,只根据Label的当前位置是判断不出来的。如果位于窗体中间,可能是向上移动、也可能是向下移动。这个时候只能先约定一个方向。
每次只是在应该转向的时候 调转方向即可。