How do I make a WinForms app go Full Screen

前端 未结 9 1492
你的背包
你的背包 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:54

    A tested and simple solution

    I've been looking for an answer for this question in SO and some other sites, but one gave an answer was very complex to me and some others answers simply doesn't work correctly, so after a lot code testing I solved this puzzle.

    Note: I'm using Windows 8 and my taskbar isn't on auto-hide mode.

    I discovered that setting the WindowState to Normal before performing any modifications will stop the error with the not covered taskbar.

    The code

    I created this class that have two methods, the first enters in the "full screen mode" and the second leaves the "full screen mode". So you just need to create an object of this class and pass the Form you want to set full screen as an argument to the EnterFullScreenMode method or to the LeaveFullScreenMode method:

    class FullScreen
    {
        public void EnterFullScreenMode(Form targetForm)
        {
            targetForm.WindowState = FormWindowState.Normal;
            targetForm.FormBorderStyle = FormBorderStyle.None;
            targetForm.WindowState = FormWindowState.Maximized;
        }
    
        public void LeaveFullScreenMode(Form targetForm)
        {
            targetForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            targetForm.WindowState = FormWindowState.Normal;
        }
    }
    

    Usage example

        private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FullScreen fullScreen = new FullScreen();
    
            if (fullScreenMode == FullScreenMode.No)  // FullScreenMode is an enum
            {
                fullScreen.EnterFullScreenMode(this);
                fullScreenMode = FullScreenMode.Yes;
            }
            else
            {
                fullScreen.LeaveFullScreenMode(this);
                fullScreenMode = FullScreenMode.No;
            }
        }
    

    I have placed this same answer on another question that I'm not sure if is a duplicate or not of this one. (Link to the other question: How to display a Windows Form in full screen on top of the taskbar?)

提交回复
热议问题