Make your C# form go fullscreen
Many applications have the ability to go fullscreen. Here’s a simple snippet which will make your form go fullscreen and restore to its original location.
For testing purposes, I’ve attached a very simple KeyDown event which checks for F11
keypresses (the standard fullscreen keyboard shortcut in many applications).
[csharp]using System.Drawing;
using System.Windows.Forms;
namespace Danny_GB_Net {
public partial class FullscreenForm : Form {
// struct containing key information needed to restore the form to its original size/state
struct FullscreenRestoreState {
public Point location;
public int width;
public int height;
public FormWindowState state;
public FormBorderStyle style;
};
FullscreenRestoreState restoreState;
// used in ToggleFullscreen so that the program knows whether it needs to go fullscreen or restore
bool isFullscreen = false;
// standard constructor
public FullscreenForm() {
InitializeComponent();
// attach a test event
KeyDown += new System.Windows.Forms.KeyEventHandler(KeyDownEventHandler);
}
void ToggleFullscreen() {
if (!isFullscreen) { // save state information to restoreState and go fullscreen
restoreState.location = Location;
restoreState.width = Width;
restoreState.height = Height;
restoreState.state = WindowState;
restoreState.style = FormBorderStyle;
TopMost = true;
Location = new Point(0, 0);
FormBorderStyle = FormBorderStyle.None;
Width = Screen.PrimaryScreen.Bounds.Width;
Height = Screen.PrimaryScreen.Bounds.Height;
} else { // restore from restoreState
TopMost = false;
Location = restoreState.location;
Width = restoreState.width;
Height = restoreState.height;
WindowState = restoreState.state;
FormBorderStyle = restoreState.style;
}
// toggle our variable
isFullscreen = !isFullscreen;
}
// event for testing purposes
private void KeyDownEventHandler(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.F11) {
ToggleFullscreen();
}
}
}
}[/csharp]