this.Location = new Point(3, 5);后面的3和5 你自己弄成随机的就可以了
???? 本身的窗体 那个THIS方法不大明白
那就和4楼说的那样,用个timer啊 不断的改变Location的值啊
private int screenWidth = 0; private int screenHeight = 0; private int x; private int y; public static int rx; public static int ry;private void Form1_Load(object sender, EventArgs e) { screenWidth = Screen.PrimaryScreen.Bounds.Width; screenHeight = Screen.PrimaryScreen.Bounds.Height; x = Location.X; y = Location.Y;
} private void timer1_Tick(object sender, EventArgs e) { Point p2 = new Point(x, y); this.Location = p2; Random ran = new Random(); rx = ran.Next(5, screenWidth); ry = ran.Next(5, screenHeight); if ((x >= rx - 2 && x <= rx + 2) || (y >= ry - 2 && y <= ry + 2)) if (rx < this.Location.X) x -= 1; else x += 1; if (ry < this.Location.Y) y -= 1; else y += 1; } private void button1_Click(object sender, EventArgs e) { this.timer1.Start(); }
和4楼说的那样,用timer 不断的改变Location的值
Random ra = new Random(); int width = ra.Next(Screen.PrimaryScreen.Bounds.Width - this.Width); int height = ra.Next(Screen.PrimaryScreen.Bounds.Height - this.Height); this.Location = new Point(width, height);重复这个动作,至于是用定时器,还是自己开个线程都可以
本身的窗体
那个THIS方法不大明白
private int screenHeight = 0;
private int x;
private int y;
public static int rx;
public static int ry;private void Form1_Load(object sender, EventArgs e)
{
screenWidth = Screen.PrimaryScreen.Bounds.Width;
screenHeight = Screen.PrimaryScreen.Bounds.Height;
x = Location.X;
y = Location.Y;
} private void timer1_Tick(object sender, EventArgs e)
{
Point p2 = new Point(x, y);
this.Location = p2;
Random ran = new Random();
rx = ran.Next(5, screenWidth);
ry = ran.Next(5, screenHeight);
if ((x >= rx - 2 && x <= rx + 2) || (y >= ry - 2 && y <= ry + 2))
if (rx < this.Location.X)
x -= 1;
else
x += 1;
if (ry < this.Location.Y)
y -= 1;
else
y += 1;
}
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
Random ra = new Random();
int width = ra.Next(Screen.PrimaryScreen.Bounds.Width - this.Width);
int height = ra.Next(Screen.PrimaryScreen.Bounds.Height - this.Height);
this.Location = new Point(width, height);重复这个动作,至于是用定时器,还是自己开个线程都可以