- WindowStyle="None"时可以做到去掉系统自带标题栏,但是此时在顶部会出现一个蓝条,代码如下:
<Window x:Class="WpfApplication1.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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="None" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<Grid>
<TextBlock Text="无标题测试" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
如下图:
2.如果想去除该蓝条可以设置AllowsTransparency=“True”,代码如下:
<Window x:Class="WpfApplication1.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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="None" AllowsTransparency="True" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<Grid>
<TextBlock Text="无标题测试" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
效果如下图:
问题是此时窗体也会失去所有自带的窗体控制,如:最大化、最小化、窗体缩放
3.为了使窗体可以缩放,可以设置ResizeMode=“CanResizeWithGrip”,代码如下:
<Window x:Class="WpfApplication1.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"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="None" AllowsTransparency="True" ResizeMode="CanResizeWithGrip" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<Grid>
<TextBlock Text="无标题测试" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
如下图:
问题是此时窗体右下角会出现一个小三角,见上图右下角;并且此时的缩放只能拖动右下角的小三角
4.换种思路,使用WindowChrome自定义标题栏,WindowChrome是在.NET Framework 4.5中引入System.Windows.Shell的,使用时需要先引入System.Windows.Shell,之后对WindowChrome属性进行设置,此时的WindowStyle=“SingleBorderWindow” ResizeMode=“CanResize”,移除AllowsTransparency属性,代码如下:
<Window x:Class="WpfApplication1.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"
xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="SingleBorderWindow" ResizeMode="CanResize" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
CornerRadius="0"
GlassFrameThickness="0"
NonClientFrameEdges="None"
UseAeroCaptionButtons="False"/>
</shell:WindowChrome.WindowChrome>
<Grid>
<TextBlock Text="无标题测试" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
此时可以正常的拖动及缩放窗体,见下图:
5.此时会引入新问题,在窗体最大化时有部分窗体会被任务栏遮盖,修改示例代码以展示此问题,代码如下:
<Window x:Class="WpfApplication1.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"
xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="SingleBorderWindow" ResizeMode="CanResize" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
CornerRadius="0"
GlassFrameThickness="0"
NonClientFrameEdges="None"
UseAeroCaptionButtons="False"/>
</shell:WindowChrome.WindowChrome>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="30" Background="Gray" Grid.Row="0"/>
<TextBlock Text="无标题测试 无标题测试 无标题测试" Grid.Row="2"/>
</Grid>
</Window>
此时正常大小如下图:
最大化后效果如下图:
可以明显看出左下角文字被任务栏遮盖
6. 为了解决此问题,需要在窗体中引入模版,在最大化时设置窗体的MaxWidth和MaxHeight,代码如下(因为之前示例代码没有设置Background,此段模版设置Background为白色):
<Window x:Class="WpfApplication1.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"
xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="SingleBorderWindow" ResizeMode="CanResize" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
CornerRadius="0"
GlassFrameThickness="0"
NonClientFrameEdges="None"
UseAeroCaptionButtons="False"/>
</shell:WindowChrome.WindowChrome>
<Window.Template>
<ControlTemplate TargetType="{x:Type Window}">
<Border Background="White" Name="win_content" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="WindowState" Value="Maximized">
<Setter Property="Margin" TargetName="win_content" Value="8"/>
<!--二选一-->
<Setter Property="MaxWidth" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Width}" />
<Setter Property="MaxHeight" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Height}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Template>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="30" Background="Gray" Grid.Row="0"/>
<TextBlock Text="无标题测试 无标题测试 无标题测试" Grid.Row="2"/>
</Grid>
</Window>
显示效果如下图,此时已经可以正常显示全部窗体:
7.此时测试引入新的问题,在自定义的标题栏中添加按钮,按钮无法点击,为了解决此问题,需要在控件属性中添加WindowChrome.IsHitTestVisibleInChrome=“True”,代码如下:
<Window x:Class="WpfApplication1.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"
xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="SingleBorderWindow" ResizeMode="CanResize" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
CornerRadius="0"
GlassFrameThickness="0"
NonClientFrameEdges="None"
UseAeroCaptionButtons="False"/>
</shell:WindowChrome.WindowChrome>
<Window.Template>
<ControlTemplate TargetType="{x:Type Window}">
<Border Background="White" Name="win_content" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="WindowState" Value="Maximized">
<Setter Property="Margin" TargetName="win_content" Value="8"/>
<!--二选一-->
<Setter Property="MaxWidth" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Width}" />
<Setter Property="MaxHeight" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Height}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Template>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="30" Background="Gray" Grid.Row="0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Content="X" Width="20" Height="20" Margin="0,0,20,0" Grid.Column="1" WindowChrome.IsHitTestVisibleInChrome="True"/>
</Grid>
</Border>
<TextBlock Text="无标题测试 无标题测试 无标题测试" Grid.Row="2"/>
</Grid>
</Window>
在此段代码中,也可以将WindowChrome.IsHitTestVisibleInChrome="True"添加到button上层的border控件中,则此时会在整个border屏蔽系统提供的鼠标操作,如双击最大化、窗体拖动等。为了解决此问题,可以在border控件上自定义鼠标操作,如窗体拖动可以添加MouseMove事件的实现,代码如下:
<Window x:Class="WpfApplication1.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"
xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" WindowStyle="SingleBorderWindow" ResizeMode="CanResize" Top="500" Left="500" BorderBrush="Black" BorderThickness="1">
<shell:WindowChrome.WindowChrome>
<shell:WindowChrome
CornerRadius="0"
GlassFrameThickness="0"
NonClientFrameEdges="None"
UseAeroCaptionButtons="False"/>
</shell:WindowChrome.WindowChrome>
<Window.Template>
<ControlTemplate TargetType="{x:Type Window}">
<Border Background="White" Name="win_content" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="WindowState" Value="Maximized">
<Setter Property="Margin" TargetName="win_content" Value="8"/>
<!--二选一-->
<Setter Property="MaxWidth" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Width}" />
<Setter Property="MaxHeight" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Height}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Template>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Height="30" Background="Gray" Grid.Row="0" MouseMove="Border_MouseMove" WindowChrome.IsHitTestVisibleInChrome="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Content="X" Width="20" Height="20" Margin="0,0,20,0" Grid.Column="1"/>
</Grid>
</Border>
<TextBlock Text="无标题测试 无标题测试 无标题测试" Grid.Row="2"/>
</Grid>
</Window>
Border_MouseMove实现如下:
private void Border_MouseMove(object sender, MouseEventArgs e)
{
if (e.MouseDevice.LeftButton == MouseButtonState.Pressed)
DragMove();
}