WPF 窗体最大化、最小化、还原 | WPF 最大化/最小化 按钮图标切换

  • UI界面:
<Window x:Class="Test.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:Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="275" Width="325" 
        WindowStyle="None" AllowsTransparency="True" 
        WindowStartupLocation="CenterOwner" Background="{x:Null}">
    <Border Style="{DynamicResource BorderStyle}" MouseLeftButtonDown="Border_MouseLeftButtonDown">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button x:Name="MinButton" Style="{DynamicResource MinButtonStyle}" Click="MinButton_Click"/>
                <Button x:Name="MaxButton" Style="{DynamicResource MaxButtonStyle}" Click="MaxButton_Click"/>
                <Button x:Name="CloseButton" Style="{DynamicResource CloseButtonStyle}" Click="CloseButton_Click"/>
            </StackPanel>
        </Grid>
    </Border>
</Window>
  • 资源字典:名字可自定义

关于字体图标部分有不懂的可以参考 WPF 字体图标的使用 - l夏目l - 博客园

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--背景-->
    <Style x:Key="BorderStyle" TargetType="{x:Type Border}">
        <Setter Property="CornerRadius" Value="5"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect ShadowDepth="0" BlurRadius="10" Opacity="0.4"/>
            </Setter.Value>
        </Setter>
    </Style>
    
    <!--关闭按钮-->
    <Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Content" Value="&#xe67c;"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Margin" Value="4 0"/>
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="Template" Value="{DynamicResource ButtonTemplate}"/>
    </Style>

    <!--最小化按钮-->
    <Style x:Key="MinButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Content" Value="&#xe6af;"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Margin" Value="4 0"/>
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="Template" Value="{DynamicResource ButtonTemplate}"/>
    </Style>

    <!--最大化按钮-->
    <Style x:Key="MaxButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Content" Value="&#xe6c7;"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Margin" Value="4 0"/>
        <Setter Property="FontSize" Value="18"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" CornerRadius="5,5,5,5" Width="25" Height="25"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <TextBlock FontFamily="{DynamicResource Iconfont}" Text="{TemplateBinding Content}" 
                                       HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="{TemplateBinding FontSize}"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" TargetName="border" Value="#26000000"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" TargetName="border" Value="#4D000000"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!--最大化、还原状态|图标切换-->
        <Style.Triggers>
            <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Value="Maximized">
                <Setter Property="Content" Value="&#xe6c9;"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Value="Normal">
                <Setter Property="Content" Value="&#xe6c7;"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
        <Border x:Name="border" CornerRadius="5,5,5,5" Width="25" Height="25"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
                <TextBlock FontFamily="{DynamicResource Iconfont}" Text="{TemplateBinding Content}" 
                                       HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="{TemplateBinding FontSize}"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" TargetName="border" Value="#26000000"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Background" TargetName="border" Value="#4D000000"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>
  • 后台:CS代码
using System.Windows;
using System.Windows.Input;

namespace Test
{
    /// <summary>
    /// The interaction logic of MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.MaxHeight = SystemParameters.PrimaryScreenHeight;//防止最大化时系统任务栏被遮盖
        }

        /// <summary>
        /// 窗口移动
        /// </summary>
        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        /// <summary>
        /// 最小化按钮
        /// </summary>
        private void MinButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 最大化按钮
        /// </summary>
        private void MaxButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

效果:

原代码:https://download.csdn.net/download/qq3141629907/85814276

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

丨XIAMU丨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值