首页 文章

如何在任务栏顶部全屏显示Windows窗体? [重复]

提问于
浏览
58

这个问题在这里已有答案:

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

  • WindowState = FormWindowState.Normal

  • TopMost =正常

  • 大小= 1024,768(这是它将要运行的机器的屏幕分辨率)

  • FormBorderStyle =无

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

  • this.Focus(); (在给焦点之后.Focus属性总是假的)

  • this.BringToFront();

  • this.TopMost = true; (但这在我的场景中并不理想)

  • this.Bounds = Screen.PrimaryScreen.Bounds;

  • this.Bounds = Screen.PrimaryScreen.Bounds;

有没有办法在.NET中执行它,或者我必须调用本机Windows方法,如果是这样,我们非常感谢代码片段 .

非常感谢

9 回答

  • 4

    使用:

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

    然后将表单放在任务栏上 .

  • 11

    我已经尝试了很多解决方案,其中一些解决方案适用于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处于假状态,允许其他形式通过主窗体 .

    它绝对解决了我的问题 .

  • 14

    这就是我如何使表格全屏 .

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

    我的简单修复结果是调用了表单的 Activate() 方法,因此不需要使用 TopMost (这就是我的目标) .

  • 2
    FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    WindowState = FormWindowState.Maximized;
    
  • -3

    经过测试的简单解决方案

    我一直在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

  • 0

    我相信可以通过简单地将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;
                }
            }
    
  • 88

    我没有解释它是如何工作的,但是有效,而且是牛仔编码器就是我所需要的 .

    System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
            this.MaximizedBounds = Screen.GetWorkingArea(this);
            this.WindowState = FormWindowState.Maximized;
    
  • 43
    FormBorderStyle = FormBorderStyle.Sizable;
    TopMost = false;
    WindowState = FormWindowState.Normal;
    

    这个代码使你的窗户全屏,这也将覆盖整个屏幕

相关问题