WPF 简单重写最小化 最大化 关闭按钮

按钮重写

大部分情况下,原生的WPF边框不好看,我们都会选择重写边框,可以得到自己想要的样式。

1.清除边框样式

在这里插入图片描述2. 找到最大化,最小化,关闭按钮的图标
这边建议图标去阿里云的icon网站去找,有很多免费的优质图标。https://www.iconfont.cn/
在这里插入图片描述
3. 进入搜索你想要的图标,点击购物车的按钮。
在这里插入图片描述
4. 点击添加项目,如果没有项目就自己建一个项目文件夹。
在这里插入图片描述
进入该项目文件夹,点击下载至本地,位置随便你放在哪里。
5. 更改图标
在项目中新建一个文件夹,点击添加现有项,进入刚刚下载,解压之后的文件夹,更改为所有文件,
选择后缀为ttf的文件
在这里插入图片描述
6. 项目中 引用
因为有三个按钮,并且样式差不多,这里采用资源字典的方式进行样式的编写

公共样式

 <Style TargetType="Button" x:Key="WindowControlButtonStyle">
        <Setter Property="Width" Value="50"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="Transparent" Name="back">
                        <TextBlock Text="{Binding Content,RelativeSource={RelativeSource AncestorType=Button,Mode=FindAncestor}}"
                       VerticalAlignment="Center" HorizontalAlignment="Center"
                       FontFamily="../Assets/Fonts/#iconfont" FontSize="24"></TextBlock>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="back" Property="Background" Value="#22FFFFFF"></Setter>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="back" Property="Background" Value="#44FFFFFF"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

前端代码
注意!!!! content中的字符是在对应图标的代码
在这里插入图片描述

<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Height="50" Width="150" Grid.Column="1">
                <Button Style="{StaticResource WindowControlButtonStyle}" Content="&#xe64c;" Click="MinSizeButton_Click"/>
                <Button Style="{StaticResource WindowControlButtonStyle}" Content="&#xe607;" Click="MaxSizeButton_Click"/>
                <Button Style="{StaticResource WindowControlButtonStyle}" Content="&#xe615;" Click="CloseButton_Click"/>
            </StackPanel>

后端代码

 private void MinSizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void MaxSizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = this.WindowState == WindowState.Maximized ?
                WindowState.Normal : WindowState.Maximized;
        }

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

在这里就已经全部完成了,简单的重写。

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

B&Y

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

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

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

打赏作者

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

抵扣说明:

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

余额充值