【问题标题】:How to display a Windows Form in full screen on top of the taskbar? [duplicate]如何在任务栏顶部全屏显示 Windows 窗体? [复制]
【发布时间】:2011-01-17 08:08:25
【问题描述】:

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

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

我尝试在表单加载时添加以下内容,但没有一个对我有用:

  • this.Focus(); (赋予焦点后 this.Focus 属性始终为 false)
  • this.BringToFront();
  • this.TopMost = true; (但这在我的场景中并不理想)
  • this.Bounds = Screen.PrimaryScreen.Bounds;
  • this.Bounds = Screen.PrimaryScreen.Bounds;

有没有办法在 .NET 中做到这一点,或者我必须调用本机 Windows 方法,如果是这样,非常感谢代码 sn-p。

非常感谢

【问题讨论】:

  • private void Form1_Activated(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; }

标签: c# .net winforms fullscreen taskbar


【解决方案1】:

用途:

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

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

【讨论】:

  • 我想我迟到了一天,还差了一美元。
  • 发送打印屏幕,它的外观。这是标准方式,我成功地用于此任务。我认为会有一些其他的问题。
  • 抱歉,由于客户原因,我无法发送屏幕截图。它基本上是一个没有边框的表单,它占据了整个屏幕的大小,但是当它第一次启动时显示在任务栏的后面,直到你点击激活它并将它带到任务栏顶部的表单。谢谢
  • 使用此技术时,开始栏仍然可见。
  • 它对我不起作用,它仍然没有覆盖任务栏
【解决方案2】:

我尝试了很多解决方案,其中一些适用于 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,从而允许其他表单超越主表单。

它绝对解决了我的问题。

【讨论】:

  • 这个解决方案在 Windows 8 上对我有用,而其他人没有。
  • 这很好用,应该是公认的答案。
  • 在 Windows 10 上为我出色地工作
  • this.Bounds = Screen.GetBounds(this);
  • 很好的简单解决方案。
【解决方案3】:

这就是我使表单全屏显示的方式。

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

【讨论】:

    【解决方案4】:

    我的简单修复结果是调用表单的Activate() 方法,因此无需使用TopMost(这是我的目标)。

    【讨论】:

      【解决方案5】:

      经过测试的简单解决方案

      我一直在 SO 和其他一些网站上寻找这个问题的答案,但一个人给出的答案对我来说非常复杂,而其他一些答案根本无法正常工作,所以经过大量代码测试后我解决了这个谜题。

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

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

      代码

      我创建的这个类有两个方法,第一个进入“全屏模式”,第二个离开“全屏模式”。所以你只需要创建这个类的一个对象,并将你想要设置全屏的Form作为参数传递给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;
              }
          }
      

      我在另一个我不确定是否与这个问题重复的问题上给出了相同的答案。 (链接到另一个问题:How do I make a WinForms app go Full Screen

      【讨论】:

      • 这在Win7中有很多问题:进入全屏时任务栏仍然显示在窗口上大约一秒钟,退出全屏时其他全屏窗口呈现而不是任务栏,并且它不保存WindowState ,因此如果您在最大化时进入全屏模式,您会返回一个未最大化的窗口。 (为什么全屏窗口仍然很难?)
      • @GlennMaynard 对于“...它不保存 WindowState...”问题,我最终在我的表单上添加了一个布尔值,指示表单在全屏之前是否已最大化;不幸的是,我无法在 Aero 或 Windows Classic 中可靠地重现您在 Windows 7 x64 上遇到的其他问题。
      • 仍然可以在 Windows 10 上运行并正确处理多个显示器(这里唯一的解决方案似乎可以做到这一点)
      【解决方案6】:
      FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
      WindowState = FormWindowState.Maximized;
      

      【讨论】:

      • mammadalius 已经提供了类似的解决方案。
      【解决方案7】:

      我相信只需将 FormBorderStyle 属性设置为 None 并将 WindowState 设置为 Maximized 即可。如果您使用的是 Visual Studio,那么两者都可以在 IDE 中找到,因此无需以编程方式执行此操作。确保在执行此操作之前包含一些关闭/退出程序的方法,因为这将删除右上角的那个非常有用的 X。

      编辑:

      试试这个。是我收藏了很久的sn-p。我什至不记得该归功于谁,但它确实有效。

      /*
       * 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 并没有解决问题。无论如何感谢您的回答
      • "this.TopMost" 将您的框架放在顶部(duh),但这包括在任务栏和所有内容的顶部。
      • 是的,通过使用您的 sn-p,屏幕最终位于任务栏的顶部,但是,正如我在原始问题中提到的那样,在我的应用程序中使用设置 TopMost 为 true 是不可取的,没有它sn-p 不再起作用了。
      • 这仅适用于主屏幕。如果您有 2 屏幕桌面,它将无法正常工作!您必须找到表单最初位于哪个屏幕上,然后使用该 Screen 对象来拉伸表单:Screen myScreen = Screen.FromHandle(this.Handle); 现在 myScreen 是表单所在的任何屏幕,而不是您在代码中输入的始终为 PrimaryScreen。 :) :P
      • 添加到 Cipi 的评论中我认为 Screen.GetBounds(panel1);也可以。
      【解决方案8】:

      我没有解释它是如何工作的,但它确实有效,而作为牛仔编码员就是我所需要的。

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

      【讨论】:

      • 这会将边框和标题栏定位在屏幕外。多台显示器就没那么好了。
      【解决方案9】:
      FormBorderStyle = FormBorderStyle.Sizable;
      TopMost = false;
      WindowState = FormWindowState.Normal;
      

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

      【讨论】:

      • 不,它不会...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多