【 WPF 】重写Windows窗体样式

版权声明:欢迎分享 https://blog.csdn.net/qq_42791845/article/details/83109473

1.隐藏默认窗体样式

Title行添加 AllowsTransparency="True" WindowStyle="None"

<Window x:Class="FeederProject.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:FeederProject"
        mc:Ignorable="d"
        Title="MainWindow" Height="760" Width="1440" AllowsTransparency="True" WindowStyle="None">
    <Window.Resources>

2.创建新的窗体Grid

<Grid x:Name="WindowGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="38" Name="WindowsTitle"></RowDefinition>
        </Grid.RowDefinitions>
</Grid>

3.使窗体Title可以被鼠标点击拖动

后台代码中添加

private void Grid_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

4.窗体Title添加功能按钮(关闭,最大化,隐藏)

功能按钮样式

<Style x:Key="BtnWindowStyle" TargetType="Button">
            <Setter Property="Width" Value="30"/>
            <Setter Property="Height" Value="30"/>
            <!--<Setter Property="Margin" Value="50,15"/>-->
            <!--<Setter Property="Foreground" Value="White"/>-->
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="border" Padding="0" CornerRadius="2" Background="{TemplateBinding Background}" BorderBrush="LightGray" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                            <TextBlock x:Name="Block" FontSize="20" Text="{TemplateBinding Content}" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="White"/>
                                <Setter TargetName="border" Property="BorderThickness" Value="1"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="LightGray"/>
                                <Setter TargetName="Block" Property="Foreground" Value="Black"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

添加功能按钮

        <Button Content="T" Grid.Column="1" Margin="0,3,110,3" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource BtnWindowStyle}" Click="btnT_Click"></Button>
        <Button Content="E" Grid.Column="1" Margin="0,3,75,3" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource BtnWindowStyle}" Click="btnE_Click"></Button>
        <Button Content="A" Grid.Column="1"  Margin="0,3,40,3" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource BtnWindowStyle}" Click="btnA_Click"></Button>
        <Button Content="M" Grid.Column="1" Margin="0,3,5,3" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource BtnWindowStyle}" Click="btnM_Click"></Button>

按钮功能--后台添加

关闭窗口

private void btnM_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

最大化窗口

private void btnA_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Maximized;
        }

隐藏窗口

private void btnT_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Maximized;
        }

5.实际效果

猜你喜欢

转载自blog.csdn.net/qq_42791845/article/details/83109473
今日推荐