How do I make a WinForms app go Full Screen

前端 未结 9 1506
你的背包
你的背包 2020-11-22 11:20

I have a WinForms app that I am trying to make full screen (somewhat like what VS does in full screen mode).

Currently I am setting FormBorderStyle to <

9条回答
  •  时光说笑
    2020-11-22 11:50

    I don't know if it will work on .NET 2.0, but it worked me on .NET 4.5.2. Here is the code:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public partial class Your_Form_Name : Form
    {
        public Your_Form_Name()
        {
            InitializeComponent();
        }
    
        // CODE STARTS HERE
    
        private System.Drawing.Size oldsize = new System.Drawing.Size(300, 300);
        private System.Drawing.Point oldlocation = new System.Drawing.Point(0, 0);
        private System.Windows.Forms.FormWindowState oldstate = System.Windows.Forms.FormWindowState.Normal;
        private System.Windows.Forms.FormBorderStyle oldstyle = System.Windows.Forms.FormBorderStyle.Sizable;
        private bool fullscreen = false;
        /// 
        /// Goes to fullscreen or the old state.
        /// 
        private void UpgradeFullscreen()
        {
            if (!fullscreen)
            {
                oldsize = this.Size;
                oldstate = this.WindowState;
                oldstyle = this.FormBorderStyle;
                oldlocation = this.Location;
                this.WindowState = System.Windows.Forms.FormWindowState.Normal;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
                fullscreen = true;
            }
            else
            {
                this.Location = oldlocation;
                this.WindowState = oldstate;
                this.FormBorderStyle = oldstyle;
                this.Size = oldsize;
                fullscreen = false;
            }
        }
    
        // CODE ENDS HERE
    }
    

    Usage:

    UpgradeFullscreen(); // Goes to fullscreen
    UpgradeFullscreen(); // Goes back to normal state
    // You don't need arguments.
    

    Notice: You MUST place it inside your Form's class (Example: partial class Form1 : Form { /* Code goes here */ } ) or it will not work because if you don't place it on any form, code this will create an exception.

提交回复
热议问题