如何使WinForms应用程序全屏显示

122

我有一个WinForms应用程序,我正在尝试使其全屏(类似于VS在全屏模式下的做法)。

目前,我将FormBorderStyle设置为None并将WindowState设置为Maximized,这样可以给我更多的空间,但如果任务栏可见,它不会覆盖任务栏。

我需要做什么才能利用那部分空间呢?

为了获得额外加分,是否有办法使我的MenuStrip自动隐藏以释放那部分空间呢?

11个回答

171

对于基础问题,下面的方法可以解决(隐藏任务栏)

private void Form1_Load(object sender, EventArgs e)
{
    this.TopMost = true;
    this.FormBorderStyle = FormBorderStyle.None;
    this.WindowState = FormWindowState.Maximized;
}

但有趣的是,如果你交换这两行代码,任务栏仍然可见。我认为这些操作的顺序可能很难通过属性窗口来控制。


4
订购问题是导致之前无法正常工作的原因。我实际上是按照那个顺序设置属性的,但当窗体已经最大化时,将边框设置为无并不能扩展到覆盖任务栏。 我通过“还原”窗体来解决这个问题,改变边框设置,然后再最大化窗体。 - Grady
3
我已经按正确的顺序排列了,但它仍然无法正常工作。任务栏始终可见,应用程序并不在其下方,它只是在那里为任务栏留出了一些自由空间。(Win7) - Preza8
@Preza8 - 请阅读Grady的评论,检查是否适用于您的情况。 - H H
1
对不起,我很久没在这里上线了,我忘记了我是怎么做到的,但我记得尝试随机顺序执行这些命令有所帮助。 - Preza8
注意:由于某种原因,我不得不设置属性并将其放入代码中。 - Joe Phillips

24

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

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

我已经把这个相同的答案放在另一个问题上了,我不确定它是否与这个重复了。(链接到另一个问题:如何在任务栏顶部以全屏显示Windows窗体?


2
出于好奇,为什么你需要一个完整的枚举来描述真/假条件? - Nathan Ridley
2
我很久以前写的,当时我只是在试着写代码,请原谅我的年轻愚蠢。这确实毫无意义,我应该简单地使用布尔类型。 - Zignd
1
它对我起作用了,而且我不得不在退出全屏之前设置 targetForm.WindowState = FormWindowState.Normal;。这是为了处理用户从最大化窗口进入全屏的情况。 - gneri

6
对于menustrip问题,尝试设置
MenuStrip1.Parent = Nothing

当处于全屏模式时,应该使其消失。

退出全屏模式时,将 menustrip1.parent 重置为窗体,菜单栏就会恢复正常。


5
您可以使用以下代码来适应您的系统屏幕,同时任务栏也可见。
    private void Form1_Load(object sender, EventArgs e)
    {   
        // hide max,min and close button at top right of Window
        this.FormBorderStyle = FormBorderStyle.None;
        // fill the screen
        this.Bounds = Screen.PrimaryScreen.Bounds;
    }

无需使用:

    this.TopMost = true;

该行代码会影响使用alt+tab键切换到其他应用程序。("TopMost"表示该窗口会保持在其他窗口的顶部,除非它们也被标记为"TopMost")。


4

最近我制作了一个媒体播放器应用程序,我使用API调用来确保当程序全屏运行时隐藏任务栏,然后在程序不在全屏或失去焦点或退出时恢复任务栏。

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer

Sub HideTrayBar()
    Try


        Dim tWnd As Integer = 0
        Dim bWnd As Integer = 0
        tWnd = FindWindow("Shell_TrayWnd", vbNullString)
        bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
        ShowWindow(tWnd, 0)
        ShowWindow(bWnd, 0)
    Catch ex As Exception
        'Error hiding the taskbar, do what you want here..'
    End Try
End Sub
Sub ShowTraybar()
    Try
        Dim tWnd As Integer = 0
        Dim bWnd As Integer = 0
        tWnd = FindWindow("Shell_TrayWnd", vbNullString)
        bWnd = FindWindowEx(tWnd, bWnd, "BUTTON", vbNullString)
        ShowWindow(bWnd, 1)
        ShowWindow(tWnd, 1)
    Catch ex As Exception
        'Error showing the taskbar, do what you want here..'
    End Try


End Sub

7
如果有两个程序都这样做了会怎么样?如果你的程序在获得取消隐藏任务栏的机会之前崩溃了会怎么办? - Tmdean
@Tmdean:在我的情况下,这个程序并不是问题,它运行在专门用于在等候室屏幕上运行我的程序的机器上。 - Stefan
@Tmdean:如果两个程序都这样做,那么这一点是有道理的,如果处理不正确,可能会导致混乱。 - Stefan

4

我致力于改进Zingd的想法,让它更加易用。

同时,我还添加了标准的F11键来切换全屏模式。

设置

现在所有内容都在FullScreen类中,所以您不需要在表单中声明一堆变量。您只需在表单的构造函数中实例化一个FullScreen对象:

FullScreen fullScreen;

public Form1()
{
    InitializeComponent();
    fullScreen = new FullScreen(this);
}

请注意,这假设在创建FullScreen对象时,表单没有被最大化。

使用方法

您只需使用类中的一个函数来切换全屏模式:

fullScreen.Toggle();

或者如果您需要显式处理它:

fullScreen.Enter();
fullScreen.Leave();

代码

using System.Windows.Forms;


class FullScreen
{ 
    Form TargetForm;

    FormWindowState PreviousWindowState;

    public FullScreen(Form targetForm)
    {
        TargetForm = targetForm;
        TargetForm.KeyPreview = true;
        TargetForm.KeyDown += TargetForm_KeyDown;
    }

    private void TargetForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.F11)
        {
            Toggle();
        }
    }

    public void Toggle()
    {
        if (TargetForm.WindowState == FormWindowState.Maximized)
        {
            Leave();
        }
        else
        {
            Enter();
        }
    }
        
    public void Enter()
    {
        if (TargetForm.WindowState != FormWindowState.Maximized)
        {
            PreviousWindowState = TargetForm.WindowState;
            TargetForm.WindowState = FormWindowState.Normal;
            TargetForm.FormBorderStyle = FormBorderStyle.None;
            TargetForm.WindowState = FormWindowState.Maximized;
        }
    }
      
    public void Leave()
    {
        TargetForm.FormBorderStyle = FormBorderStyle.Sizable;
        TargetForm.WindowState = PreviousWindowState;
    }
}

