Full screen Windows Form goes beyond screen dimensions

风格不统一 提交于 2019-11-27 04:51:22

问题


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.

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


回答1:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/32821133/full-screen-windows-form-goes-beyond-screen-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!