在Windows窗体中调整活动窗体的大小
本文关键字:窗体 活动 调整 Windows | 更新日期: 2023-09-27 18:13:59
这是游戏Catch me if you can。目前规模是静态的。我们只能在静态高度/宽度下玩游戏。我怎么能创建按钮去不同的坐标时,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));
}
如果我明白你想说什么,你可以在Point方法中传递表单的实际大小,像这样:
button1.Location = new Point(rnd.Next(12, this.Size.Height), rnd.Next(12, this.Size.Width));
祝你好运!
使用
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));
}
这样你就可以确保按钮永远不会超出表单的边界。如果你不从最大宽度和高度中减去按钮的宽度和高度,那么按钮的某些部分可能会超出表单的边界。