简体   繁体   English

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

[英]How do I make a WinForms app go Full Screen

I have a WinForms app that I am trying to make full screen (somewhat like what VS does in full screen mode).我有一个 WinForms 应用程序,我正在尝试全屏显示(有点像 VS 在全屏模式下所做的)。

Currently I am setting FormBorderStyle to None and WindowState to Maximized which gives me a little more space, but it doesn't cover over the taskbar if it is visible.目前我将FormBorderStyle设置为None并将WindowState设置为Maximized这给了我更多的空间,但如果它是可见的,它不会覆盖任务栏。

What do I need to do to use that space as well?我还需要做什么才能使用该空间?

For bonus points, is there something I can do to make my MenuStrip autohide to give up that space as well?对于奖励积分,我可以做些什么让我的MenuStrip自动隐藏以放弃该空间?

To the base question, the following will do the trick (hiding the taskbar)对于基本问题,以下将解决问题(隐藏任务栏)

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

But, interestingly, if you swap those last two lines the Taskbar remains visible.但是,有趣的是,如果您交换最后两行,任务栏仍然可见。 I think the sequence of these actions will be hard to control with the properties window.我认为这些操作的顺序将很难用属性窗口控制。

A tested and simple solution经过测试的简单解决方案

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.我一直在 SO 和其他一些网站上寻找这个问题的答案,但有人给出的答案对我来说非常复杂,而其他一些答案根本无法正常工作,因此经过大量代码测试后,我解决了这个难题。

Note: I'm using Windows 8 and my taskbar isn't on auto-hide mode.注意:我使用的是Windows 8 ,我的任务栏未处于自动隐藏模式。

I discovered that setting the WindowState to Normal before performing any modifications will stop the error with the not covered taskbar.我发现在执行任何修改之前将 WindowState 设置为 Normal 将停止未覆盖任务栏的错误。

The code编码

I created this class that have two methods, the first enters in the "full screen mode" and the second leaves the "full screen mode".我创建了这个有两种方法的类,第一个进入“全屏模式”,第二个离开“全屏模式”。 So you just need to create an object of this class and pass the Form you want to set full screen as an argument to the EnterFullScreenMode method or to the LeaveFullScreenMode method:所以你只需要创建这个类的一个对象,并将你想要设置全屏的 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;
    }
}

Usage example使用示例

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

I have placed this same answer on another question that I'm not sure if is a duplicate or not of this one.我已将相同的答案放在另一个问题上,我不确定是否与此问题重复。 (Link to the other question: How to display a Windows Form in full screen on top of the taskbar? ) (链接到另一个问题: 如何在任务栏顶部全屏显示 Windows 窗体?

And for the menustrip-question, try set对于菜单条问题,请尝试设置

MenuStrip1.Parent = Nothing

when in fullscreen mode, it should then disapear.在全屏模式下,它应该消失。

And when exiting fullscreenmode, reset the menustrip1.parent to the form again and the menustrip will be normal again.和退出fullscreenmode时,重置menustrip1.parent再次向形式和的MenuStrip将再次正常。

You can use the following code to fit your system screen and task bar is visible.您可以使用以下代码来适应您的系统屏幕并且任务栏是可见的。

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

No need to use:无需使用:

    this.TopMost = true;

That line interferes with alt+tab to switch to other application.该行会干扰alt+tab切换到其他应用程序。 ("TopMost" means the window stays on top of other windows, unless they are also marked "TopMost".) (“TopMost”表示该窗口位于其他窗口之上,除非它们也被标记为“TopMost”。)

I recently made an Mediaplayer application and I used API-calls to make sure the taskbar was hidden when the program was running fullscreen and then restored the taskbar when the program was not in fullscreen or not had focus or was exited.我最近制作了一个 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

I worked on Zingd idea and made it simpler to use.我致力于 Zingd 的想法并使其更易于使用。

I also added the standard F11 key to toggle fullscreen mode.我还添加了标准F11 键来切换全屏模式。

Setup设置

Everything is now in the FullScreen class, so you don't have to declare a bunch of variables in your Form.现在一切都在 FullScreen 类中,因此您不必在表单中声明一堆变量。 You just instanciate a FullScreen object in your form's constructor :您只需在表单的构造函数中实例化一个 FullScreen 对象:

FullScreen fullScreen;

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

Please note this assumes the form is not maximized when you create the FullScreen object.请注意,这假定在您创建 FullScreen 对象时表单未最大化。

Usage用法

You just use one of the classe's functions to toggle the fullscreen mode :您只需使用 classe 的功能之一来切换全屏模式:

fullScreen.Toggle();

or if you need to handle it explicitly :或者如果您需要明确处理它:

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

Code代码

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

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

I don't know if it will work on .NET 2.0, but it worked me on .NET 4.5.2.我不知道它是否适用于 .NET 2.0,但它适用于 .NET 4.5.2。 Here is the code:这是代码:

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
}

Usage:用法:

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

Notice: You MUST place it inside your Form's class (Example: partial class Form1 : Form { /* Code goes here */ } ) or it will not work because if you don't place it on any form, code this will create an exception.注意:你必须把它放在你的 Form 的类中(例如: partial class Form1 : Form { /* Code goes here */ } )否则它不会工作,因为如果你不把它放在任何表单上,代码this将创建一个例外。

On the Form Move Event add 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);
}

If you want to keep the border of the form and have it cover the task bar, use the following:如果要保留窗体的边框使其覆盖任务栏,请使用以下命令:

Set FormBoarderStyle to either FixedSingle or Fixed3DFormBoarderStyle设置为FixedSingleFixed3D

Set MaximizeBox to FalseMaximizeBox设置为False

Set WindowState to MaximizedWindowState设置为Maximized

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM