滚轮缩放效果
public void MouseWheelEventHandler(object sender, MouseWheelEventArgs e) { double oldScalX = ((ScaleTransform)canvas.RenderTransform).ScaleX; double oldScalY = ((ScaleTransform)canvas.RenderTransform).ScaleY; var storyBoard = new Storyboard(); //var group = new TransformGroup(); //var scale = new ScaleTransform(1.5, 1.5); //group.Children.Add(scale); //group.Children.Add(new TranslateTransform(300, 300)); //canvas.RenderTransform = group; RegisterName("MainCanvas", canvas); var growAnimation = new DoubleAnimation(); QuinticEase ee = new QuinticEase(); ee.EasingMode = EasingMode.EaseOut; growAnimation.EasingFunction = ee; growAnimation.From = oldScalX; if (e.Delta > 0) { growAnimation.To = oldScalX + 0.2; } else { growAnimation.To = oldScalX - 0.2; } storyBoard.Children.Add(growAnimation); var growAnimation2 = new DoubleAnimation(); QuinticEase ee2 = new QuinticEase(); ee2.EasingMode = EasingMode.EaseOut; growAnimation2.EasingFunction = ee2; growAnimation2.From = oldScalY; if (e.Delta > 0) { growAnimation2.To = oldScalY + 0.2; } else { growAnimation2.To = oldScalY - 0.2; } storyBoard.Children.Add(growAnimation2); Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.ScaleX")); Storyboard.SetTargetProperty(growAnimation2, new PropertyPath("RenderTransform.ScaleY")); Storyboard.SetTargetName(growAnimation, "MainCanvas"); Storyboard.SetTargetName(growAnimation2, "MainCanvas"); storyBoard.Begin(this); }
添加事件代码
private void UserControl_Loaded_1(object sender, RoutedEventArgs e) { Window.GetWindow(this).MouseWheel += MouseWheelEventHandler; }