C# WPF:如何实现不隐藏任务栏、不调整大小和不显示窗口样式的自动全屏?

C# WPF:如何实现不隐藏任务栏、不调整大小和不显示窗口样式的自动全屏?,c#,wpf,fullscreen,C#,Wpf,Fullscreen,如何实现不隐藏任务栏、不调整大小和不显示窗口样式的自动全屏 我尝试使用下面的代码,但它不能正常工作,如下图所示 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

如何实现不隐藏任务栏、不调整大小和不显示窗口样式的自动全屏

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

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;
    }
}
结果如下:

如您所见,“底部”按钮部分位于任务栏下方,我希望它完全位于任务栏上方。因此,理想情况下,它将如下图所示,但顶部没有边框:
这可以通过调用非托管代码来完成。检查文章,看看如何做。基本上,只需从代码隐藏中删除宽度和高度设置,实现文章中的
WindowSizing
类,并从窗口的
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;   
}
只需确保从窗口中删除
WindowState=“Maximized”


但是,不确定这些是否优雅。

计算任务栏的高度并设置窗口高度=(屏幕高度-任务栏高度)为什么不只使用?。最大化状态应该为您完成所有工作。@icebat-因为我不希望顶部有“x”按钮…@null1941-谢谢,我会试试的。但是没有比这更有效的解决方案了吗。。。优雅?谢谢。为什么需要将其停靠在左上角?因为窗口没有最大化,它只有工作区的宽度和高度,即屏幕上没有任务栏的区域。因此,如果您将其放置在其他任何位置,它将超出工作区域边界或位于任务栏上方。