Click here to Skip to main content
Click here to Skip to main content

How To Make a Windows Form App Truly Full Screen (and Hide Taskbar) in C#

By , 3 Dec 2006
 
Sample Image - MaxWinForm.png

Introduction

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

The initial approach is obvious...

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

... assuming that the target form is referenced with targetForm.

Does it work? Well, sort of. If your Taskbar has default setting unchecked for “Keep the taskbar on top of other Windows”, this will present your application in all its glory all over the 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.

Update For This Approach

In the discussion of the article (below) Azlan David (thanks David!) suggested to try this approach:

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

(Just changed the order of property assignments.) It does the work on all Win XP SP2 computers where I had the opportunity to test it; however, on one Win 2003 SP1 computer, this did not help; I'm still investigating why.

Let’s go further - the next step is to use P/Invoke and to engage the Win32 API services. There is an easy way to hide a 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 - the Taskbar is hidden, but your application still does not occupy the whole screen - the place where the Taskbar was is not used.

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

The steps are as follows:

  • Find out the dimension of the screen estate by calling GetSystemMetrics
  • Set the dimensions of your form to full screen

Here is the actual code:

/// <summary>
/// Selected Win API 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; // 0×0040

    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 / maximize 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;
    }
}

Now you can use the above class in your WinForms application like this:

public partial class MaxForm : Form
{
    FormState formState = new FormState();

    public MaxForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        formState.Maximize(this);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        formState.Restore(this);
    }
}

The full code and example application can be downloaded from the link at the top of this article.

History

  • 3rd December, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dejan.Vesic
Web Developer
Serbia Serbia
Member
Highly skilled, very efficient, organized and focused senior programmer and project manager.
 
Specialist for the E-commerce solutions based on Microsoft Web technologies on top of MS SQL or Oracle database servers.
 
