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

我有一个 WinForms 应用程序,我试图使全屏(有点像 VS 在全屏模式下所做的)。

目前我正在设置 FormBorderStyleNoneWindowStateMaximized,这给了我一点更多的空间,但它不覆盖在任务栏,如果它是可见的。

我需要做什么来使用这个空间呢?

对于奖励点,是否有什么我可以做,使我的 MenuStrip自动隐藏,以放弃该空间以及?

149201 次浏览

You need to set your window to be topmost.

我最近开发了一个 MediapPlayer 应用程序,我使用 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

至于大姨妈的问题,试试套餐

MenuStrip1.Parent = Nothing

当处于全屏模式时,它应该会消失。

And when exiting fullscreenmode, reset the menustrip1.parent to the form again and the menustrip will be normal again.

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

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

但是,有趣的是,如果交换最后两行,任务栏仍然是可见的。我认为这些操作的顺序将很难控制与属性窗口。

您可以使用下面的代码来适应系统屏幕,并且任务栏是可见的。

    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;

That line interferes with alt+tab to switch to other application. ("TopMost" means the window stays on top of other windows, unless they are also marked "TopMost".)

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

I've been looking for an answer for this question in SO and some other sites, but one gave an answer was very complex to me and some others answers simply doesn't work correctly, so after a lot code testing I solved this puzzle.

注意: 我正在使用 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;
}
}

我把同样的答案放在另一个问题上,我不确定是否是这个问题的复制品。(连结至另一个问题: 如何在任务栏顶部全屏显示 Windows 窗体?)

我不知道这是否会起作用。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.

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

在“窗体移动”事件中添加以下内容:

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

I worked on Zingd idea and made it 更简单 to use.

我还添加了标准的 F11 key来切换全屏模式。

设置

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

FullScreen fullScreen;


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

请注意,这里假设在创建 FullScreen 对象时窗体不是最大化的。

用法

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

fullScreen.Toggle();

or if you need to handle it explicitly :

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

如果你想保留表格 还有的边框,让它覆盖任务栏,使用以下内容:

FormBoarderStyle设置为 FixedSingleFixed3D

Set MaximizeBox to False

WindowState设置为 Maximized