How to make Windows Form app truly Full Screen (and to hide Taskbar) in C#?

One of sound-like-simple questions is “how to make your application truly Full Screen” i.e. not showing Taskbar or anything like that.

Initial approach is obvious:


    targetForm.WindowState = FormWindowState.Maximized;
    targetForm.FormBorderStyle = FormBorderStyle.None;
    targetForm.TopMost = true;

Does it work? Well, sort of. If your Taskbar have default setting unchecked for “Keep the taskbar on top of other windows”, this will present your application in all it’s glory all over screen estate.

However, if the Taskbar is set to appear on top of all others, this won’t help – your application won’t cover it.

Let’s go further – next step is to use P/Invoke and to engage Win32 API services. There is easy way to hide particular window. So, find the Taskbar and hide it:


    private const int SW_HIDE = 0;
    private const int SW_SHOW = 1;

    [DllImport("user32.dll")]
    private static extern int FindWindow(string className, string windowText);
    [DllImport("user32.dll")]
    private static extern int ShowWindow(int hwnd, int command);

    int hWnd = FindWindow("Shell_TrayWnd", "");
    ShowWindow(hWnd, SW_HIDE);

    targetForm.WindowState = FormWindowState.Maximized;
    targetForm.FormBorderStyle = FormBorderStyle.None;
    targetForm.TopMost = true;

(you need to add using System.Runtime.InteropServices;)

Is this better? In theory yes – Taskbar is hidden, but your application still does not occupy whole screen – place where Taskbar was is not used.

Real and proven solution is to make request to WinAPI that your form take whole screen estate – Taskbar will hide itself in that case. Full information about that can be found in KB Article Q179363: How To Cover the Task Bar with a Window and here is the code:


/// <summary>
/// Selected Win AI Function Calls
/// </summary>

public class WinApi
{
    [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
    public static extern int GetSystemMetrics(int which);

    [DllImport("user32.dll")]
    public static extern void 
        SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                     int X, int Y, int width, int height, uint flags);        
    
    private const int SM_CXSCREEN = 0;
    private const int SM_CYSCREEN = 1;
    private static IntPtr HWND_TOP = IntPtr.Zero;
    private const int SWP_SHOWWINDOW = 64; // 0x0040
    
    public static int ScreenX
    {
        get { return GetSystemMetrics(SM_CXSCREEN);}
    }
    
    public static int ScreenY
    {
        get { return GetSystemMetrics(SM_CYSCREEN);}
    }
    
    public static void SetWinFullScreen(IntPtr hwnd)
    {
        SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
    }
}

/// <summary>
/// Class used to preserve / restore state of the form
/// </summary>
public class FormState
{
    private FormWindowState winState;
    private FormBorderStyle brdStyle;
    private bool topMost;
    private Rectangle bounds;

    private bool IsMaximized = false;

    public void Maximize(Form targetForm)
    {
        if (!IsMaximized)
        {
            IsMaximized = true;
            Save(targetForm);
            targetForm.WindowState = FormWindowState.Maximized;
            targetForm.FormBorderStyle = FormBorderStyle.None;
            targetForm.TopMost = true;
            WinApi.SetWinFullScreen(targetForm.Handle);
        }
    }
    
    public void Save(Form targetForm)
    {
        winState = targetForm.WindowState;
        brdStyle = targetForm.FormBorderStyle;
        topMost = targetForm.TopMost;
        bounds = targetForm.Bounds;
    }

    public void Restore(Form targetForm)
    {
        targetForm.WindowState = winState;
        targetForm.FormBorderStyle = brdStyle;
        targetForm.TopMost = topMost;
        targetForm.Bounds = bounds;
        IsMaximized = false;
    }
}

Code for example application is here: MaxWinForm.zip

