如何使WinForms应用程序全屏显示
我有一个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将会停止未覆盖任务栏的错误。
代码
我创建了这个类有两种方法,第一种进入“全屏模式”,第二种留下“全屏模式”。 所以你只需要创建这个类的一个对象,然后把你想要设置的全屏幕形式作为参数传递给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窗体? )
对于菜单栏问题,请尝试设置
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
你需要把你的窗口设置为最顶层。
我不知道它是否可以在.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 */ }
),否则它将不起作用,因为如果你不把它放在任何表单上,代码会创建一个例外。