实现功能 :当鼠标移上去的时候按钮变大,离开的时候恢复。
1 <Window 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 x:Class="WPFtest.Window1" 5 x:Name="Window" 6 Title="Window1" 7 Width="640" Height="480"> 8 <Window.Resources> 9 <Color x:Key="Color1">#FFEBD3D3</Color> 10 <Style x:Key="ButtonStyle1" BasedOn="{x:Null}" TargetType="{x:Type Button}"> 11 <Setter Property="Template"> 12 <Setter.Value> 13 <ControlTemplate TargetType="{x:Type Button}"> 14 <Grid> 15 <Rectangle RenderTransformOrigin="0.5,0.5" Stroke="#FF000000" RadiusX="18.5" RadiusY="18.5"> 16 <Rectangle.RenderTransform> 17 <TransformGroup> 18 <ScaleTransform ScaleX="1" ScaleY="1"/> 19 <SkewTransform AngleX="0" AngleY="0"/> 20 <RotateTransform Angle="-0.019"/> 21 <TranslateTransform X="0" Y="0"/> 22 </TransformGroup> 23 </Rectangle.RenderTransform> 24 <Rectangle.Fill> 25 <LinearGradientBrush SpreadMethod="Pad" EndPoint="0.5,1" StartPoint="0.5,0"> 26 <GradientStop Color="#FF000000" Offset="0"/> 27 <GradientStop Color="{DynamicResource Color1}" Offset="1"/> 28 </LinearGradientBrush> 29 </Rectangle.Fill> 30 </Rectangle> 31 <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/> 32 </Grid> 33 <ControlTemplate.Triggers> 34 <Trigger Property="IsFocused" Value="True"/> 35 <Trigger Property="IsDefaulted" Value="True"/> 36 <Trigger Property="IsMouseOver" Value="True"/> 37 <Trigger Property="IsPressed" Value="True"/> 38 <Trigger Property="IsEnabled" Value="False"/> 39 </ControlTemplate.Triggers> 40 </ControlTemplate> 41 </Setter.Value> 42 </Setter> 43 </Style> 44 <Storyboard x:Key="Storyboard1"> 45 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"> 46 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 47 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.725"/> 48 </DoubleAnimationUsingKeyFrames> 49 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"> 50 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 51 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.725"/> 52 </DoubleAnimationUsingKeyFrames> 53 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"> 54 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/> 55 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.875"/> 56 </DoubleAnimationUsingKeyFrames> 57 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"> 58 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/> 59 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="-0.5"/> 60 </DoubleAnimationUsingKeyFrames> 61 </Storyboard> 62 <Storyboard x:Key="Storyboard2"> 63 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="button"> 64 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.725"/> 65 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/> 66 </DoubleAnimationUsingKeyFrames> 67 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="button"> 68 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.725"/> 69 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/> 70 </DoubleAnimationUsingKeyFrames> 71 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="button"> 72 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1.875"/> 73 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/> 74 </DoubleAnimationUsingKeyFrames> 75 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="button"> 76 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="-0.5"/> 77 <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/> 78 </DoubleAnimationUsingKeyFrames> 79 </Storyboard> 80 </Window.Resources> 81 <Window.Triggers> 82 <EventTrigger RoutedEvent="FrameworkElement.Loaded"/> 83 <EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="button"> 84 <BeginStoryboard Storyboard="{StaticResource Storyboard1}" x:Name="Storyboard1_BeginStoryboard"/> 85 </EventTrigger> 86 <EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="button"> 87 <BeginStoryboard Storyboard="{StaticResource Storyboard2}" x:Name="Storyboard2_BeginStoryboard"/> 88 </EventTrigger> 89 </Window.Triggers> 90 91 <Grid x:Name="LayoutRoot"> 92 <Button HorizontalAlignment="Left" Margin="82,63,0,0" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Top" Width="110" Height="40" Content="按钮" Foreground="#FFF9F6F9" x:Name="button" RenderTransformOrigin="0.5,0.5"> 93 <Button.RenderTransform> 94 <TransformGroup> 95 <ScaleTransform ScaleX="1" ScaleY="1"/> 96 <SkewTransform AngleX="0" AngleY="0"/> 97 <RotateTransform Angle="0"/> 98 <TranslateTransform X="0" Y="0"/> 99 </TransformGroup> 100 </Button.RenderTransform> 101 </Button> 102 </Grid> 103 </Window>
时间: 2024-10-03 23:00:47