51 thoughts on “How to make Windows Form app truly Full Screen (and to hide Taskbar) in C#?”

  1. Hi!
    just wanted to say your arcticle was very helpful and provided exactly the solution I needed 🙂 Thanks
    Frank

  2. Hi.
    Nice post. However I would like to know for a Windows Mobile CE 5.0 device application, can i use the same code? I tried to run the code on a device application but it does not work. Any help?

  3. I’ve found a simpler method of ‘doing’ full screen in C# that doesn’t require any messing about with Windows APIs. All you have to do is set your Form’s FormBorderStyle to None, and then position your Form at 0,0 (using the Location property) and resize it (using the Size property) to the dimensions of your screen (you can get this info from the System.Windows.Forms.Screen class). This seems to do the job nicely, and will hide the task bar – and crucially its easy and an ‘official C#’ way around the problem. I’d be interested to know if this works for everyone else too and if anyone finds problems with it?

  4. Hi Alex,

    I tried the way you suggested on a Windows Mobile Ce 5.0. I have set the FormBorStyle to None and set Location to 0,0. In the load event handler, I have set the size to Bounds.Size or WorkingArea.Size…but it doesn’t solve my problem. First, the task bar is still showing and secondly, I don’t have the title bar.

    Do you have any solution so that I can have a form in a Windows Mobile Ce 5.0 c# application to take the screen space completely (even the task bar space) and have the title bar of the screen and also, the screen should not be movable.

    Thanks.

    Ashvin

  5. hello !! it is so helpful and it is working also but i have problem that when i click on fullscreen button on my screen many flickring is there and after some time it will be give result as fullscreen

  6. This workaround (and all the others that i found on net) has a problem.

    I tried this on an MDI app with a toolstrip menu. When the task bar is not on the bottom of the screen, the toolstrip menus that are above the hidden taskbar show on wrong position. They show at the end of the taskbar. Just like if the taskbar was still there !!!

    Haven’t found a solution yet.

  7. hey,

    i was wondering if you could tell me a way to keep the taskbar on top of the full screen program. The “keep task bar on top” is checked but it still doesn’t keep it on top of the full screen program. thanks

  8. This is contradiction for this post 🙂

    There is no way (IMHO) that you can force Taskbar over ANY application – if this is possible, that means than there will be no full screen application.

  9. This is very Good Article.
    But if i had open-up my form within MDI.
    SO, Is it possible to remove form border or control box from target open-up form ?

  10. I can’t find the user32.dll in my VisualStudio APIs. I’ve downloaded it but I can’t add this library as reference so I try to put it in the folder of my DeviceApplication but it doesn’t work..Could someone help me?

  11. This technique doesn’t prevent the user from accessing the taskbar by pressing (Control + Escape) or pressing on the Windows button on the keyboard. Is there anyway of prevent the users from accessing the taskbar **at all**? The user shouldn’t be allow to exit your application to run other apps. Any idea?

  12. I do not know how to do that from top of my head, but if you can (have access to machine and rights to change Registry), you can replace user’s standard shell application (explorer.exe) with your application and take over all of control.

    (look for example here: http://support.microsoft.com/kb/143164)

    However, this is rather dangerous technique and suggest to test it a lot (using for example Virtual PC) before releasing it as a general solution.

  13. What does this solution do to the performence??
    I’m thinking of using this but I can also let the user set the taskbar to auto hide it.

  14. Thanks a lot! I need a solution like this for visual basic (not C#) and with a little translation it worked like a champ.

  15. Hi nice solution !

    Iuse the following code.

    I foud out that the order of commands are importend ! (if you change it want work correct!)

    matches this your solution?? please try!

    public New() // Formconstructor
    {
    // if FormStartPosition.Manual is not set, then you can´t set the Form Location !!!
    // see msdn documentation
    this.StartPosition = FormStartPosition.Manual;

    // Set the Form to VirtualScreen (FullScreen)
    this.Location = SystemInformation.VirtualScreen.Location;
    this.Size = SystemInformation.VirtualScreen.Size;
    this.ClientSize = SystemInformation.VirtualScreen.Size;

    //maximize The Window to FullScreen without Borders and Titelbar
    // erase WindowBorder
    this.FormBorderStyle = Windows.Forms.FormBorderStyle.None;
    // Set Window to Topmost
    this.TopMost = true;
    }

  16. I will change
    private static IntPtr HWND_TOP = IntPtr.Zero;
    to
    private static IntPtr HWND_NOTOPMOST = new IntPtr(-2);
    if you want to use other windows (Alt+Tab)

  17. Thanx, man! You’re really a god! I’ve spent the entire evening optimizing the fullscreen switch behavior of my app.. Until in the middle of the night have found your amazing solution 🙂

  18. Thanks for the article, I’ve been futzing around trying to figure this out for a couple hours. This works!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.