全屏模式,但不要覆盖任务栏。

22

我有一个WinForms应用程序,在登录时设置为全屏模式。

我的问题是它也遮盖了Windows任务栏。我不想让我的应用程序覆盖任务栏。

如何解决这个问题?


4
这就是全屏的定义。也许你想将它设置为“最大化”? - The Evil Greebo
@Evil:你应该用你的问题来回答这个问题。 - dance2die
1
@The E:我将它设置为 this.windowstate = maximised... - user698065
13个回答

60

我使用以下代码来完成此操作:

this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.WindowState = FormWindowState.Maximized;

5
我认为这应该是通过的答案,因为上面提供的解决方案不允许正常的Windows状态改变行为。 - mohnston
1
被接受的答案基本上就是 StackOverflow 的声望狩猎;提出一个小学生也能想到的不完美解决方案。这就是 OP 所要求的,就是这个! - IOviSpot
这适用于我的主显示器。一旦我将应用程序移动到我的次要显示器,分辨率相同,应用程序就会消失。 - user1853517

31

这可能是你想要的。它创建一个“最大化”的窗口,而不隐藏任务栏。

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

    private void Form1_Load( object sender, EventArgs e )
    {
        FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        Left = Top = 0;
        Width = Screen.PrimaryScreen.WorkingArea.Width;
        Height = Screen.PrimaryScreen.WorkingArea.Height;
    }
}

6

我曾经在这里回答过类似问题:

我之前没有提到的一件事是,我关闭了窗口的最大化按钮。当我测试重新打开该属性时,任务栏再次出现了。显然,它认为如果您不想要最大化按钮,则表示您正在创建一种全屏展示应用程序,不希望用户看到除应用程序屏幕以外的任何内容。不完全符合我的期望,但我想这可以解决问题。

我曾经遇到过这个问题,并通过Jeff的帮助解决了它。首先将窗口状态设置为“最大化”,但不要禁用MaximizeBox。然后,如果您想要禁用MaximizeBox,可以通过编程来实现:

private void frmMain_Load(object sender, EventArgs e)
{
    this.MaximizeBox = false;
}

6

Arcanox的回答对于单个显示器非常好,但如果您在除最左边以外的任何屏幕上尝试它,它将只会使表单消失。我使用了以下代码代替。

var workingArea = Screen.FromHandle(Handle).WorkingArea;
MaximizedBounds =  new Rectangle(0, 0, workingArea.Width, workingArea.Height);
WindowState = FormWindowState.Maximized;

唯一的区别在于我正在覆盖顶部和左侧的值为0,0,因为它们在其他屏幕上会不同。

1
我是这样解决的: Size = Screen.FromHandle(Handle).WorkingArea.Size; Location = Screen.FromHandle(Handle).WorkingArea.Location; - Sergio Villalobos

2

如果您有多个屏幕,您需要重置MaximizedBounds的位置:

Rectangle rect = Screen.FromHandle(this.Handle).WorkingArea;
rect.Location = new Point(0, 0);
this.MaximizedBounds = rect;
this.WindowState = FormWindowState.Maximized;

1
如果您想使用 WindowState = Maximized;,您应该首先通过 MaximizedBounds 属性指定最大化的窗体大小限制...
例如:
MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
WindowState = FormWindowState.Maximized;

您在哪里将表单大小限制为显示器的桌面区域?


1

我不擅长解释,但这是我用来最大化或全屏WinForms的代码,它不会覆盖任务栏。希望能帮到你。^^

private void Form_Load(object sender, EventArgs e)
{
    this.Height = Screen.PrimaryScreen.WorkingArea.Height;
    this.Width = Screen.PrimaryScreen.WorkingArea.Width;
    this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}

2
这个仅包含代码的答案可能需要一些解释! - Pyves

1

0
private void frmGateEntry_Load(object sender, EventArgs e)
    {
        // set default start position to manual  
        this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;

        // set position and size to the Form.  
        this.Bounds = Screen.PrimaryScreen.WorkingArea;
    }

0
尝试不使用 FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 这样的代码,并将注释行写成:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load( object sender, EventArgs e )
    {
        // FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        Left = Top = 0;
        Width = Screen.PrimaryScreen.WorkingArea.Width;
        Height = Screen.PrimaryScreen.WorkingArea.Height;
    }
}

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