ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-06-28 19:06:56  阅读:372  来源: 互联网

标签:最大化 sender void object private WindowState 最小化 WPF MainWindow


  • 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>
  • 资源字典:名字可自定义

关于字体图标部分有不懂的可以参考 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>
  • 后台: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(); } } }

效果:

正常

 

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有