【问题标题】:WPF: How to achieve an automatic full screen that not hides the taskbar, with no resize and no window style?WPF:如何实现不隐藏任务栏、不调整大小、不设置窗口样式的自动全屏?
【发布时间】:2016-03-04 21:09:37
【问题描述】:

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

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

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;
    }
}

结果如下:

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

【问题讨论】:

  • 计算任务栏高度并设置窗口高度=(屏幕高度-任务栏高度)
  • 为什么不只使用WindownState?。最大化状态应该为您完成所有工作。
  • @icebat - 因为我不想要顶部的“x”按钮...
  • @null1941- 谢谢,我会试试的。但是有没有更...​​优雅的解决方案?

标签: c# wpf fullscreen


【解决方案1】:

这可以通过调用非托管代码来完成。查看this 文章以了解如何操作。基本上,只需从代码隐藏中删除您的宽度和高度设置,实现文章中的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"

但不确定这些是否优雅。

【讨论】:

  • 谢谢。为什么要把它停靠在左上角?
  • 因为窗口没有最大化,只有工作区的宽度和高度,也就是没有任务栏的屏幕区域。因此,如果您将其放置在其他任何位置,它将超出工作区域范围或超出任务栏。
猜你喜欢
  • 2021-10-11
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 2019-06-29
  • 2011-03-20
相关资源
最近更新 更多