如何在任务栏上方全屏显示Windows窗体?

74

我有一个 .NET 的 Windows 应用程序,需要以全屏模式运行。但是当应用程序启动时,任务栏会显示在主窗体的顶部,只有在通过单击或使用 ALT-TAB 激活窗体后才会消失。主窗体的当前属性如下:

  • WindowState=FormWindowState.Normal
  • TopMost=Normal
  • Size=1024,768 (这是它将要运行的机器的屏幕分辨率)
  • FormBorderStyle=None

我尝试在窗体加载时添加以下内容,但都没有起作用:

  • this.Focus();(在给予焦点后,这个 Focus 属性总是为 false)
  • this.BringToFront();
  • this.TopMost=true;(然而,在我的情况下,这并不理想)
  • this.Bounds=Screen.PrimaryScreen.Bounds;
  • this.Bounds=Screen.PrimaryScreen.Bounds;

是否有一种方法可以在.NET中实现全屏显示,或者我必须调用本地的Windows方法?如果是的话,非常感谢提供代码片段。

非常感谢!


1
私有 void Form1_Activated(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; } - Waruna Manjula
9个回答

128

使用:

FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;

然后您的表单被放置在任务栏上方。


1
我想说:“我似乎晚了一天,而且钱也不够。” - Christopher B. Adkins
发送屏幕截图,看看它的样子。这是我成功使用此任务的标准方式。我认为会有其他问题。 - TcKs
抱歉,由于客户原因,我无法发送屏幕截图。它基本上是一个没有边框的表单,占据整个屏幕大小,但当它首次启动时会显示在任务栏后面,直到您点击该表单激活它并将其置于任务栏之上。谢谢。 - myquestionoftheday
2
当使用这种技术时,开始栏仍然可见。 - Dan Gifford
4
对我来说不起作用,它仍然无法覆盖任务栏。 - r.hamd

69

我尝试了很多解决方案,其中一些在Windows XP上有效,但所有这些方法在Windows 7上都无效。最后我写了一个简单的方法来实现。

private void GoFullscreen(bool fullscreen)
    {
        if (fullscreen)
        {
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = Screen.PrimaryScreen.Bounds;
        }
        else
        {
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
        }
    }

代码的顺序很重要,如果你改变WindwosState和FormBorderStyle的位置,它将不起作用。

这种方法的优点之一是将 TOPMOST 设为 false,这允许其他表单遮盖主表单。

它绝对解决了我的问题。


1
当其他方法都无法解决问题时,这个解决方案在Windows 8上对我有效。 - msbarnard
4
这个很好用,应该被接受作为答案。 - Jhollman
1
在Windows 10上对我非常有效。 - Joe1992
4
这行代码的作用是获取当前窗口所在屏幕的边界信息,并将其存储在变量Bounds中。 - the.knife
2
好的简单解决方案。 - Rakibul Haq
W7 - 工作完美! - Blackmeser

16

这是我如何使表单全屏显示。

private void button1_Click(object sender, EventArgs e)
{
    int minx, miny, maxx, maxy;
    inx = miny = int.MaxValue;
    maxx = maxy = int.MinValue;

    foreach (Screen screen in Screen.AllScreens)
    {
        var bounds = screen.Bounds;
        minx = Math.Min(minx, bounds.X);
        miny = Math.Min(miny, bounds.Y);
        maxx = Math.Max(maxx, bounds.Right);
        maxy = Math.Max(maxy, bounds.Bottom);
    }

    Form3 fs = new Form3();
    fs.Activate();
    Rectangle tempRect = new Rectangle(1, 0, maxx, maxy);
    this.DesktopBounds = tempRect;
}

minx = miny = int.MaxValue; - undefined

15

我的简单修复方法是调用窗体的Activate()方法,因此不需要使用TopMost(这就是我想要的)。


7

一个经过测试的简单解决方案

我在 Stack Overflow 和其他网站上寻找答案,但其中一个答案对我来说太过复杂,而另一些答案则不起作用。因此,在大量代码测试后,我解决了这个难题。

注:我正在使用 Windows 8,我的任务栏没有开启自动隐藏模式。

我发现,在执行任何修改之前将 WindowState 设置为 Normal 将阻止未覆盖任务栏的错误。

代码

我创建了这个类,其中有两种方法,第一种进入“全屏模式”,第二种离开“全屏模式”。因此,您只需要创建这个类的对象,并将您想要设置为全屏的表单作为参数传递给 EnterFullScreenMode 方法或 LeaveFullScreenMode 方法:

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

