c#自定义关闭,最小化和最大化按钮

本文关键字:最大化 按钮 最小化 自定义 | 更新日期: 2023-09-27 18:10:52

我一直在尝试创建自己的程序,使用自定义关闭最大化和最小化按钮(如Visual Studio或Word 2013等)…(我的边框样式设置为"None"))所以我一直在尝试做的是创建三个按钮。一个有关闭选项(可以正常工作),一个有最小化选项(也可以正常工作),一个有最大化按钮。单独最大化按钮工作得很好,但我想让它像标准的窗口按钮,这样当窗体最大化时,它会恢复窗体以前的状态(正常),我知道可以用

this.WindowState = FormWindowState.Normal;

如果你明白我的意思,它应该是一个按钮。我所尝试的是制作一个bool,当形式最大化时,其值设置为true(使用"if"语句),当形式未最大化时设置为false (else函数)。现在,当最大化按钮被点击的形式将最大化,因此布尔值将被设置为真,但当我再次点击,什么都没有发生!其他功能,如关闭和最小化工作很好,我甚至做了一个"恢复"按钮,这工作得很好!

感谢任何帮助,这是我的代码:

    bool restore;
    private void set_Restore()
    {
        {
            if (this.WindowState == FormWindowState.Maximized) //Here the "is" functions is
            {
                restore = true; //Sets the bool "restore" to true when the windows maximized
            }
            else
            {
                restore = false; //Sets the bool "restore" to false when the windows isn't maximized
            }
        }
    }
    private void MaximizeButton_Click(object sender, EventArgs e)
    {
        {
            if (restore == true)
            {
                this.WindowState = FormWindowState.Normal; //Restore the forms state
            }
            else
            {
                this.WindowState = FormWindowState.Maximized; //Maximizes the form
            }
        }
    }

嗯,我有三个警告,这是我认为是错误的:

"WindowsFormsApplication2.Form1

字段。"Restore"永远不会被赋值,它的默认值总是false。

我认为它说bool"restore"永远不会被使用,并且总是有它的默认值FALSE,这是不应该的,因为我的set_Restore当它被最大化。

另外两个警告是:

变量'restore'被赋值,但它的值从未被使用变量'restore'被赋值,但它的值从未被使用过

提前感谢。

c#自定义关闭,最小化和最大化按钮

您正在set_Restore()方法中创建一个新的本地恢复变量:

bool restore = true;

试着把它改成:

restore = true;

我甚至不认为这个变量是需要的。我认为你可以这样做:

private void MaximizeButton_Click(object sender, EventArgs e) {
  if (this.WindowState == FormWindowState.Maximized) {
    this.WindowState = FormWindowState.Normal;
  } else {
    this.WindowState = FormWindowState.Maximized;
  }
}