enhance: add move gesture for ImageViewer.
This commit is contained in:
1 parent
dfa8e70c42
commit
0ecbd4fd87
1 file changed
+54
-22
@@ -1,10 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Data;
|
using System.Windows.Data;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Markup;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Animation;
|
using System.Windows.Media.Animation;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
@@ -99,9 +101,9 @@ public class ImageViewer : Control
|
|||||||
private Point _imgCurrentPoint;
|
private Point _imgCurrentPoint;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 鼠标是否在图片上按下左键
|
/// 鼠标是否在图片上按下
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private bool _imgIsMouseLeftButtonDown;
|
private bool _imgIsMouseDown;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 在图片上按下时图片的位置
|
/// 在图片上按下时图片的位置
|
||||||
@@ -119,9 +121,9 @@ public class ImageViewer : Control
|
|||||||
private Point _imgSmallCurrentPoint;
|
private Point _imgSmallCurrentPoint;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 鼠标是否在小图片上按下左键
|
/// 鼠标是否在小图片上按下
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private bool _imgSmallIsMouseLeftButtonDown;
|
private bool _imgSmallIsMouseDown;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 在小图片上按下时图片的位置
|
/// 在小图片上按下时图片的位置
|
||||||
@@ -162,6 +164,8 @@ public class ImageViewer : Control
|
|||||||
|
|
||||||
private bool _isLoaded;
|
private bool _isLoaded;
|
||||||
|
|
||||||
|
private MouseBinding _mouseMoveBinding;
|
||||||
|
|
||||||
#endregion Data
|
#endregion Data
|
||||||
|
|
||||||
#region ctor
|
#region ctor
|
||||||
@@ -175,6 +179,8 @@ public class ImageViewer : Control
|
|||||||
CommandBindings.Add(new CommandBinding(ControlCommands.Enlarge, ButtonEnlarge_OnClick));
|
CommandBindings.Add(new CommandBinding(ControlCommands.Enlarge, ButtonEnlarge_OnClick));
|
||||||
CommandBindings.Add(new CommandBinding(ControlCommands.RotateLeft, ButtonRotateLeft_OnClick));
|
CommandBindings.Add(new CommandBinding(ControlCommands.RotateLeft, ButtonRotateLeft_OnClick));
|
||||||
CommandBindings.Add(new CommandBinding(ControlCommands.RotateRight, ButtonRotateRight_OnClick));
|
CommandBindings.Add(new CommandBinding(ControlCommands.RotateRight, ButtonRotateRight_OnClick));
|
||||||
|
CommandBindings.Add(new CommandBinding(ControlCommands.MouseMove, ImageMain_OnMouseDown));
|
||||||
|
OnMoveGestureChanged(MoveGesture);
|
||||||
|
|
||||||
Loaded += (s, e) =>
|
Loaded += (s, e) =>
|
||||||
{
|
{
|
||||||
@@ -235,6 +241,9 @@ public class ImageViewer : Control
|
|||||||
public static readonly DependencyProperty IsFullScreenProperty = DependencyProperty.Register(
|
public static readonly DependencyProperty IsFullScreenProperty = DependencyProperty.Register(
|
||||||
nameof(IsFullScreen), typeof(bool), typeof(ImageViewer), new PropertyMetadata(ValueBoxes.FalseBox));
|
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(
|
internal static readonly DependencyProperty ImgPathProperty = DependencyProperty.Register(
|
||||||
nameof(ImgPath), typeof(string), typeof(ImageViewer), new PropertyMetadata(default(string)));
|
nameof(ImgPath), typeof(string), typeof(ImageViewer), new PropertyMetadata(default(string)));
|
||||||
|
|
||||||
@@ -283,6 +292,14 @@ public class ImageViewer : Control
|
|||||||
set => SetValue(IsFullScreenProperty, ValueBoxes.BooleanBox(value));
|
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
|
public bool ShowImgMap
|
||||||
{
|
{
|
||||||
get => (bool) GetValue(ShowImgMapProperty);
|
get => (bool) GetValue(ShowImgMapProperty);
|
||||||
@@ -408,8 +425,6 @@ public class ImageViewer : Control
|
|||||||
|
|
||||||
public override void OnApplyTemplate()
|
public override void OnApplyTemplate()
|
||||||
{
|
{
|
||||||
if (_imageMain != null) _imageMain.MouseLeftButtonDown -= ImageMain_OnMouseLeftButtonDown;
|
|
||||||
|
|
||||||
if (_canvasSmallImg != null)
|
if (_canvasSmallImg != null)
|
||||||
{
|
{
|
||||||
_canvasSmallImg.MouseLeftButtonDown -= CanvasSmallImg_OnMouseLeftButtonDown;
|
_canvasSmallImg.MouseLeftButtonDown -= CanvasSmallImg_OnMouseLeftButtonDown;
|
||||||
@@ -430,7 +445,6 @@ public class ImageViewer : Control
|
|||||||
var t = new RotateTransform();
|
var t = new RotateTransform();
|
||||||
BindingOperations.SetBinding(t, RotateTransform.AngleProperty, new Binding(ImageRotateProperty.Name) { Source = this });
|
BindingOperations.SetBinding(t, RotateTransform.AngleProperty, new Binding(ImageRotateProperty.Name) { Source = this });
|
||||||
_imageMain.LayoutTransform = t;
|
_imageMain.LayoutTransform = t;
|
||||||
_imageMain.MouseLeftButtonDown += ImageMain_OnMouseLeftButtonDown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_canvasSmallImg != null)
|
if (_canvasSmallImg != null)
|
||||||
@@ -581,12 +595,10 @@ public class ImageViewer : Control
|
|||||||
if (SaveFileDialog.ShowDialog() == true)
|
if (SaveFileDialog.ShowDialog() == true)
|
||||||
{
|
{
|
||||||
var path = SaveFileDialog.FileName;
|
var path = SaveFileDialog.FileName;
|
||||||
using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
|
using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
|
||||||
{
|
var encoder = new PngBitmapEncoder();
|
||||||
var encoder = new PngBitmapEncoder();
|
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
|
||||||
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
|
encoder.Save(fileStream);
|
||||||
encoder.Save(fileStream);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -645,14 +657,14 @@ public class ImageViewer : Control
|
|||||||
MoveSmallImg(_imgSmallMouseDownMargin.Left, _imgSmallMouseDownMargin.Top);
|
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);
|
_imgMouseDownPoint = Mouse.GetPosition(_panelMain);
|
||||||
_imgMouseDownMargin = ImageMargin;
|
_imgMouseDownMargin = ImageMargin;
|
||||||
_imgIsMouseLeftButtonDown = true;
|
_imgIsMouseDown = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e) => _imgIsMouseLeftButtonDown = false;
|
protected override void OnPreviewMouseUp(MouseButtonEventArgs e) => _imgIsMouseDown = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 右下角小图片显示切换
|
/// 右下角小图片显示切换
|
||||||
@@ -818,6 +830,14 @@ public class ImageViewer : Control
|
|||||||
BeginAnimation(ImageRotateProperty, animation);
|
BeginAnimation(ImageRotateProperty, animation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MouseButtonState GetMouseButtonState() => MoveGesture.MouseAction switch
|
||||||
|
{
|
||||||
|
MouseAction.LeftClick => Mouse.LeftButton,
|
||||||
|
MouseAction.RightClick => Mouse.RightButton,
|
||||||
|
MouseAction.MiddleClick => Mouse.MiddleButton,
|
||||||
|
_ => Mouse.LeftButton
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 移动图片
|
/// 移动图片
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -827,12 +847,12 @@ public class ImageViewer : Control
|
|||||||
ShowCloseButton = _imgCurrentPoint.Y < 200;
|
ShowCloseButton = _imgCurrentPoint.Y < 200;
|
||||||
ShowBorderBottom = _imgCurrentPoint.Y > ActualHeight - 200;
|
ShowBorderBottom = _imgCurrentPoint.Y > ActualHeight - 200;
|
||||||
|
|
||||||
if (Mouse.LeftButton == MouseButtonState.Released)
|
if (GetMouseButtonState() == MouseButtonState.Released)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_imgIsMouseLeftButtonDown)
|
if (_imgIsMouseDown)
|
||||||
{
|
{
|
||||||
var subX = _imgCurrentPoint.X - _imgMouseDownPoint.X;
|
var subX = _imgCurrentPoint.X - _imgMouseDownPoint.X;
|
||||||
var subY = _imgCurrentPoint.Y - _imgMouseDownPoint.Y;
|
var subY = _imgCurrentPoint.Y - _imgMouseDownPoint.Y;
|
||||||
@@ -869,12 +889,12 @@ public class ImageViewer : Control
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private void MoveSmallImg()
|
private void MoveSmallImg()
|
||||||
{
|
{
|
||||||
if (!_imgSmallIsMouseLeftButtonDown)
|
if (!_imgSmallIsMouseDown)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Mouse.LeftButton == MouseButtonState.Released)
|
if (GetMouseButtonState() == MouseButtonState.Released)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -916,11 +936,23 @@ public class ImageViewer : Control
|
|||||||
{
|
{
|
||||||
_imgSmallMouseDownPoint = Mouse.GetPosition(_canvasSmallImg);
|
_imgSmallMouseDownPoint = Mouse.GetPosition(_canvasSmallImg);
|
||||||
_imgSmallMouseDownMargin = _borderMove.Margin;
|
_imgSmallMouseDownMargin = _borderMove.Margin;
|
||||||
_imgSmallIsMouseLeftButtonDown = true;
|
_imgSmallIsMouseDown = true;
|
||||||
e.Handled = 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 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user