I have a WinForms app that I am trying to make full screen (somewhat like what VS does in full screen mode).
Currently I am setting FormBorderStyle
to <
I don't know if it will work on .NET 2.0, but it worked me on .NET 4.5.2. Here is the code:
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Your_Form_Name : Form
{
public Your_Form_Name()
{
InitializeComponent();
}
// CODE STARTS HERE
private System.Drawing.Size oldsize = new System.Drawing.Size(300, 300);
private System.Drawing.Point oldlocation = new System.Drawing.Point(0, 0);
private System.Windows.Forms.FormWindowState oldstate = System.Windows.Forms.FormWindowState.Normal;
private System.Windows.Forms.FormBorderStyle oldstyle = System.Windows.Forms.FormBorderStyle.Sizable;
private bool fullscreen = false;
/// <summary>
/// Goes to fullscreen or the old state.
/// </summary>
private void UpgradeFullscreen()
{
if (!fullscreen)
{
oldsize = this.Size;
oldstate = this.WindowState;
oldstyle = this.FormBorderStyle;
oldlocation = this.Location;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
fullscreen = true;
}
else
{
this.Location = oldlocation;
this.WindowState = oldstate;
this.FormBorderStyle = oldstyle;
this.Size = oldsize;
fullscreen = false;
}
}
// CODE ENDS HERE
}
Usage:
UpgradeFullscreen(); // Goes to fullscreen
UpgradeFullscreen(); // Goes back to normal state
// You don't need arguments.
Notice: You MUST place it inside your Form's class (Example:
partial class Form1 : Form { /* Code goes here */ }
) or it will not work because if you don't place it on any form, codethis
will create an exception.
I worked on Zingd idea and made it simpler to use.
I also added the standard F11 key to toggle fullscreen mode.
Everything is now in the FullScreen class, so you don't have to declare a bunch of variables in your Form. You just instanciate a FullScreen object in your form's constructor :
FullScreen fullScreen;
public Form1()
{
InitializeComponent();
fullScreen = new FullScreen(this);
}
Please note this assumes the form is not maximized when you create the FullScreen object.
You just use one of the classe's functions to toggle the fullscreen mode :
fullScreen.Toggle();
or if you need to handle it explicitly :
fullScreen.Enter();
fullScreen.Leave();
using System.Windows.Forms;
class FullScreen
{
Form TargetForm;
FormWindowState PreviousWindowState;
public FullScreen(Form targetForm)
{
TargetForm = targetForm;
TargetForm.KeyPreview = true;
TargetForm.KeyDown += TargetForm_KeyDown;
}
private void TargetForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F11)
{
Toggle();
}
}
public void Toggle()
{
if (TargetForm.WindowState == FormWindowState.Maximized)
{
Leave();
}
else
{
Enter();
}
}
public void Enter()
{
if (TargetForm.WindowState != FormWindowState.Maximized)
{
PreviousWindowState = TargetForm.WindowState;
TargetForm.WindowState = FormWindowState.Normal;
TargetForm.FormBorderStyle = FormBorderStyle.None;
TargetForm.WindowState = FormWindowState.Maximized;
}
}
public void Leave()
{
TargetForm.FormBorderStyle = FormBorderStyle.Sizable;
TargetForm.WindowState = PreviousWindowState;
}
}
I've been looking for an answer for this question in SO and some other sites, but one gave an answer was very complex to me and some others answers simply doesn't work correctly, so after a lot code testing I solved this puzzle.
Note: I'm using Windows 8 and my taskbar isn't on auto-hide mode.
I discovered that setting the WindowState to Normal before performing any modifications will stop the error with the not covered taskbar.
I created this class that have two methods, the first enters in the "full screen mode" and the second leaves the "full screen mode". So you just need to create an object of this class and pass the Form you want to set full screen as an argument to the EnterFullScreenMode method or to the LeaveFullScreenMode method:
class FullScreen
{
public void EnterFullScreenMode(Form targetForm)
{
targetForm.WindowState = FormWindowState.Normal;
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.WindowState = FormWindowState.Maximized;
}
public void LeaveFullScreenMode(Form targetForm)
{
targetForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
targetForm.WindowState = FormWindowState.Normal;
}
}
private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
{
FullScreen fullScreen = new FullScreen();
if (fullScreenMode == FullScreenMode.No) // FullScreenMode is an enum
{
fullScreen.EnterFullScreenMode(this);
fullScreenMode = FullScreenMode.Yes;
}
else
{
fullScreen.LeaveFullScreenMode(this);
fullScreenMode = FullScreenMode.No;
}
}
I have placed this same answer on another question that I'm not sure if is a duplicate or not of this one. (Link to the other question: How to display a Windows Form in full screen on top of the taskbar?)
To the base question, the following will do the trick (hiding the taskbar)
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
But, interestingly, if you swap those last two lines the Taskbar remains visible. I think the sequence of these actions will be hard to control with the properties window.
On the Form Move Event add this:
private void Frm_Move (object sender, EventArgs e)
{
Top = 0; Left = 0;
Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
}
And for the menustrip-question, try set
MenuStrip1.Parent = Nothing
when in fullscreen mode, it should then disapear.
And when exiting fullscreenmode, reset the menustrip1.parent
to the form again and the menustrip will be normal again.