|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Windows; |
| 5 | +using System.Windows.Controls; |
| 6 | +using System.Windows.Media.Animation; |
| 7 | + |
| 8 | +namespace On_Screen_Keyboard |
| 9 | +{ |
| 10 | + public class AnimationHandler : Page |
| 11 | + { |
| 12 | + private const int shrinkModifier = 10; |
| 13 | + private const int animationDuration = 50; |
| 14 | + private Button button; |
| 15 | + private double previousWidth; |
| 16 | + private double previousHeight; |
| 17 | + private Thickness previousMargin; |
| 18 | + public AnimationHandler(Button button) |
| 19 | + { |
| 20 | + this.button = button; |
| 21 | + previousWidth = button.Width; |
| 22 | + previousHeight = button.Height; |
| 23 | + previousMargin = button.Margin; |
| 24 | + |
| 25 | + NameScope.SetNameScope(this, new NameScope()); |
| 26 | + RegisterName(button.Name, button); |
| 27 | + } |
| 28 | + |
| 29 | + public void ButtonMouseDownAnimation(object sender, RoutedEventArgs e) |
| 30 | + { |
| 31 | + DoubleAnimation widthShrinkAnimation = new DoubleAnimation(button.Width - shrinkModifier, TimeSpan.FromMilliseconds(animationDuration)); |
| 32 | + Storyboard.SetTargetName(widthShrinkAnimation, button.Name); |
| 33 | + Storyboard.SetTargetProperty(widthShrinkAnimation, new PropertyPath(Button.WidthProperty)); |
| 34 | + |
| 35 | + Thickness margin = new Thickness(); |
| 36 | + margin.Left = shrinkModifier / 2 + button.Margin.Left; |
| 37 | + margin.Top = shrinkModifier / 2 + button.Margin.Top; |
| 38 | + margin.Right = shrinkModifier / 2 + button.Margin.Right; |
| 39 | + margin.Bottom = shrinkModifier / 2 + button.Margin.Bottom; |
| 40 | + |
| 41 | + ThicknessAnimation marginExpandAnimation = new ThicknessAnimation(margin, TimeSpan.FromMilliseconds(animationDuration)); |
| 42 | + Storyboard.SetTargetName(marginExpandAnimation, button.Name); |
| 43 | + Storyboard.SetTargetProperty(marginExpandAnimation, new PropertyPath(Button.MarginProperty)); |
| 44 | + |
| 45 | + Storyboard storyboard = new Storyboard(); |
| 46 | + storyboard.Children.Add(widthShrinkAnimation); |
| 47 | + storyboard.Children.Add(marginExpandAnimation); |
| 48 | + |
| 49 | + if (button.Name.Equals("Btn_ENTER")) //the height of Btn_ENTER is not Auto so you have to set it manually |
| 50 | + { |
| 51 | + DoubleAnimation heightShrinkAnimation = new DoubleAnimation(button.Height - shrinkModifier, TimeSpan.FromMilliseconds(animationDuration)); |
| 52 | + Storyboard.SetTargetName(heightShrinkAnimation, button.Name); |
| 53 | + Storyboard.SetTargetProperty(heightShrinkAnimation, new PropertyPath(Button.HeightProperty)); |
| 54 | + storyboard.Children.Add(heightShrinkAnimation); |
| 55 | + } |
| 56 | + |
| 57 | + storyboard.Begin(this); |
| 58 | + } |
| 59 | + |
| 60 | + public void ButtonMouseUpAnimation(object sender, RoutedEventArgs e) |
| 61 | + { |
| 62 | + DoubleAnimation widthExpandAnimation = new DoubleAnimation(previousWidth, TimeSpan.FromMilliseconds(animationDuration)); |
| 63 | + Storyboard.SetTargetName(widthExpandAnimation, button.Name); |
| 64 | + Storyboard.SetTargetProperty(widthExpandAnimation, new PropertyPath(Button.WidthProperty)); |
| 65 | + |
| 66 | + ThicknessAnimation marginShrinkAnimation = new ThicknessAnimation(previousMargin, TimeSpan.FromMilliseconds(animationDuration)); |
| 67 | + Storyboard.SetTargetName(marginShrinkAnimation, button.Name); |
| 68 | + Storyboard.SetTargetProperty(marginShrinkAnimation, new PropertyPath(Button.MarginProperty)); |
| 69 | + |
| 70 | + Storyboard storyboard = new Storyboard(); |
| 71 | + storyboard.Children.Add(widthExpandAnimation); |
| 72 | + storyboard.Children.Add(marginShrinkAnimation); |
| 73 | + |
| 74 | + if (button.Name.Equals("Btn_ENTER")) //the height of Btn_ENTER is not Auto so you have to reset it manually |
| 75 | + { |
| 76 | + DoubleAnimation heightShrinkAnimation = new DoubleAnimation(previousHeight, TimeSpan.FromMilliseconds(animationDuration)); |
| 77 | + Storyboard.SetTargetName(heightShrinkAnimation, button.Name); |
| 78 | + Storyboard.SetTargetProperty(heightShrinkAnimation, new PropertyPath(Button.HeightProperty)); |
| 79 | + storyboard.Children.Add(heightShrinkAnimation); |
| 80 | + } |
| 81 | + |
| 82 | + storyboard.Begin(this); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | +} |
0 commit comments