Raheel Khan Raheel Khan - 2 months ago 25
C# Question

Full screen Windows Form goes beyond screen dimensions

I have a WinForms app (.NET 4) that needs to be shown either full screen or maximized without borders.

Using the following code in the

Form_Shown
event:

#if (DEBUG)
var debug = true;
#else
var debug = false;
#endif

this.Text = "";
this.ControlBox = false;
this.ShowInTaskbar = true;
//this.TopMost = debug;
this.TopLevel = true;
this.FormBorderStyle = FormBorderStyle.None;

if (debug) { this.Bounds = Screen.FromControl(this).WorkingArea; }
else { this.WindowState = FormWindowState.Maximized; }


If you look closely at the screenshot below, the top and bottom areas are cut off by a few pixels. Also, if maximized, the window still does not cover the task bar.

Please note that I have only one monitor attached. No secondary displays.

Any suggestions on how to address the two issues above would be appreciated.

Screenshot of app in maximized mode

UPDATE: The code above seems to work fine with forms without a
MenuStrip
or
StatusStrip
.

Answer

Here is the code I use for fullscreen. I create a FullScreen property for my form and when I need, I set this.FullScreen = true;

private bool fullScreen = false;
[DefaultValue(false)]
public bool FullScreen
{
    get
    {
        return fullScreen;
    }
    set
    {
        fullScreen = value;

        if (value)
        {
            //this.SuspendLayout();
            this.WindowState = FormWindowState.Normal;
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            //this.ResumeLayout(true);
        }
        else
        {
            this.Activate();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
        }
    }
}
Comments