一、缩放要求
1.缩放对象在可视区域内,已对象的中心点进行缩放。
2.缩放对象比可视区大,并且对象各边界都在可视区外围,那么已鼠标位置进行缩放。
3.缩放对象比可视区大,但是缩放后某一边界在可视区的对应边界内,那么该边界与可视区边界对齐,对向缩放。
二、缩放代买片段
1 private void Window_MouseWheel(object sender, MouseWheelEventArgs e) { 2 if (e.Delta == 0) return; 3 4 5 double d = e.Delta / Math.Abs(e.Delta); 6 7 if (_scaleValue < 0.5 && d < 0) return; 8 9 if (_scaleValue > 20 && d > 0) return; 10 11 _scaleValue += d * .2; 12 13 //获取鼠标在缩放之前的目标上的位置 14 Point targetZoomFocus1 = e.GetPosition(target); 15 16 //获取目标在缩放之前的Rect 17 Rect beforeScaleRect = 18 target.RenderTransform.TransformBounds(new Rect(target.RenderSize)); 19 20 //缩放的中心点为左上角(0,0) 21 scaler.ScaleX = _scaleValue; 22 scaler.ScaleY = _scaleValue; 23 24 //获取鼠标在缩放之后的目标上的位置 25 Point targetZoomFocus2 = new Point(targetZoomFocus1.X * (1 + d * .2), targetZoomFocus1.Y * (1 + d * .2)); 26 27 //获取目标在缩放之后的Rect 28 Rect afterScaleRect = target.RenderTransform.TransformBounds(new Rect(target.RenderSize)); 29 30 //算的缩放前后鼠标的位置间的差 31 Vector v = targetZoomFocus2 - targetZoomFocus1; 32 33 34 if (afterScaleRect.Size.Width <= _viewportSize.Width) { 35 //缩放之后居中 36 double widthHalfDelta = (Container.RenderSize.Width - afterScaleRect.Width) / 2; 37 translater.X = widthHalfDelta; 38 } 39 else if (afterScaleRect.X - v.X > 0) { 40 //目标左边界与可视左边界对齐 41 translater.X = 0; 42 } 43 else if (afterScaleRect.X + afterScaleRect.Width - v.X < Container.RenderSize.Width) { 44 //目标右边界与可视右边界对齐 45 translater.X = Container.RenderSize.Width - afterScaleRect.Size.Width; 46 } 47 else { 48 //减去鼠标点在缩放前后之间的差值,实际上就是以鼠标点为中心进行缩放 49 translater.X -= v.X; 50 } 51 52 53 if (afterScaleRect.Size.Height <= _viewportSize.Height) { 54 double heightHalfDleta = (Container.RenderSize.Height - afterScaleRect.Height) / 2; 55 translater.Y = heightHalfDleta; 56 } 57 else if (afterScaleRect.Y - v.Y > 0) { 58 translater.Y = 0; 59 } 60 else if (afterScaleRect.Y + afterScaleRect.Height - v.Y < Container.RenderSize.Height) { 61 translater.Y = Container.RenderSize.Height - afterScaleRect.Size.Height; 62 } 63 else { 64 translater.Y -= v.Y; 65 } 66 }
代码:
http://download.csdn.net/download/kongxh_1981/9161521