如果可以,这就是游戏抓住我。 目前尺寸是静态的。我们只能以静态高度/宽度玩游戏。 当WindowsForm的大小调整到最大尺寸时,如何创建按钮转到不同的坐标? 对不起解释,希望你理解:S
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.MouseEnter += button1_MouseEnter;
button1.BackColor = Color.AliceBlue;
button1.Text = "Noķer";
}
private void button1_Click(object sender, EventArgs e)
{
button1.MouseEnter -= button1_MouseEnter;
button1.Text = "gatavs";
button1.BackColor = Color.Aquamarine;
}
private void button1_MouseEnter(object sender, EventArgs e)
{
Random rnd = new Random();
button1.Location = new Point(rnd.Next(12, 197), rnd.Next(12, 226));
}
答案 0 :(得分:2)
如果我理解你想说什么,你可以在Point方法中传递实际的表单大小,如下所示:
button1.Location = new Point(rnd.Next(12, this.Size.Height), rnd.Next(12, this.Size.Width));
祝你好运!
答案 1 :(得分:1)
使用此:
private void button1_MouseEnter(object sender, EventArgs e)
{
Random rnd = new Random();
button1.Location = new Point(
rnd.Next(0, this.ClientRectangle.Width- this.button1.Width),
rnd.Next(0,this.ClientRectangle.Height- this.button1.Height));
}
这样您就可以确保按钮永远不会超出表格边框。如果您不从最大宽度和高度减去按钮宽度和高度,则按钮的某些部分可能会超出表格边框。