Click here to Skip to main content
6,305,776 members and growing! (16,768 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Intermediate License: The Code Project Open License (CPOL)

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

By Dejan.Vesic

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 on the screen estate
C#, Windows, .NET, Visual Studio, Dev
Posted:3 Dec 2006
Views:42,817
Bookmarked:48 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
18 votes for this article.
Popularity: 4.90 Rating: 3.90 out of 5

1

2
4 votes, 23.5%
3
3 votes, 17.6%
4
10 votes, 58.8%
5
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


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).
Occupation: Web Developer
Location: Serbia Serbia

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralHow to make a windows application in .net such that it goes full screen and disable the keyboard at the same time Pinmemberkeeks12322:20 11 Feb '09  
GeneralRe: How to make a windows application in .net such that it goes full screen and disable the keyboard at the same time PinmemberDejan.Vesic22:31 11 Feb '09  
GeneralHi, Does anyone know how to do this in C++/CLI ? Pinmembershadowfaxe4:39 2 Sep '08  
GeneralSimpler way to get screen bounds Pinmembermaciej_jl8:16 19 Dec '07  
GeneralTaskbar not hiding PinmemberCodeHook1:36 25 Sep '07  
GeneralControlbox Pinmemberjamesrgoodwin7:26 21 Aug '07  
GeneralThank you Pinmembervaximillian6:56 26 Mar '07  
Generalconvert to vb.net, please HELP Pinmembertlin504323:13 17 Dec '06  
GeneralRe: convert to vb.net, please HELP PinmemberDejan.Vesic23:19 17 Dec '06  
GeneralRe: convert to vb.net, please HELP Pinmemberixup4:47 26 Aug '08  
QuestionHow to use on Pocket PC? PinmemberKitro6:57 15 Dec '06  
AnswerRe: How to use on Pocket PC? PinmemberDejan.Vesic11:37 15 Dec '06  
AnswerRe: How to use on Pocket PC? PinmemberAshvin Gunga19:56 20 Feb '07  
GeneralAdditional details.. PinmemberSkylan Hill19:24 7 Dec '06  
GeneralSimpler Way PinmemberAzlan David7:15 5 Dec '06  
GeneralRe: Simpler Way PinmemberDejan.Vesic7:36 5 Dec '06  
GeneralRe: Simpler Way PinmemberAzlan David21:49 5 Dec '06  
GeneralRe: Simpler Way PinmemberDejan.Vesic23:19 5 Dec '06  
GeneralRe: Simpler Way PinmemberAzlan David0:36 6 Dec '06  
GeneralRe: Simpler Way PinmemberDejan.Vesic0:55 6 Dec '06  
QuestionVery nice, but can you... PinmemberKGilbert8:41 4 Dec '06  
AnswerRe: Very nice, but can you... PinmemberDejan.Vesic9:03 4 Dec '06  
AnswerRe: Very nice, but can you... PinmemberAzlan David22:22 5 Dec '06  
GeneralWell Done Pinmemberaprenot7:29 4 Dec '06  
GeneralRe: Well Done PinmemberDejan.Vesic8:23 4 Dec '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Dec 2006
Editor: Deeksha Shenoy
Copyright 2006 by Dejan.Vesic
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project