标签:
主界面的代码
<StackPanel ButtonBase.Click="Grid_Click"> <Button Content="逐渐变大缩小"/> <Button Content="鼠标移动特效" /> </StackPanel>
cs :
//这事件不做过多的解释有基础的一看就会明白 private void Grid_Click(object sender, RoutedEventArgs e) { object obj = e.OriginalSource; Button butn = null; if (obj is Button) butn = obj as Button; Type type = this.GetType();//获取当前实例 Assembly assembly = type.Assembly;//获取在其中声明的类型 //动态的实例化一个对象 Window win = (Window)assembly.CreateInstance(type.Namespace + "." + butn.Content.ToString()); win.Show(); } 下面进行第一个动画: xaml界面 <Grid> <Button Content="点击逐渐增长" Height="45" HorizontalAlignment="Left" Margin="29,37,0,0" Name="btnGrow1" VerticalAlignment="Top" Width="213" /> <Button Content="点击逐渐归位" Height="23" HorizontalAlignment="Left" Margin="86,88,0,0" Name="btnBack" VerticalAlignment="Top" Width="90" /> <Button Content="点击增长" Height="46" HorizontalAlignment="Left" Margin="98,161,0,0" Name="btnGrow" VerticalAlignment="Top" Width="78" /> </Grid> cs: public 逐渐变大缩小() { InitializeComponent(); //给button注册点击事件 btnGrow1.Click += new RoutedEventHandler(btnGrow1_Click); btnBack.Click += new RoutedEventHandler(btnBack_Click); btnGrow.Click += new RoutedEventHandler(btnGrow_Click); } //点击增长 void btnGrow_Click(object sender, RoutedEventArgs e) { DoubleAnimation widthAnimation = new DoubleAnimation() { By=50,Duration=TimeSpan.FromSeconds(0.2) }; btnGrow.BeginAnimation(Button.WidthProperty, widthAnimation); } //还原动画 void btnBack_Click(object sender, RoutedEventArgs e) { DoubleAnimation widthAmination = new DoubleAnimation(); widthAmination.Duration = TimeSpan.FromSeconds(1); DoubleAnimation heightAmimation = new DoubleAnimation(); heightAmimation.Duration = TimeSpan.FromSeconds(1); btnGrow1.BeginAnimation(Button.WidthProperty, widthAmination); btnGrow1.BeginAnimation(Button.HeightProperty, heightAmimation); } //逐渐增长事件 void btnGrow1_Click(object sender, RoutedEventArgs e) { DoubleAnimation widthAnimation = new DoubleAnimation() { To = this.Width - 30, Duration = TimeSpan.FromSeconds(1) }; DoubleAnimation heightAnimation = new DoubleAnimation() { To=(this.Height-40)/3, Duration=TimeSpan.FromSeconds(1) }; btnGrow1.BeginAnimation(Button.WidthProperty, widthAnimation); btnGrow1.BeginAnimation(Button.HeightProperty, heightAnimation); }
标签:
原文地址:http://www.cnblogs.com/imeiba/p/5697975.html