2

你需要将窗口设置为最顶层。


1
不行。即使我将窗口设置为最顶层,它也无法覆盖任务栏。 - Grady
3
尝试一下: Bounds = Screen.PrimaryScreen.Bounds;http://www.codeproject.com/KB/cs/scrframework.aspx 有更多细节,例如用于多显示器。 - Tron

1

我不知道它是否适用于.NET 2.0,但它在.NET 4.5.2上对我有效。这是代码:

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Your_Form_Name : Form
{
    public Your_Form_Name()
    {
        InitializeComponent();
    }

    // CODE STARTS HERE

    private System.Drawing.Size oldsize = new System.Drawing.Size(300, 300);
    private System.Drawing.Point oldlocation = new System.Drawing.Point(0, 0);
    private System.Windows.Forms.FormWindowState oldstate = System.Windows.Forms.FormWindowState.Normal;
    private System.Windows.Forms.FormBorderStyle oldstyle = System.Windows.Forms.FormBorderStyle.Sizable;
    private bool fullscreen = false;
    /// <summary>
    /// Goes to fullscreen or the old state.
    /// </summary>
    private void UpgradeFullscreen()
    {
        if (!fullscreen)
        {
            oldsize = this.Size;
            oldstate = this.WindowState;
            oldstyle = this.FormBorderStyle;
            oldlocation = this.Location;
            this.WindowState = System.Windows.Forms.FormWindowState.Normal;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            fullscreen = true;
        }
        else
        {
            this.Location = oldlocation;
            this.WindowState = oldstate;
            this.FormBorderStyle = oldstyle;
            this.Size = oldsize;
            fullscreen = false;
        }
    }

    // CODE ENDS HERE
}

使用方法:

UpgradeFullscreen(); // Goes to fullscreen
UpgradeFullscreen(); // Goes back to normal state
// You don't need arguments.

注意: 您必须将它放在您的窗体类中(例如:partial class Form1 : Form { /* Code goes here */ }),否则它将无法工作,因为如果您不将其放置在任何窗体上,代码 this 将会引发异常。


1
如果您想保留表单的边框并覆盖任务栏,请使用以下方法:
FormBorderStyle 设为 FixedSingleFixed3DMaximizeBox 设置为FalseWindowState 设置为Maximized

1
在表单移动事件中添加以下内容:
private void Frm_Move (object sender, EventArgs e)
{
    Top = 0; Left = 0;
    Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
}

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