122

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 None and WindowState to Maximized which gives me a little more space, but it doesn't cover over the taskbar if it is visible.

What do I need to do to use that space as well?

For bonus points, is there something I can do to make my MenuStrip autohide to give up that space as well?

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34

11 Answers11

170

To the base question, the following will do the trick (hiding the taskbar)

private void Form1_Load(object sender, EventArgs e)
{
    this.TopMost = true;
    this.FormBorderStyle = FormBorderStyle.None;
    this.WindowState = FormWindowState.Maximized;
}

But, interestingly, if you swap those last two lines the Taskbar remains visible. I think the sequence of these actions will be hard to control with the properties window.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 4
    The ordering issue is why it wasn't working for me before. I was actually setting the properties in that order, but when the form is already maximized setting the border to none doesn't expand to cover the taskbar. I worked around by "restoring" the form changing the border and then maximizing. –  Feb 03 '09 at 22:14
  • 3
    I have it in the right order and it doesn't work. Taskbar is always visible and the app is not below it, it just left free spave for taskbar there. (Win7) – Preza8 Jul 31 '13 at 17:36
  • @Preza8 - read Grady's comment, check if that applies to your situation. – H H Aug 02 '13 at 20:08
  • 1
    I'm sorry, I haven't been online here for a long time and I forgot how I did it but I remember that after some trying of random order of those commands helps. – Preza8 Sep 18 '13 at 12:37
  • Note: For some reason I had to set the properties AND put this in the code – Joe Phillips Jun 06 '17 at 04:34
24

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?)

Community
  • 1
  • 1
Zignd
  • 6,896
  • 12
  • 40
  • 62
  • 2
    Out of curiosity, why do you have a whole enum to describe a true/false condition? – Nathan Ridley Jun 08 '15 at 04:25
  • 2
    I wrote this a long time ago when I was only grasping to write code, please mind my young dumbness. It indeed makes no sense at all and I should have simply used a boolean type. – Zignd Jun 09 '15 at 07:38
  • 1
    It worked for me, and I had to set `targetForm.WindowState = FormWindowState.Normal;` at the beginning of leaving full screen as well. That for handling the case where the user is going full screen from a maximized window. – gneri Jun 12 '18 at 12:24
6

And for the menustrip-question, try set

MenuStrip1.Parent = Nothing

when in fullscreen mode, it should then disapear.

And when exiting fullscreenmode, reset the menustrip1.parent to the form again and the menustrip will be normal again.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Stefan
  • 11,423
  • 8
  • 50
  • 75
5

You can use the following code to fit your system screen and task bar is visible.

    private void Form1_Load(object sender, EventArgs e)
    {   
        // hide max,min and close button at top right of Window
        this.FormBorderStyle = FormBorderStyle.None;
        // fill the screen
        this.Bounds = Screen.PrimaryScreen.Bounds;
    }

No need to use:

    this.TopMost = true;

That line interferes with alt+tab to switch to other application. ("TopMost" means the window stays on top of other windows, unless they are also marked "TopMost".)

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
4

I recently made a Mediaplayer application and I used API calls to make sure the taskbar was hidden when the program was running fullscreen and then restored the taskbar when the program was not in fullscreen or not had the focus or was exited.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer

Sub HideTrayBar()
    Try


        Dim tWnd As Integer = 0
        Dim bWnd As Integer = 0
        tWnd = FindWindow("Shell_TrayWnd", vbNullString)
        bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
        ShowWindow(tWnd, 0)
        ShowWindow(bWnd, 0)
    Catch ex As Exception
        'Error hiding the taskbar, do what you want here..'
    End Try
End Sub
Sub ShowTraybar()
    Try
        Dim tWnd As Integer = 0
        Dim bWnd As Integer = 0
        tWnd = FindWindow("Shell_TrayWnd", vbNullString)
        bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
        ShowWindow(bWnd, 1)
        ShowWindow(tWnd, 1)
    Catch ex As Exception
        'Error showing the taskbar, do what you want here..'
    End Try


End Sub
Tridib Roy Arjo
  • 26
  • 2
  • 10
Stefan
  • 11,423
  • 8
  • 50
  • 75
  • 7
    What if two programs did this? What if your program crashes before it gets a chance to unhide the taskbar? – Tmdean Feb 03 '09 at 17:53
  • @Tmdean: In my case it was not an problem, this program was running on mashines that was dedicated to run only my program on screens in waitingrooms. – Stefan Feb 03 '09 at 19:03
  • @Tmdean: The point about if two programs did this is valid, it could mess things up if not handled correct. – Stefan Feb 03 '09 at 19:07
4

I worked on Zingd idea and made it simpler to use.

I also added the standard F11 key to toggle fullscreen mode.

Setup

Everything is now in the FullScreen class, so you don't have to declare a bunch of variables in your Form. You just instanciate a FullScreen object in your form's constructor :

FullScreen fullScreen;

public Form1()
{
    InitializeComponent();
    fullScreen = new FullScreen(this);
}

Please note this assumes the form is not maximized when you create the FullScreen object.

Usage

You just use one of the classe's functions to toggle the fullscreen mode :

fullScreen.Toggle();

or if you need to handle it explicitly :

fullScreen.Enter();
fullScreen.Leave();

Code

using System.Windows.Forms;


class FullScreen
{ 
    Form TargetForm;

    FormWindowState PreviousWindowState;

    public FullScreen(Form targetForm)
    {
        TargetForm = targetForm;
        TargetForm.KeyPreview = true;
        TargetForm.KeyDown += TargetForm_KeyDown;
    }

    private void TargetForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.F11)
        {
            Toggle();
        }
    }

    public void Toggle()
    {
        if (TargetForm.WindowState == FormWindowState.Maximized)
        {
            Leave();
        }
        else
        {
            Enter();
        }
    }
        
    public void Enter()
    {
        if (TargetForm.WindowState != FormWindowState.Maximized)
        {
            PreviousWindowState = TargetForm.WindowState;
            TargetForm.WindowState = FormWindowState.Normal;
            TargetForm.FormBorderStyle = FormBorderStyle.None;
            TargetForm.WindowState = FormWindowState.Maximized;
        }
    }
      
    public void Leave()
    {
        TargetForm.FormBorderStyle = FormBorderStyle.Sizable;
        TargetForm.WindowState = PreviousWindowState;
    }
}
geriwald
  • 190
  • 1
  • 4
  • 17
2

You need to set your window to be topmost.

Tron
  • 1,397
  • 10
  • 11
  • 1
    No dice. Even if I set the window to be topmost it doesn't cover up the taskbar. –  Feb 02 '09 at 23:07
  • 3
    Try: Bounds = Screen.PrimaryScreen.Bounds; http://www.codeproject.com/KB/cs/scrframework.aspx has more details, like for multimon – Tron Feb 02 '09 at 23:29
1

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;
    /// <summary>
    /// Goes to fullscreen or the old state.
    /// </summary>
    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.

1

On the Form Move Event add this:

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);
}
Segan
  • 486
  • 5
  • 5
1

If you want to keep the border of the form and have it cover the task bar, use the following:

Set FormBoarderStyle to either FixedSingle or Fixed3D

Set MaximizeBox to False

Set WindowState to Maximized

Quinton
  • 11
  • 1
0

Need to display a window in "real full screen" mode, including when there are several monitors? It's very simple and all in one line:

this.DesktopBounds = SystemInformation.VirtualScreen;

To be placed in the constructor of the window for example.

Be careful if you set this.TopMost = True because the window will cover the taskbar which will therefore be inaccessible.

damien
  • 54
  • 4