关于WPF自定义标题框问题记录

  1. 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();
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

砖农L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值