WPF:如何实现不隐藏任务栏、不调整大小、没有window样式的自动全屏?

WPF: How to achieve an automatic full screen that not hides the taskbar, with no resize and no window style?

如何实现不隐藏任务栏、不调整大小、无window样式的自动全屏?

我试过使用下面的代码,但是不能正常工作,如下图所示。

XAML:

<Window x:Class="WPF_Test_Environment.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowState="Maximized" ResizeMode="NoResize" WindowStyle="None">
    <DockPanel>
        <Button DockPanel.Dock="Top" Height="50">Top</Button>
        <Button DockPanel.Dock="Bottom" Height="50">Bottom</Button>
        <Button DockPanel.Dock="Left" Width="50">Left</Button>
        <Button DockPanel.Dock="Right" Width="50">Right</Button>
        <Button Width="50" Height="50">Center</Button>
    </DockPanel>
</Window>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        MinHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
        MinWidth = SystemParameters.MaximizedPrimaryScreenWidth;
    }
}

结果如下:

如您所见,"Bottom" 按钮部分位于任务栏下方,我希望它完全位于任务栏上方。因此,理想情况下,它看起来如下图所示,但顶部没有边框:

可以通过调用非托管代码来完成。查看 this 文章了解如何操作。基本上,只需从代码隐藏中删除宽度和高度设置,实现文章中的 WindowSizing class 并从 window.[=15 的 SourceInitialized 事件中调用它=]

编辑

另一个解决方案是添加对 Windows 表单的引用并使用:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Set the width and height values to the values of working area
    this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
    this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
    // Move the window to top left corner of the screen
    this.Left = 0;
    this.Top = 0;   
}

只需确保从您的 window 中删除 WindowState="Maximized"

但不确定这些是否优雅。