WPF Animation: simple button animation and wpf animation creation button
Main Interface code
<StackPanel ButtonBase. Click = "Grid_Click"> <Button Content = "getting bigger and smaller"/> <Button Content = "move the mouse"/> </StackPanel>
Cs:
// This event is not explained too much. If you have a basic understanding, you will understand 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 (); // obtain the current instance Assembly = type. assembly; // obtain the declared type in it // dynamically instantiate an Object Window win = (Window) assembly. createInstance (type. namespace + ". "+ butn. content. toString (); win. show () ;}the first animation is shown below: The xaml interface <Grid> <Button Content = "click to gradually increase" Height = "45" HorizontalAlignment = "Left" Margin = "29,37, 213 "Name =" btnGrow1 "verticalignment =" Top "Width =" "/> <Button Content =" click to gradually return "Height =" 23 "HorizontalAlignment =" Left "Margin = "86,88, 0, 0 "Name =" btnBack "verticalignment =" Top "Width =" 90 "/> <Button Content =" click to increase "Height =" 46 "HorizontalAlignment =" Left "Margin =" 98,161, 0, 0 "Name =" btnGrow "verticalignment =" Top "Width =" 78 "/> </Grid> cs: public is gradually becoming larger and smaller () {InitializeComponent (); // register and click the button event btnGrow1.Click + = new RoutedEventHandler (btngrow#click); btnBack. click + = new RoutedEventHandler (btnBack_Click); btnGrow. click + = new increment (btnGrow_Click);} // Click to increase void increment (object sender, RoutedEventArgs e) {DoubleAnimation widthAnimation = new DoubleAnimation () {By = 50, Duration = TimeSpan. fromSeconds (0.2)}; btnGrow. beginAnimation (Button. widthProperty, widthAnimation);} // restore the animation 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 );}
Now a simple click zoom-in and zoom-out animation is completed. Summary: 1. In this example, a ButtonBase is used in the menu form. click special note: this event is very useful. If a button on the Interface needs to pop up a form for many events, it is best to use this, saving code, 2, to implement animation in WPF, you need the DoubleAnimation class. According To your own ideas, you need a control To trigger the animation From the initial state From definition To the end To the Duration of the event. For example: btnGrow1.BeginAnimation (Button. widthProperty, widthAnimation); this Code indicates that the click of the btnGrow1 control triggers the widthAnimation animation. The affected object is the width of the control, which will be written here, very interesting. Compared with winform, its interface is more flexible, its code is more flexible, and its color is also well defined. In addition, you can download axm for animations. L, but I do not like to write this on the window sill of the interface design. If you write it in the background, you can see it clearly.