From 0ecbd4fd8797d676c1b9b00ff12d02b4f043f3c5 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Fri, 6 Jan 2023 00:09:00 +0800 Subject: [PATCH] enhance: add move gesture for ImageViewer. --- .../Controls/Image/ImageViewer.cs | 76 +++++++++++++------ 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Image/ImageViewer.cs b/src/Shared/HandyControl_Shared/Controls/Image/ImageViewer.cs index 8632a3a7..abf9598d 100644 --- a/src/Shared/HandyControl_Shared/Controls/Image/ImageViewer.cs +++ b/src/Shared/HandyControl_Shared/Controls/Image/ImageViewer.cs @@ -1,10 +1,12 @@ using System; +using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; +using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; @@ -99,9 +101,9 @@ public class ImageViewer : Control private Point _imgCurrentPoint; /// - /// 鼠标是否在图片上按下左键 + /// 鼠标是否在图片上按下 /// - private bool _imgIsMouseLeftButtonDown; + private bool _imgIsMouseDown; /// /// 在图片上按下时图片的位置 @@ -119,9 +121,9 @@ public class ImageViewer : Control private Point _imgSmallCurrentPoint; /// - /// 鼠标是否在小图片上按下左键 + /// 鼠标是否在小图片上按下 /// - private bool _imgSmallIsMouseLeftButtonDown; + private bool _imgSmallIsMouseDown; /// /// 在小图片上按下时图片的位置 @@ -162,6 +164,8 @@ public class ImageViewer : Control private bool _isLoaded; + private MouseBinding _mouseMoveBinding; + #endregion Data #region ctor @@ -175,6 +179,8 @@ public class ImageViewer : Control CommandBindings.Add(new CommandBinding(ControlCommands.Enlarge, ButtonEnlarge_OnClick)); CommandBindings.Add(new CommandBinding(ControlCommands.RotateLeft, ButtonRotateLeft_OnClick)); CommandBindings.Add(new CommandBinding(ControlCommands.RotateRight, ButtonRotateRight_OnClick)); + CommandBindings.Add(new CommandBinding(ControlCommands.MouseMove, ImageMain_OnMouseDown)); + OnMoveGestureChanged(MoveGesture); Loaded += (s, e) => { @@ -235,6 +241,9 @@ public class ImageViewer : Control public static readonly DependencyProperty IsFullScreenProperty = DependencyProperty.Register( nameof(IsFullScreen), typeof(bool), typeof(ImageViewer), new PropertyMetadata(ValueBoxes.FalseBox)); + public static readonly DependencyProperty MoveGestureProperty = DependencyProperty.Register( + nameof(MoveGesture), typeof(MouseGesture), typeof(ImageViewer), new UIPropertyMetadata(new MouseGesture(MouseAction.LeftClick), OnMoveGestureChanged)); + internal static readonly DependencyProperty ImgPathProperty = DependencyProperty.Register( nameof(ImgPath), typeof(string), typeof(ImageViewer), new PropertyMetadata(default(string))); @@ -283,6 +292,14 @@ public class ImageViewer : Control set => SetValue(IsFullScreenProperty, ValueBoxes.BooleanBox(value)); } + [ValueSerializer(typeof(MouseGestureValueSerializer))] + [TypeConverter(typeof(MouseGestureConverter))] + public MouseGesture MoveGesture + { + get => (MouseGesture) GetValue(MoveGestureProperty); + set => SetValue(MoveGestureProperty, value); + } + public bool ShowImgMap { get => (bool) GetValue(ShowImgMapProperty); @@ -408,8 +425,6 @@ public class ImageViewer : Control public override void OnApplyTemplate() { - if (_imageMain != null) _imageMain.MouseLeftButtonDown -= ImageMain_OnMouseLeftButtonDown; - if (_canvasSmallImg != null) { _canvasSmallImg.MouseLeftButtonDown -= CanvasSmallImg_OnMouseLeftButtonDown; @@ -430,7 +445,6 @@ public class ImageViewer : Control var t = new RotateTransform(); BindingOperations.SetBinding(t, RotateTransform.AngleProperty, new Binding(ImageRotateProperty.Name) { Source = this }); _imageMain.LayoutTransform = t; - _imageMain.MouseLeftButtonDown += ImageMain_OnMouseLeftButtonDown; } if (_canvasSmallImg != null) @@ -581,12 +595,10 @@ public class ImageViewer : Control if (SaveFileDialog.ShowDialog() == true) { var path = SaveFileDialog.FileName; - using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write)) - { - var encoder = new PngBitmapEncoder(); - encoder.Frames.Add(BitmapFrame.Create(ImageSource)); - encoder.Save(fileStream); - } + using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write); + var encoder = new PngBitmapEncoder(); + encoder.Frames.Add(BitmapFrame.Create(ImageSource)); + encoder.Save(fileStream); } } @@ -645,14 +657,14 @@ public class ImageViewer : Control MoveSmallImg(_imgSmallMouseDownMargin.Left, _imgSmallMouseDownMargin.Top); } - private void ImageMain_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) + private void ImageMain_OnMouseDown(object sender, ExecutedRoutedEventArgs e) { _imgMouseDownPoint = Mouse.GetPosition(_panelMain); _imgMouseDownMargin = ImageMargin; - _imgIsMouseLeftButtonDown = true; + _imgIsMouseDown = true; } - protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e) => _imgIsMouseLeftButtonDown = false; + protected override void OnPreviewMouseUp(MouseButtonEventArgs e) => _imgIsMouseDown = false; /// /// 右下角小图片显示切换 @@ -818,6 +830,14 @@ public class ImageViewer : Control BeginAnimation(ImageRotateProperty, animation); } + private MouseButtonState GetMouseButtonState() => MoveGesture.MouseAction switch + { + MouseAction.LeftClick => Mouse.LeftButton, + MouseAction.RightClick => Mouse.RightButton, + MouseAction.MiddleClick => Mouse.MiddleButton, + _ => Mouse.LeftButton + }; + /// /// 移动图片 /// @@ -827,12 +847,12 @@ public class ImageViewer : Control ShowCloseButton = _imgCurrentPoint.Y < 200; ShowBorderBottom = _imgCurrentPoint.Y > ActualHeight - 200; - if (Mouse.LeftButton == MouseButtonState.Released) + if (GetMouseButtonState() == MouseButtonState.Released) { return; } - if (_imgIsMouseLeftButtonDown) + if (_imgIsMouseDown) { var subX = _imgCurrentPoint.X - _imgMouseDownPoint.X; var subY = _imgCurrentPoint.Y - _imgMouseDownPoint.Y; @@ -869,12 +889,12 @@ public class ImageViewer : Control /// private void MoveSmallImg() { - if (!_imgSmallIsMouseLeftButtonDown) + if (!_imgSmallIsMouseDown) { return; } - if (Mouse.LeftButton == MouseButtonState.Released) + if (GetMouseButtonState() == MouseButtonState.Released) { return; } @@ -916,11 +936,23 @@ public class ImageViewer : Control { _imgSmallMouseDownPoint = Mouse.GetPosition(_canvasSmallImg); _imgSmallMouseDownMargin = _borderMove.Margin; - _imgSmallIsMouseLeftButtonDown = true; + _imgSmallIsMouseDown = true; e.Handled = true; } - private void CanvasSmallImg_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) => _imgSmallIsMouseLeftButtonDown = false; + private void CanvasSmallImg_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) => _imgSmallIsMouseDown = false; private void CanvasSmallImg_OnMouseMove(object sender, MouseEventArgs e) => MoveSmallImg(); + + private static void OnMoveGestureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((ImageViewer) d).OnMoveGestureChanged((MouseGesture) e.NewValue); + } + + private void OnMoveGestureChanged(MouseGesture newValue) + { + InputBindings.Remove(_mouseMoveBinding); + _mouseMoveBinding = new MouseBinding(ControlCommands.MouseMove, newValue); + InputBindings.Add(_mouseMoveBinding); + } }