WPF 最大化,最小化,关闭,拖拽,双击事件、及Button 样式练习
十年河东,十年河西,莫欺少年穷
学无止境,精益求精
xaml 如下:
<Window x:Class="WpfApp8.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:WpfApp8" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <Style x:Key="btn" TargetType="Button"> <Setter Property="Background" Value="#FFF514AE"/> <Setter Property="Foreground" Value="Yellow"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="FontFamily" Value="微软雅黑"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="border" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true"> <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsDefaulted" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" TargetName="border" Value="Black"/> </Trigger> <Trigger Property="IsPressed" Value="true"> <Setter Property="Background" TargetName="border" Value="Green"/> <Setter Property="BorderBrush" TargetName="border" Value="red"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Button Content="click me" Width="200" Height="40" Style="{StaticResource btn}"/> </Grid> </Window>
代码如下
public MainView() { InitializeComponent(); //最小化 btnMin.Click += (s, e) => { this.WindowState = WindowState.Minimized; }; //最大化 btnMax.Click += (s, e) => { if (this.WindowState == WindowState.Normal) { this.WindowState = WindowState.Maximized; } else { this.WindowState = WindowState.Normal; } }; //关闭事件 btnClose.Click += (s, e) => { this.Close(); }; //鼠标拖拽事件 ColorZone.MouseMove += (s, e) => { if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); } }; //鼠标双击导航栏事件 ColorZone.MouseDoubleClick += (s, e) => { if (this.WindowState == WindowState.Normal) { this.WindowState = WindowState.Maximized; } else { this.WindowState = WindowState.Normal; } }; }