我已经在另一个问题上发布了相同的答案,我不确定它是否是这个问题的重复。 (链接到另一个问题:如何使WinForms应用程序全屏


1
这在Win7中存在许多问题:进入全屏模式时,任务栏仍然会在窗口上显示约一秒钟,退出全屏模式时,其他全屏窗口会渲染而不是任务栏,并且它不保存WindowState,因此如果您在最大化时进入全屏模式,则返回的窗口不会最大化。(为什么全屏窗口仍然很难实现?) - Glenn Maynard
@GlennMaynard 对于“...它不保存WindowState...”问题,我最终在我的窗体上添加了一个布尔值,指示在全屏之前该窗体是否已被最大化;不幸的是,在Aero或Windows Classic中,我无法可靠地重现您在Windows 7 x64上遇到的其他问题。 - jrh
3
仍可在Windows 10上运行,并正确处理多个显示器(这似乎是唯一一个能够做到这一点的解决方法)。 - Garr Godfrey

6
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;

1
mammadalius已经提供了类似的解决方案。 - falsetru

3
我相信只需将您的FormBorderStyle属性设置为None,将WindowState设置为Maximized即可实现。如果您使用Visual Studio,则这两个属性可以在IDE中找到,因此无需通过编程实现。在执行此操作之前,请确保包括一种关闭/退出程序的方式,因为这将删除右上角那个非常有用的 X。
编辑:
尝试这个代码片段。我已经保存了很长时间,甚至记不起是谁写的了,但它确实有效。
/*
 * A function to put a System.Windows.Forms.Form in fullscreen mode
 * Author: Danny Battison
 * Contact: gabehabe@googlemail.com
 */

        // a struct containing important information about the state to restore to
        struct clientRect
        {
            public Point location;
            public int width;
            public int height;
        };
        // this should be in the scope your class
        clientRect restore;
                bool fullscreen = false;

        /// <summary>
        /// Makes the form either fullscreen, or restores it to it's original size/location
        /// </summary>
        void Fullscreen()
        {
            if (fullscreen == false)
            {
                this.restore.location = this.Location;
                this.restore.width = this.Width;
                this.restore.height = this.Height;
                this.TopMost = true;
                this.Location = new Point(0,0);
                this.FormBorderStyle = FormBorderStyle.None;
                this.Width = Screen.PrimaryScreen.Bounds.Width;
                this.Height = Screen.PrimaryScreen.Bounds.Height;
            }
            else
            {
                this.TopMost = false;
                this.Location = this.restore.location;
                this.Width = this.restore.width;
                this.Height = this.restore.height;
                                // these are the two variables you may wish to change, depending
                                // on the design of your form (WindowState and FormBorderStyle)
                this.WindowState = FormWindowState.Normal;
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
        }

抱歉,我之前应该说过了,FormBorderStyle已经设置为None,但将WindowState设置为Maximized并没有解决问题。无论如何,感谢您的答复。 - myquestionoftheday
"this.TopMost" 将你的框架置于顶部(显然),但这包括在任务栏和其他所有内容之上。 - Christopher B. Adkins
是的,使用您的代码片段,屏幕最终位于任务栏之上。然而,正如我在原始问题中提到的那样,在我的应用程序中设置TopMost为true并不理想,如果没有它,该代码片段将不再起作用。 - myquestionoftheday
3
这将仅在主屏幕上起作用。如果您有两个屏幕的桌面,则无法使用它! 您必须找到表单最初所在的屏幕,然后使用该屏幕对象来拉伸表单:Screen myScreen = Screen.FromHandle(this.Handle); 现在myScreen是表单所在的任何屏幕,并不总是您在代码中放置的PrimaryScreen。 :) :P - Cipi
补充Cipi的评论,我认为Screen.GetBounds(panel1);也可以。 - colin lamarre

2

我无法解释它是如何工作的,但它确实可以工作,作为一个牛仔程序员,这就是我所需要的。

        System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
        this.MaximizedBounds = Screen.GetWorkingArea(this);
        this.WindowState = FormWindowState.Maximized;

这会将边框和标题栏定位到屏幕之外。在使用多个显示器时不太理想。 - Garr Godfrey

-5
FormBorderStyle = FormBorderStyle.Sizable;
TopMost = false;
WindowState = FormWindowState.Normal;

这段代码可以让你的Windows全屏,也会覆盖整个屏幕。


1
不,它不会... - Metoniem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接