c#中当鼠标移动到Button上时Button随机移动到一个位置,程序怎么做...
发布网友
发布时间:2023-12-24 08:09
我来回答
共2个回答
热心网友
时间:2024-07-22 10:49
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);
}
热心网友
时间:2024-07-22 10:48
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);//随机位置
这个程序应该粘贴进去直接成功,你要有点基础 应该怎么粘贴吧。
看我这么辛苦,给点分吧!
}