其他分享
首页 > 其他分享> > WPF 窗体最大化、最小化、还原 | WPF 最大化/最小化 按钮图标切换

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

作者:互联网

<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>

关于字体图标部分有不懂的可以参考 https://www.cnblogs.com/lxiamul/p/16284837.html

<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>
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(); } } }

效果:

正常

 

标签:最大化,sender,void,object,private,WindowState,最小化,WPF,MainWindow
来源: https://www.cnblogs.com/lxiamul/p/16420674.html