13 years of experience. Strong in .Net (C# / ASP.NET), XML, JavaScript, DHTML, Database design, PL/SQL, T-SQL, ADO / ADO.NET, scripting and shell languages (Awk, CMD, 4Dos / 4NT).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey23 Feb '12 - 21:40 
NIce
GeneralEasier Stillmembergutharius12 Jan '11 - 6:05 
Just set the form size to the screen width and height, set the location to 0,0, and then DON'T set the maximized option just leave it at normal. This way all you need to do is create a timer to refresh the form and call me.bringtofront().
 
This automatically causes the form to cover up the taskbar without you having to touch the taskbar in any way.
GeneralRe: Easier Stillmemberjohannesnestler23 Feb '12 - 22:32 
this would be a bad hack - don't use it in any relevant software!
Generalhiding the mouse cursor on fullscreen..memberasgz25 Jul '10 - 23:28 
hiding the mouse cursor on fullscreen mode
Cursor.Hide()
 
showing the mouse cursor on normal mode
Cursor.Show()
QuestionDoes this work on Multi-Monitor systems?memberquintic20 May '10 - 12:04 
I need to scale across 4 screens, is this a viable solution for that?
 
Thanks, in advance
- Wayne
QuestionHow to make a windows application in .net such that it goes full screen and disable the keyboard at the same timememberkeeks12311 Feb '09 - 21:20 
I would like to make an application for online exam such that when the exam starts, the window has to go full screen (even the taskbar should be hidden) and only exit it after the exam is over, so that no one can go onto windows and cheat during the exam. Kindly Help someone Frown | :(
 
Thanks in advance! Smile | :)
AnswerRe: How to make a windows application in .net such that it goes full screen and disable the keyboard at the same timememberDejan.Vesic11 Feb '09 - 21:31 
"Online Exam" - I presume that you are talking about Web application and not desktop one.
 
If this is a case I am sorry, but I am almost positive that that is not possible.
 
Regards,
Dejan
QuestionHi, Does anyone know how to do this in C++/CLI ?membershadowfaxe2 Sep '08 - 3:39 
Hi.
I've been searching all over for a way to do this in C++/CLI.
Does anybody know how?
 
Best Regards
GeneralSimpler way to get screen boundsmembermaciej_jl19 Dec '07 - 7:16 
I'm using code like this:
this.FormBorderStyle = FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;

 
Works fine on my Vista Business .NET 2.0
 
Cheers !!!
GeneralTaskbar not hidingmemberCodeHook25 Sep '07 - 0:36 
Hi
After clicking the full screen,lock the desktop and unlock. You could see the taskbar. Now the application is not in full screen.Frown | :(
 
I don't need a signature
GeneralControlboxmemberjamesrgoodwin21 Aug '07 - 6:26 
Thanks, this is great.
 
However, I would like to add the full screen button to the controlbox at the top right of the application.. This is similar to the TV mode button on WinTV!
 
Have you any idea how I can do this?
 
Cheers,
James
GeneralThank youmembervaximillian26 Mar '07 - 5:56 
Thank you for your contribution.
Only one minor thing. Start the application and press the "Normal" button instead of the "FullScreen" button. The form will be minimized. It wont happen if the form is maximized once.
 
Again, thanks for contributing.
Regards
Generalconvert to vb.net, please HELPmembertlin504317 Dec '06 - 22:13 
i found this article is very useful for me. but i not good in C#, is it possible anyone here could kindly help me convert it to vb or tell me how to call it in VB. Please help!Frown | :(
GeneralRe: convert to vb.net, please HELPmemberDejan.Vesic17 Dec '06 - 22:19 
Try simply first:
 
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.WindowState = FormWindowState.Maximized;
 
It is same in both languages Smile | :)
 
If that fails, we will try other approach.
GeneralRe: convert to vb.net, please HELPmemberixup26 Aug '08 - 3:47 
Namespace Org.Vesic.WinForms
    '''
    ''' Selected Win AI Function Calls
    '''

   
    Public Class WinApi
        <dllimport("user32.dll",> _
        Public Shared Function GetSystemMetrics(ByVal which As Integer) As Integer
        End Function
       
        <dllimport("user32.dll")> _
        Public Shared Sub SetWindowPos(ByVal hwnd As IntPtr, ByVal hwndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal width As Integer, ByVal height As Integer, _
        ByVal flags As UInteger)
        End Sub
       
        Private Const SM_CXSCREEN As Integer = 0
        Private Const SM_CYSCREEN As Integer = 1
        Private Shared HWND_TOP As IntPtr = IntPtr.Zero
        Private Const SWP_SHOWWINDOW As Integer = 64
        ' 0x0040
        Public Shared ReadOnly Property ScreenX() As Integer
            Get
                Return GetSystemMetrics(SM_CXSCREEN)
            End Get
        End Property
       
        Public Shared ReadOnly Property ScreenY() As Integer
            Get
                Return GetSystemMetrics(SM_CYSCREEN)
            End Get
        End Property
       
        Public Shared Sub SetWinFullScreen(ByVal hwnd As IntPtr)
            SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, _
            SWP_SHOWWINDOW)
        End Sub
    End Class
   
    '''
    ''' Class used to preserve / restore state of the form
    '''

    Public Class FormState
        Private winState As FormWindowState
        Private brdStyle As FormBorderStyle
        Private topMost As Boolean
        Private bounds As Rectangle
       
        Private IsMaximized As Boolean = False
       
        Public Sub Maximize(ByVal targetForm As Form)
            If Not IsMaximized Then
                IsMaximized = True
                Save(targetForm)
                targetForm.WindowState = FormWindowState.Maximized
                targetForm.FormBorderStyle = FormBorderStyle.None
                targetForm.TopMost = True
                WinApi.SetWinFullScreen(targetForm.Handle)
            End If
        End Sub
       
        Public Sub Save(ByVal targetForm As Form)
            winState = targetForm.WindowState
            brdStyle = targetForm.FormBorderStyle
            topMost = targetForm.TopMost
            bounds = targetForm.Bounds
        End Sub
       
        Public Sub Restore(ByVal targetForm As Form)
            targetForm.WindowState = winState
            targetForm.FormBorderStyle = brdStyle
            targetForm.TopMost = topMost
            targetForm.Bounds = bounds
            IsMaximized = False
        End Sub
    End Class
End Namespace
 
Visual Basic Lover

QuestionHow to use on Pocket PC?memberKitro15 Dec '06 - 5:57 
Hi
 
The article sound nice. But i work with Windows CE 5.0 devices and need this functionality for it. Can you give a short snippet how to set up a Pocket PC form to Fullscreen mode. the user32.dll isn't supported under Compact Framework 2.0 on Pocket PC.
 
Best regards
Kitro
AnswerRe: How to use on Pocket PC?memberDejan.Vesic15 Dec '06 - 10:37 
Hello.
 
I never tried to do this on PocketPC, but you can try usual approach:
 
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.WindowState = FormWindowState.Maximized;
 
and if that fails, to use WinApi, replacing User32.dll with CoreDll.dll
 
I am not sure that will work, but you can try Smile | :)
AnswerRe: How to use on Pocket PC?memberAshvin Gunga20 Feb '07 - 18:56 
Hello Kitro,
 
