简体   繁体   English

最大化无边框形式覆盖任务栏

[英]Maximized borderless form covers taskbar

A question has been asked here but none of the solutions provided worked with .NET 4.0 / Win7/8.1. 此处提出了一个问题但提供的解决方案均不适用于 .NET 4.0/Win7/8.1。 The answer revolves around not setting MaximizeBox to false but the following snippet shows it doesn't work (ie the form covers the entire screen anyway (tested on Win7 and 8.1 with ClassicShell). I need this to work across multiple screens and setting MaximumSize doesn't work very well: when the form isn't maximized, user should be allowed to have the form width spanning two monitors. There's also no BeforeMaximize event to hook into. So you can't simply set MaximumSize in the OnMove event.答案围绕着不将 MaximizeBox 设置为 false 但以下代码段显示它不起作用(即该表单无论如何都覆盖整个屏幕(在 Win7 和 8.1 上使用 ClassicShell 进行测试)。我需要它在多个屏幕上工作,并且设置MaximumSize不起作用效果不佳:当表单未最大化时,应允许用户将表单宽度跨越两个显示器。也没有可以挂钩的BeforeMaximize事件。因此您不能简单地在OnMove事件中设置MaximumSize

public TestForm()
{
    InitializeComponent();

    FormBorderStyle = FormBorderStyle.None;
    WindowState = FormWindowState.Maximized;
}

VS2013 seems to be able to do just that without covering the taskbar. VS2013 似乎能够在不覆盖任务栏的情况下做到这一点。

EDIT: Setting MaximizedBounds (as answered by Hans Passant) doesn't work in secondary screen where the secondary screen is larger than the primary.编辑:设置 MaximizedBounds(由 Hans Passant 回答)在辅助屏幕大于主屏幕的辅助屏幕中不起作用。 (note: the following is a modified version of Hans' answer as his simply did not work in secondary screen) (注意:以下是汉斯答案的修改版本,因为他根本无法在辅助屏幕中工作)

eg例如

protected override void OnLocationChanged(EventArgs e)
{
    var workArea = Screen.FromControl(this).WorkingArea;
    MaximizedBounds = new Rectangle(0, 0, workArea.Width, workArea.Height);
    Debug.WriteLine(MaximizedBounds);

    base.OnLocationChanged(e);
}

// Button click event (hit when form maximized):
WinApi.RECT rect;
WinApi.GetWindowRect(Handle, out rect);
Debug.WriteLine(rect);

Output:输出:

OnLocationChanged: {X=0,Y=0,Width=1920,Height=1040}

ButtonClick when form is maximized: {Left=1366,Top=-216,Right=3840,Bottom=876}

This works out to be:结果是:

Maximized Width = 3840 - 1366 = 2474最大宽度 = 3840 - 1366 = 2474

Maximized Height = 876 + 216 = 1092最大高度 = 876 + 216 = 1092

Where did the framework get those numbers from?框架从哪里获得这些数字?

If your borderless window cover the taskbar you can do it:如果您的无边框窗口覆盖了任务栏,您可以这样做:

this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Maximized;
this.MaximumSize = this.Size;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Normal;

This do it like this: On load maximizes window with border and sets the maximum size to the maximized with border - it isn't covers taskbar.这样做是这样的:加载时最大化带边框的窗口并将最大尺寸设置为带边框的最大化 - 它不覆盖任务栏。 After this it removes border and returns window to normal state.在此之后,它移除边框并将窗口返回到正常状态。 Add this snippet to load section.将此代码段添加到加载部分。 Sorry for possible errors, I'm from Poland :)对于可能的错误,我很抱歉,我来自波兰 :)

late to this party - anyway - this works for me:迟到这个派对 - 无论如何 - 这对我有用:

Screen screen = Screen.FromControl(this);
int x = screen.WorkingArea.X - screen.Bounds.X;
int y = screen.WorkingArea.Y - screen.Bounds.Y;
this.MaximizedBounds = new Rectangle(x, y,
    screen.WorkingArea.Width, screen.WorkingArea.Height);
this.MaximumSize = screen.WorkingArea.Size;
this.WindowState = FormWindowState.Maximized;

Very late coming to this but in .net 4.7.2 I just used很晚才知道,但在 .net 4.7.2 中我刚使用

Rectangle wa = Screen.FromControl(this).WorkingArea;
this.MaximumSize = new Size(wa.Width, wa.Height);
WindowState = FormWindowState.Maximized;

The clues are all in the previous posts, but this seems more succinct.线索都在之前的帖子里,不过这次好像更简洁了。

I am VERY late for the party, but based on the solution suggested by ry sieł, I finally fixed my problem:我参加聚会很晚,但根据 ry sieł 建议的解决方案,我终于解决了我的问题:

  • Inside OnLoad event: OnLoad事件内部:
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Maximized;
  • Inside OnShow event: OnShow事件内部:
this.MaximumSize = this.Size;
this.FormBorderStyle = FormBorderStyle.None;

仅将此添加到表单的 OnResize 事件中

this.MaximizedBounds = Screen.GetWorkingArea(this);

I'm 6 years late but for anyone else experiencing this problem, these two lines should be placed in the Form.Load method:我迟到了 6 年,但对于遇到此问题的其他人,这两行应该放在Form.Load方法中:

System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
this.MaximizedBounds = Screen.GetWorkingArea(this);

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

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