我正在开发 WPF 应用程序,我遇到的问题是应用程序何时WindowStyle=None
位于WindowState = WindowState.Maximized
顶部或左侧放置的任务栏下方。
当任务栏位于底部或右侧时,一切正常。
我知道窗口Left
的Top
属性,但它们在Maximized
状态中被忽略了。
此外,它还Microsoft.Windows.Shell.WindowСhrome
提供拖动、双击以最大化和恢复、捕捉和取消捕捉的能力。(需要添加为 dll 引用)
我想实现我的应用程序不会隐藏或进入任务栏,并且可以与WindowСhrome
提供的行为一起正常工作。
主窗口.xaml
<Window x:Class="WpfAppTestFullScreen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
WindowStyle="None"
Title="MainWindow" Height="350" Width="525"
Left="100" Top="100">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="{Binding ActualHeight,ElementName=topBarGrid}"/>
</WindowChrome.WindowChrome>
<Grid x:Name="mainGrid" Background="Yellow">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid x:Name="topBarGrid" Grid.Row="0" >
<Border BorderBrush="Black" BorderThickness="1" >
<DockPanel x:Name="panelForWindowControls"
VerticalAlignment="Stretch"
DockPanel.Dock="Right"
LastChildFill="False"
>
<Button Name="buttonExit"
Width="43" Height="28"
Margin="0" Click="buttonExit_Click"
DockPanel.Dock="Right" Content="x"
WindowChrome.IsHitTestVisibleInChrome="True"
/>
<Button Name="buttonMax"
Width="43" Height="28"
Margin="0" Click="buttonMax_Click"
DockPanel.Dock="Right" Content="[]"
WindowChrome.IsHitTestVisibleInChrome="True"
/>
<Button Name="buttonMin"
Width="43" Height="28"
Margin="0" Click="buttonMin_Click"
DockPanel.Dock="Right" Content="_"
WindowChrome.IsHitTestVisibleInChrome="True"
/>
</DockPanel>
</Border>
</Grid>
<Grid x:Name="bodyGrid" Grid.Row="1">
<Button Content="FullScreen" x:Name="FullScreenButton"
Height="50" Width="200"
HorizontalAlignment="Center" VerticalAlignment="Center"
Click="FullScreenButton_Click" />
</Grid>
</Grid>
</Window>
主窗口.xaml.cs
using System.Windows;
namespace WpfAppTestFullScreen
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
}
private void FullScreenButton_Click(object sender, RoutedEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
}
else
{
WindowState = WindowState.Maximized;
}
}
private void buttonMax_Click(object sender, RoutedEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
}
else
{
WindowState = WindowState.Maximized;
}
}
private void buttonMin_Click(object sender, RoutedEventArgs e)
{
WindowState = (WindowState == WindowState.Minimized) ? WindowState.Normal : WindowState.Minimized;
}
private void buttonExit_Click(object sender, RoutedEventArgs e)
{
}
}
}