I am actually working on a Windows Mobile CE 5.0 device, and it seems to be that I am getting the same problem. When maximizing the form, all what I can do is to maximize the form and hide the task bar (my project is a c# device application). However, I can't make this maximized form take up the empty space left by the task bar. Do you have any work around to this problem? Or any possible solution?
 
I would be very grateful to you if you can help me. Thank you in advance.

 
Ashvin Gunga

GeneralAdditional details..memberSkylan Hill7 Dec '06 - 18:24 
In an stand-alone, dedicated application I develop at work, I had this very same requirement.
 
The simplest explanation is to link you directly to MS articles:
http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/regentry/55200.mspx?mfr=true
-and-
http://support.microsoft.com/kb/886217
 

Essentially,
ForegroundLockTimeout was initially disabled on Windows 2000 (IIRC), but enabled on XP and up.
This means that trying to set TopMost won't work correctly.
 
Set ForegroundLockTimeout to "0" for the proper user, and use TopMost. Note you may have issues with the input focus being stolen by other applications--good luck with that.
GeneralSimpler WaymemberAzlan David5 Dec '06 - 6:15 
If you paste this code into your example the same result is obtained without the need of your formstate module.
 
namespace Org.Vesic.WinForms
{
public partial class MaxForm : Form
{
FormState formState = new FormState();
 
public MaxForm()
{
InitializeComponent();
}
 
private void button1_Click(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
 
private void button2_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Normal;
FormBorderStyle = FormBorderStyle.Sizable;
}
}
}
GeneralRe: Simpler WaymemberDejan.Vesic5 Dec '06 - 6:36 
Well, not quite.
 
Setup your Taskbar to appear above all windows (right-click on Taskbar / Properties / Check Keep the taskbar on top of other windows) and try your example Smile | :)
GeneralRe: Simpler WaymemberAzlan David5 Dec '06 - 20:49 
If you have the code in this order then the task bar is hidden
 
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
 
However in your code you have it in this order
 
WindowState = FormWindowState.Maximized;
FormBorderStyle = FormBorderStyle.None;
 
and the task bar will always be on top.
 
The test code is your example with the new button code pasted in.
 
With the border style set to NONE the task bar is always covered up if the window stae is maximised.
 
Give it a try.
 


GeneralRe: Simpler WaymemberDejan.Vesic5 Dec '06 - 22:19 
Just tried.
 
Works on one computer (Win XP SP 2, .Net 2.0), does not work on other (Win 2003 SP1, .Net 2.0).
 
Not sure why does not work on second one.
GeneralRe: Simpler WaymemberAzlan David5 Dec '06 - 23:36 
I have only used it for our projects on XP SP2 and it has worked on all machines to date, I have not tried it on Win 2003.
 
The full window effect when using a borderless form was originaly a bug in one of our custom controls where the task bar was being covered up which was not what we wanted.
 
Interested to see if you can find why it does not work on Win 2003.
 
Regards
 
Azlan
GeneralRe: Simpler WaymemberDejan.Vesic5 Dec '06 - 23:55 
I updated article with your approach, thanks.
 
Still investigating why is not working on this particular machine - will post findings, if any.
 
Regards,
Dejan
QuestionVery nice, but can you...memberKGilbert4 Dec '06 - 7:41 
Very nice, but can you also disable the ability to Alt-Tab to other apps? This displays the taskbar over the form and kind of defeats the purpose.
AnswerRe: Very nice, but can you...memberDejan.Vesic4 Dec '06 - 8:03 
That is kind of too much Smile | :) for purpose intended when I wrote particular application.
 
However, if you need that, simplest way to do it is to register your handler for Alt-TAB once when you are in Full screen mode and (do not forget Smile | :) ) unregister it on Application Exit.
 
You should use two new Win API calls:
 
User32.RegisterHotKey and User32.UnregisterHotKey and Register/Unregister MOD_ALT + VK_TAB combination.
 
Details for subject can be found here: http://www.codeproject.com/vb/net/mclhotkeynet.asp
 

AnswerRe: Very nice, but can you...memberAzlan David5 Dec '06 - 21:22 
Give this a try
 
http://www.codeproject.com/cs/system/CSLLKeyboard.asp
GeneralWell Donememberaprenot4 Dec '06 - 6:29 
Very concise, informative article without too much fluff, yet still explaining how to do exactly what the title states. Well done Sir.
 
Aaron Prenot
GeneralRe: Well DonememberDejan.Vesic4 Dec '06 - 7:23 
Thanks a lot.
 
---
Regards,
Dejan Vesić
 
Home: http://www.vesic.org/english/ | Blog: http://www.vesic.org/english/blog/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 3 Dec 2006
Article Copyright 2006 by Dejan.Vesic
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid