如何使Windows Forms应用程序中的表单最大化到任何计算机屏幕?

26

我正在使用Visual Studio C#制作一款游戏,我希望在编译时自动将窗体最大化适应于任何用户的计算机屏幕。如何实现这个功能?


2
我确定你的意思是在运行时,而不是编译时。 - Jean-Bernard Pellerin
2
答案取决于你正在编写代码的平台。WPF?Silverlight?WinForms?等等。 - Muad'Dib
7个回答

38

你可以使用以下方法之一实现:

  1. 将表单的WindowState设置为FormWindowState.Maximized;
  2. 使用以下代码获取屏幕分辨率,并相应地设置表单的大小。

    int height = Screen.PrimaryScreen.Bounds.Height; 
    int width = Screen.PrimaryScreen.Bounds.Width;
    

4
窗口状态 = FormWindowState.Maximized; // 不是 WindowState=Maximized;对于 PrimaryScreen ,只有在一个显示器或其他所有显示器都没有更小的尺寸时才有效。 - gg89
如果窗口状态为最大化,您无需设置宽度和高度。这样就足够了。 - Fandango68

24

将您的窗体的WindowState属性设置为Maximized

这将导致在打开时您的窗体最大化显示。


1
此外,FormBorderStyle也可以设置为FormBorderStyle.None,以去除边框,获得更真实的最大化感觉,没有添加任何边框。 - Patrick

16
你可以使用this.WindowState = FormWindowState.Maximized;来最大化窗体。

7
  1. 打开“Go To Load Form As View Code”选项,使用以下代码:

C#:

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

VB:

Me.WindowState = System.Windows.Forms.FormWindowState.Maximized

这一行就是你所需要的。 - Fandango68

1
如果您正在寻找一种能够在第一次单击时最大化窗口并在第二次单击时将窗口规范化的工具,那么这篇文章可以帮助您。
private void maximiseButton_Click(object sender, EventArgs e)
    {

        //normalises window
        if (this.WindowState == FormWindowState.Maximized)
        {
            this.WindowState = FormWindowState.Normal;
            this.CenterToScreen();
        }

        //maximises window
        else
        {
            this.WindowState = FormWindowState.Maximized;
            this.CenterToScreen();
        }
    }

0

在VS2010中正确的写法:

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;


0
在表单移动事件中添加以下内容:
    private void Frm_Move (object sender, EventArgs e)
    {
        Top = 0; Left = 0;
        Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接