c#中当鼠标移动到Button上时Button随机移动到一个位置,程序怎么做,最好要代码,麻烦了(急)
2个回答
展开全部
private void Form1_MouseLeave(object sender, EventArgs e)
{
System.Random nr = new Random();
int x = nr.Next(this.Width - button1.Width * 2);
int y = nr.Next(this.Height - button1.Height * 2);
button1.Location = new Point(x, y);
}
{
System.Random nr = new Random();
int x = nr.Next(this.Width - button1.Width * 2);
int y = nr.Next(this.Height - button1.Height * 2);
button1.Location = new Point(x, y);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Button button3 = new Button();//定义按钮
public Form1()
{
InitializeComponent();
button3.Location = new System.Drawing.Point(422, 182);//初始位置
button3.Name = "button3";
button3.Size = new System.Drawing.Size(75, 23);//大小
button3.TabIndex = 5;
button3.Text = "button3";
button3.UseVisualStyleBackColor = true;
this.Controls.Add(button3);//框体 添加 BUTTON3
button3.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button3_MouseMove);//添加鼠标划过事件
}
private void button3_MouseMove(object sender, MouseEventArgs e)
{
Random a = new Random();
Random b = new Random();
int x = a.Next(1, 600);//600是界面宽
int y = a.Next(1, 300);//300是界面高,当然你可以随便设置
button3.Location = new System.Drawing.Point(x, y);//随机位置
这个程序应该粘贴进去直接成功,你要有点基础 应该怎么粘贴吧。
看我这么辛苦,给点分吧!
}
public Form1()
{
InitializeComponent();
button3.Location = new System.Drawing.Point(422, 182);//初始位置
button3.Name = "button3";
button3.Size = new System.Drawing.Size(75, 23);//大小
button3.TabIndex = 5;
button3.Text = "button3";
button3.UseVisualStyleBackColor = true;
this.Controls.Add(button3);//框体 添加 BUTTON3
button3.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button3_MouseMove);//添加鼠标划过事件
}
private void button3_MouseMove(object sender, MouseEventArgs e)
{
Random a = new Random();
Random b = new Random();
int x = a.Next(1, 600);//600是界面宽
int y = a.Next(1, 300);//300是界面高,当然你可以随便设置
button3.Location = new System.Drawing.Point(x, y);//随机位置
这个程序应该粘贴进去直接成功,你要有点基础 应该怎么粘贴吧。
看我这么辛苦,给点分吧!
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询