1. 程式人生 > >WPF DoubleAnimation設定控制元件大小、背景顏色的動畫

WPF DoubleAnimation設定控制元件大小、背景顏色的動畫

///<summary>/// 設定控制元件的【大小、背景】動畫效果,
/// 高度、寬度預設當前大小,背景預設White~LightGreen
///</summary>///<param name="control">要設定動畫的控制元件</param>///<Author> frd 2011-9-8</Author>publicstaticvoid SetDoubleAnimation(object control)
{
Type type
= control.GetType();
switch (type.Name)
{
case"Image":
{
//更多控制元件的情況,模仿編寫既是 }
break;
case"Border":
{
Border newBorder
= (Border)control;
#region 高、寬變化動畫

DoubleAnimation widthAnimation
=new DoubleAnimation(0, newBorder.Width, new Duration(TimeSpan.FromSeconds(
0.5)));
newBorder.BeginAnimation(Border.WidthProperty, widthAnimation, HandoffBehavior.Compose);

DoubleAnimation heightAnimation
=new DoubleAnimation(0, newBorder.Height, new Duration(TimeSpan.FromSeconds(0.5)));
newBorder.BeginAnimation(Border.HeightProperty, heightAnimation, HandoffBehavior.Compose);
#endregion#region 背景顏色動畫
SolidColorBrush myBrush
=new SolidColorBrush();

ColorAnimation myColorAnimation
=new ColorAnimation();
myColorAnimation.From
= Colors.White;
myColorAnimation.To
= Colors.LightGreen;
myColorAnimation.Duration
=new Duration(TimeSpan.FromSeconds(1));
myColorAnimation.AutoReverse
=true;
myColorAnimation.RepeatBehavior
= RepeatBehavior.Forever;

myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myColorAnimation, HandoffBehavior.Compose);
newBorder.Background
= myBrush;

#endregion
}
break;

default:
break;
}
}