【发布时间】:2010-10-05 01:06:00
【问题描述】:
我有一个 WinForms 应用程序,我正在尝试使其全屏显示(有点像 VS 在全屏模式下所做的)。
目前我将FormBorderStyle 设置为None,将WindowState 设置为Maximized,这给了我更多空间,但如果它可见,它不会覆盖任务栏。
我还需要做什么才能使用该空间?
对于奖励积分,我可以做些什么来让我的 MenuStrip 自动隐藏也放弃那个空间?
【问题讨论】:
我有一个 WinForms 应用程序,我正在尝试使其全屏显示(有点像 VS 在全屏模式下所做的)。
目前我将FormBorderStyle 设置为None,将WindowState 设置为Maximized,这给了我更多空间,但如果它可见,它不会覆盖任务栏。
我还需要做什么才能使用该空间?
对于奖励积分,我可以做些什么来让我的 MenuStrip 自动隐藏也放弃那个空间?
【问题讨论】:
对于基本问题,以下将解决问题(隐藏任务栏)
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
但是,有趣的是,如果您交换最后两行,任务栏仍然可见。我认为这些动作的顺序很难用属性窗口控制。
【讨论】:
我一直在 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 to display a Windows Form in full screen on top of the taskbar?)
【讨论】:
targetForm.WindowState = FormWindowState.Normal;。用于处理用户从最大化窗口进入全屏的情况。
对于menustrip-question,尝试设置
MenuStrip1.Parent = Nothing
当处于全屏模式时,它应该会消失。
并且在退出全屏模式时,再次将menustrip1.parent重置为表单,菜单栏将再次正常。
【讨论】:
您可以使用以下代码来适应您的系统屏幕并且任务栏可见。
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”。)
【讨论】:
我最近制作了一个 Mediaplayer 应用程序,我使用 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
【讨论】:
我研究了 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;
}
}
【讨论】:
您需要将窗口设置为最顶层。
【讨论】:
我不知道它是否适用于 .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将创建一个异常。
【讨论】:
在表单移动事件上添加:
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);
}
【讨论】:
如果您想保留表单的边框并让它覆盖任务栏,请使用以下内容:
将FormBoarderStyle 设置为FixedSingle 或Fixed3D
将MaximizeBox 设置为False
将WindowState 设置为Maximized
【讨论】: