1. 程式人生 > >wpf的動畫實現效果

wpf的動畫實現效果

l  動畫效果

 

               <!--滑鼠移入時動畫效果-->

               <EventTriggerRoutedEvent="MouseEnter">

                   <BeginStoryboard>

                        <Storyboard>

                            <DoubleAnimation Storyboard.TargetProperty="Width"To="100" Duration="0:0:1"></

DoubleAnimation>

                            <DoubleAnimation Storyboard.TargetProperty="Height"To="50" Duration="0:0:1"></DoubleAnimation>

                        </Storyboard>

                   </BeginStoryboard>

               </EventTrigger>

               <!--滑鼠移入時動畫效果

-->

               <EventTriggerRoutedEvent="MouseLeave">

                   <BeginStoryboard>

                        <Storyboard>

                            <DoubleAnimation Storyboard.TargetProperty="Width"To="200" Duration="0:0:0.5"></DoubleAnimation>

                            <

DoubleAnimation Storyboard.TargetProperty="Height"To="30" Duration="0:0:1"></DoubleAnimation>

                        </Storyboard>

                   </BeginStoryboard>

               </EventTrigger>               

   </Style.Triggers>

l  動畫漸變效果 以方形為例

<Style x:Key="mybuttonall"TargetType="Button">

            <Setter Property="Background"Value="#abcdff"></Setter>

            <Setter Property="Foreground"Value="#fff"></Setter>

            <!--重寫按鈕的模板樣式-->

            <Setter Property="Template">

               <Setter.Value>

                   <ControlTemplate>

                        <!--<BorderCornerRadius="10 10 10 10" Background="{TemplateBindingBackground}">

                           <ContentPresenter Content="{TemplateBindingContentControl.Content}" VerticalAlignment="Center"HorizontalAlignment="Center"></ContentPresenter>

                        </Border>-->

                        <!--動畫漸變效果方形-->

                        <Rectangle>

                            <Rectangle.Fill>

                                <LinearGradientBrush StartPoint="0.3,0"EndPoint="0.5,1">

                                    <GradientStop Color="#abcdff"Offset="0.1"></GradientStop>

                                    <GradientStop Color="#fff"Offset="0.6"></GradientStop>

                                    <GradientStop Color="#ffabcd"Offset="0.9"></GradientStop>

                                </LinearGradientBrush>

                            </Rectangle.Fill>

                        </Rectangle>

                   </ControlTemplate>

               </Setter.Value>

            </Setter>

            <!--觸發器-->

            <Style.Triggers>

               <!--滑鼠移入事件-->

               <!--<Trigger Property="IsPressed"Value="true">

                   <Setter Property="Background"Value="#ffabcd"></Setter>

               </Trigger>-->

               <!--滑鼠移出事件-->

               <!--<Trigger Property="IsMouseOver"Value="true">

                   <Setter Property="Background"Value="#ff5555"></Setter>

               </Trigger>-->

               <!--多條件組合觸發-->

               <MultiTrigger>

                   <MultiTrigger.Conditions>

                        <Condition Property="IsMouseOver"Value="true"></Condition>

                        <Condition Property="IsPressed"Value="false"></Condition>

                   </MultiTrigger.Conditions>

                   <Setter Property="Background"Value="#ff5555"></Setter>

               </MultiTrigger>

               <!--滑鼠移入時動畫效果-->

               <EventTriggerRoutedEvent="MouseEnter">

                   <BeginStoryboard>

                        <Storyboard>

                            <DoubleAnimation Storyboard.TargetProperty="Width"To="100" Duration="0:0:1"></DoubleAnimation>

                            <DoubleAnimation Storyboard.TargetProperty="Height"To="50" Duration="0:0:1"></DoubleAnimation>

                        </Storyboard>

                   </BeginStoryboard>

               </EventTrigger>

               <!--滑鼠移入時動畫效果-->

               <EventTriggerRoutedEvent="MouseLeave">

                   <BeginStoryboard>

                        <Storyboard>

                            <DoubleAnimation Storyboard.TargetProperty="Width"To="200" Duration="0:0:0.5"></DoubleAnimation>

                            <DoubleAnimation Storyboard.TargetProperty="Height"To="30" Duration="0:0:1"></DoubleAnimation>

                        </Storyboard>

                   </BeginStoryboard>

               </EventTrigger>               

            </Style.Triggers>

</Style>

l  多執行緒實現動畫lambda

void mybtn_MouseLeave(objectsender,MouseEventArgs e)

        {

            //建立一個執行緒使用lambda表示式

            Thread threa = newThread(() =>

            {

               //設定休息的時間

               Thread.Sleep(1000);

               mybtn.Dispatcher.Invoke(() =>

               {

                   //用程式碼來觸發點選事件

                   //RoutedEventArgs rea = new RoutedEventArgs();

                   //滑鼠移入事件

                   MouseEventArgs mea= new MouseEventArgs(Mouse.PrimaryDevice, 0);

                   //指定觸發事件的型別

                   mea.RoutedEvent= Button.MouseEnterEvent;

                   mybtn.RaiseEvent(mea);

               });

           });

            threa.Start();

        }

        ///<summary>

        ///滑鼠移入

        ///</summary>

        ///<paramname="sender"></param>

        ///<paramname="e"></param>

        voidmybtn_MouseEnter(object sender,MouseEventArgs e)

        {

            //建立一個執行緒使用lambda表示式

            Thread threa = newThread(() =>

            {

               Thread.Sleep(1000);

               mybtn.Dispatcher.Invoke(() =>

               {

                   //用程式碼來觸發點選事件

                   //RoutedEventArgs rea = new RoutedEventArgs();

                   //滑鼠移入事件

                   MouseEventArgs mea= new MouseEventArgs(Mouse.PrimaryDevice, 0);

                   //指定觸發事件的型別

                   mea.RoutedEvent= Button.MouseLeaveEvent;

                   mybtn.RaiseEvent(mea);

               });

           });

            threa.Start();

        }