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.
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}"); }
if (e.Alt && e.KeyCode == Keys.Enter) {
e.SuppressKeyPress = true; 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; 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