C# 如何在任务栏顶部全屏显示Windows窗体?

C# 如何在任务栏顶部全屏显示Windows窗体?,c#,.net,winforms,fullscreen,taskbar,C#,.net,Winforms,Fullscreen,Taskbar,我有一个需要全屏运行的.net windows应用程序。然而,当应用程序启动时,任务栏显示在主窗体的顶部,只有通过单击或使用ALT-TAB激活窗体时,任务栏才会消失。表单的当前属性如下所示: WindowsState=FormWindowsState.Normal 最顶端=正常 Size=1024768(这是它将要运行的机器的屏幕分辨率) FormBorderStyle=无 我尝试在表单加载中添加以下内容,但没有一个对我有效: 这是Focus();(在给出焦点后,this.focus属性始

我有一个需要全屏运行的.net windows应用程序。然而,当应用程序启动时,任务栏显示在主窗体的顶部,只有通过单击或使用ALT-TAB激活窗体时,任务栏才会消失。表单的当前属性如下所示:

  • WindowsState=FormWindowsState.Normal
  • 最顶端=正常
  • Size=1024768(这是它将要运行的机器的屏幕分辨率)
  • FormBorderStyle=无
我尝试在表单加载中添加以下内容,但没有一个对我有效:

  • 这是Focus();(在给出焦点后,this.focus属性始终为false)
  • 这个。布林托夫隆()
  • this.TopMost=true;(但在我的场景中,这并不理想)
  • this.Bounds=Screen.PrimaryScreen.Bounds
  • this.Bounds=Screen.PrimaryScreen.Bounds
是否有一种方法可以在.NET中实现这一点,或者我必须调用本机windows方法,如果是这样的话,请提供一段代码片段

非常感谢使用:

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

然后将表单放置在任务栏上。

我相信,只需将FormBorderStyle属性设置为“无”,并将Windows状态设置为最大化,就可以完成此操作。如果您使用的是VisualStudio,那么这两个工具都可以在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;
            }
        }
/*
*将System.Windows.Forms.Form置于全屏模式的函数
*作者:丹尼·巴蒂森
*联系人:gabehabe@googlemail.com
*/
//包含有关要还原到的状态的重要信息的结构
结构clientRect
{
公共点位置;
公共整数宽度;
公众内部高度;
};
//这应该在您的类的范围内
间接恢复;
bool全屏=假;
/// 
///使窗体全屏显示,或将其恢复为原始大小/位置
/// 
全屏作废()
{
如果(全屏==假)
{
this.restore.location=this.location;
this.restore.width=此.width;
this.restore.height=此.height;
this.TopMost=true;
该位置=新点(0,0);
this.FormBorderStyle=FormBorderStyle.None;
this.Width=Screen.PrimaryScreen.Bounds.Width;
this.Height=Screen.PrimaryScreen.Bounds.Height;
}
其他的
{
this.TopMost=false;
this.Location=this.restore.Location;
this.Width=this.restore.Width;
this.Height=this.restore.Height;
//这是您可能希望更改的两个变量,具体取决于
//关于窗体的设计(WindowsState和FormBorderStyle)
this.WindowState=FormWindowState.Normal;
this.FormBorderStyle=FormBorderStyle.sizeable;
}
}

我的简单解决方案原来是调用表单的
Activate()
方法,因此没有必要使用
最上面的
(这就是我的目标)。

我尝试了很多解决方案,其中一些在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的位置,代码的顺序将不起作用

这种方法的一个优点是将最上面的表单保留为false,从而允许其他表单覆盖主表单

这绝对解决了我的问题。

一个经过测试的简单解决方案 我一直在SO和其他一些网站上寻找这个问题的答案,但其中一个给出的答案对我来说非常复杂,而其他一些答案根本不正确,所以经过大量代码测试,我解决了这个难题

注意:我使用的是Windows 8,我的任务栏没有处于自动隐藏模式

我发现在执行任何修改之前将WindowsState设置为正常将停止未覆盖任务栏的错误

代码 我创建了这个类,它有两个方法,第一个进入“全屏模式”,第二个离开“全屏模式”。因此,您只需创建此类的对象,并将要设置全屏的表单作为参数传递给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 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;
}

这段代码使您的WINDOWS全屏显示这也将覆盖整个屏幕

我没有解释它是如何工作的,但它是如何工作的,而成为牛仔程序员就是我所需要的

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

我迟到了一天,我想是少了一美元。发送打印屏幕,看看它是什么样子。这是我成功完成这项任务的标准方法。我想会有其他问题。抱歉,由于客户原因,我无法发送屏幕抓图。它基本上是一个没有边框的表单,占据了整个屏幕大小,但当它第一次启动时会显示在任务栏后面,直到你点击表单激活它并将其置于任务栏顶部。感谢使用此技术时,开始栏仍然可见。它对我不起作用,它仍然无法覆盖任务栏抱歉,我应该在前面说过,FormBorderStyle已设置为“无”,但将WindowsState设置为“最大化”并不能解决问题。谢谢你
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
FormBorderStyle = FormBorderStyle.Sizable;
TopMost = false;
WindowState = FormWindowState.Normal;
        System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
        this.MaximizedBounds = Screen.GetWorkingArea(this);
        this.WindowState = FormWindowState.Maximized;