你的位置:首页 > ASP.net教程

[ASP.net教程]WPF动画制作简单的按钮动画


主界面的代码

<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);    }

好了现在的一个简单点击放大缩小的动画就做好了
小结:1,本例中在菜单窗体中用到了用到了一个 ButtonBase.Click特别说明一下这个事件很好用如果一个界面上的按钮很多
每个事件都需要弹出一个窗体用这个最好用,节省代码,
2,在WPF中要实现动画就需要DoubleAnimation这个类,按照自己的想法从初始状态From定义到结束To状态在多长事件内完成Duration
最后需要一个控件来触发动画例如:
   btnGrow1.BeginAnimation(Button.WidthProperty, widthAnimation);
这段代码的意思就是
btnGrow1控件点击是触发widthAnimation这个动画,影响的对象就是这个控件的width
好了就写到这里,以后还会有,wpf,很有意思,相比起winform来他的界面更加灵活,代码编写也更加灵活,
界面的色彩也很好定义,另外动画也可以下载a