应用程序初始化后最大化时,样式为无的 WPF 窗口覆盖任务栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1464908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:01:48  来源:igfitidea点击:

WPF Window with Style=None cover taskbar when Maximised after app initialization

wpflayoutresize

提问by Simon Fox

I want to achieve the same effect as Windows Media Player or Browser based Flash players which take up the ENTIRE (not even the taskbar is visible) real estate when maximized.

我想达到与 Windows Media Player 或基于浏览器的 Flash 播放器相同的效果,它们在最大化时占据整个(甚至任务栏都不可见)的空间。

This works fine if the WindowStateis set to Maximizedand the WindowStyleis set to Nonein XAML so the app is started in that state. Problem is I want to start the app in a bordered window and when the user chooses, maximize as specified above. In the StateChangedhandler I check for Maximizedstate and if this is the case I set the WindowStyleto None. This has the effect of maximizing the window but NOT covering the taskbar. The following code will make this work as I want but its a hack and I'd like to clean it up:

如果这工作得很好WindowState被设置为MaximizedWindowStyle被设定为None这样的应用程序处于该状态开始XAML。问题是我想在带边框的窗口中启动应用程序,当用户选择时,按照上面的指定最大化。在StateChanged处理我检查Maximized状态,如果这是我设置的情况下WindowStyleNone。这具有最大化窗口但不覆盖任务栏的效果。以下代码将按照我的意愿进行这项工作,但它是一个黑客,我想清理它:

if (WindowState == WindowState.Maximized)
{
    m_videoWindow.Maximize();

    WindowStyle = WindowStyle.None;

    //the following makes this work but I would like to clean it up
    Hide();
    Show();
}

EDITThis(from 2006 when still in CTP) mentions the problem and someone from MS states they hope to improve full screen support in the next version, have these improvements been made?

编辑这个(从 2006 年仍然在 CTP 中)提到了这个问题,来自 MS 的人表示他们希望在下一个版本中改进全屏支持,这些改进了吗?

回答by Eduardo Molteni

This article explains it all: Maximizing window (with WindowStyle=None) considering Taskbar.

这篇文章解释了这一切:考虑 Taskbar 最大化窗口(使用 WindowStyle=None)

Also worth checking out: Custom Window Chrome in WPF.

同样值得一试:WPF 中的自定义窗口 Chrome

Edit: Now new, is the WPF Shell Integration Library that allows complete restyle of the window chrome without the headaches of reimplementing move, resizing, etc.

编辑:现在新的是 WPF Shell 集成库,它允许完全重新设计窗口镶边,而无需重新实现移动、调整大小等。

Edit 2015: Shell Integration Library is now integrated in WPF and MS retired the code

2015 年编辑:Shell 集成库现已集成到 WPF 中,MS 已停用代码

回答by Chaz

I found I could maximize to full screen (covering the taskbar) by setting the properties when creating the window (in xaml), but was not able to switch back and forth after creation. After some experimenting, I found the order the properties are set seems to matter:

我发现我可以通过在创建窗口(在 xaml 中)时设置属性来最大化到全屏(覆盖任务栏),但在创建后无法来回切换。经过一些试验,我发现设置属性的顺序似乎很重要:

public bool IsFullscreen
{
    get 
    {
        return WindowState == System.Windows.WindowState.Maximized
            && ResizeMode == System.Windows.ResizeMode.NoResize
            && WindowStyle== System.Windows.WindowStyle.None;
    }
    set
    {
        if ( value )
        {
            ResizeMode = System.Windows.ResizeMode.NoResize;
            WindowStyle = System.Windows.WindowStyle.None;
            WindowState = System.Windows.WindowState.Maximized;
        }
        else
        {
            ResizeMode = System.Windows.ResizeMode.CanResize;
            WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
            WindowState = System.Windows.WindowState.Normal;            
        }
    }
}

Note that WindowState comes lastin the setter.

请注意WindowState在 setter 中排在最后

回答by x16forest

To get this to properly work in my WPF/.NET 4.0 application I am calling this function whenever I enter or exit full screen mode:

为了让它在我的 WPF/.NET 4.0 应用程序中正常工作,我在进入或退出全屏模式时调用此函数:

private static void RefreshWindowVisibility(Window window)
        {
            if (window.OriginalWindowState == WindowState.Maximized)
            {
                window.Hide();
                window.Show();
                window.BringIntoView();
            }
        }

There is a flicker associated with this method, but it seems the same flicker exists when going to full screen mode on Chrome. Internet Explorer seems to take a different approach.

有一个与此方法相关的闪烁,但在 Chrome 上进入全屏模式时似乎存在相同的闪烁。Internet Explorer 似乎采取了不同的方法。

回答by Fix

I don't know if this is ok for you, but you can resize the window to have the same size than the working area (that is, in most cases, all the screen except the taskbar) and locate it at 0,0 (top-left corner):

我不知道这是否适合您,但是您可以将窗口大小调整为与工作区域相同的大小(即,在大多数情况下,除任务栏外的所​​有屏幕)并将其定位在 0,0 (左上角):

Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width; 
Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height; 
Left = 0; 
Top = 0;

The exact definition for the WorkingArea property (from MSDN) is:

WorkingArea 属性的确切定义(来自 MSDN)是:

Gets the working area of the display. The working area is the desktop area of the display, excluding taskbars, docked windows, and docked tool bars.

获取显示的工作区域。工作区是显示器的桌面区域,不包括任务栏、停靠窗口和停靠工具栏。

Hope it helps

希望能帮助到你