Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#
Tip/Trick

Simple Full Screen Method (Hides Taskbar)

Rate me:
Please Sign up or sign in to vote.
4.85/5 (7 votes)
5 Jan 2016CPOL 19.5K   14   3
Visual Studio Winforms 100% Full Screen Method

Introduction

This is an easy way to put Winforms in 100% Full Screen.

Background

I tried for hours to find a simple way to go full screen without using [DllImport("user32.dll")], etc. because the risk of the user closing the program with the taskbar still hidden was too great.

Using the Code

So after experimenting for a few hours, I discovered if I put FormWindowState.Normal before FormWindowState.Maximized, I could cover the taskbar every time.

This code uses KeyDown to look for Alt+Enter which toggles Form1 to full-screen. The Esc key will also cancel the full-screen.

Make sure you set Form1.KeyPreview = true for KeyDown to work.

You can ignore TopPanel. It's here to show you how to hide other controls when full-screen if needed.

Using TopMost is not necessary and is up to you.

Tested on Windows 7 Ultimate. If you use this on a later Windows version, please let us know how it works.

C#
Size _size;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape && this.FormBorderStyle == FormBorderStyle.None)
    {
        e.SuppressKeyPress = true;
        SendKeys.Send("%{ENTER}");    	// Alt+Enter again to cancel full screen
    }
    if (e.Alt && e.KeyCode == Keys.Enter)	// Full Screen
    {
        e.SuppressKeyPress = true;            		// the order is important
        isFullScreen = (FormBorderStyle == FormBorderStyle.None) ? true : false;
        LeftPanel.Visible = isFullScreen;
 
        if (!isFullScreen) _size = this.Size;
 
        this.Visible = false;
        this.FormBorderStyle = (isFullScreen) ? FormBorderStyle.Sizable : FormBorderStyle.None;
        this.WindowState = FormWindowState.Normal; // forces the taskbar to be covered
        this.WindowState = FormWindowState.Maximized;
 
        if (isFullScreen) this.Size = _size;
 
        this.Visible = true;
 
        if (isFullScreen) this.WindowState = FormWindowState.Normal;

        this.TopMost = !isFullScreen;
    }
}

Points of Interest

This executes quickly with no unwanted artifacts.

History

  • 5th January, 2015: Initial version

License

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


Written By
Software Developer (Senior) none
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalrestore to normal size in windows 10 pro Pin
truword4-Jan-16 21:09
professionaltruword4-Jan-16 21:09 
GeneralRe: restore to normal size in windows 10 pro Pin
trantrum5-Jan-16 2:01
professionaltrantrum5-Jan-16 2:01 
You are correct. Sorry.

Taking a hint from "Restore original size" below (which I could not get to work). The revision is now posted.

Notice the new lines containing _size and the 2nd to last line which restores the Normal screen state.

And I was able to remove "Form1_ResizeEnd(sender, e);" because it is triggered automatically.

Thanks for catching it.

C#
Size _size;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape && this.FormBorderStyle == FormBorderStyle.None)
    {
        e.SuppressKeyPress = true;
        SendKeys.Send("%{ENTER}");              // Alt+Enter again to cancel full screen
    }
    if (e.Alt && e.KeyCode == Keys.Enter)       // Full Screen
    {
        e.SuppressKeyPress = true;              // the order is important
        isFullScreen = (FormBorderStyle == FormBorderStyle.None) ? true : false;
        LeftPanel.Visible = isFullScreen;

        if (!isFullScreen) _size = this.Size;

        this.Visible = false;
        this.FormBorderStyle = (isFullScreen) ? FormBorderStyle.Sizable : FormBorderStyle.None;
        this.WindowState = FormWindowState.Normal; // forces the taskbar to be covered
        this.WindowState = FormWindowState.Maximized;

        if (isFullScreen) this.Size = _size;

        this.Visible = true;

        if (isFullScreen) this.WindowState = FormWindowState.Normal;

        this.TopMost = !isFullScreen;
    }
}


modified 5-Jan-16 11:10am.

SuggestionRestore original size Pin
charga2-Jan-16 5:33
charga2-Jan-16 5:33 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.