fasdfsa commited on
Commit
ba480e2
·
1 Parent(s): 7a3311b

可以多次框选,右键边线弹菜单删除

Browse files
Files changed (1) hide show
  1. src/WpfEditor/MainWindow.xaml.cs +88 -15
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -12,6 +12,8 @@ using System.Windows.Media;
12
  using System.Windows.Media.Imaging;
13
  using static System.Net.Mime.MediaTypeNames;
14
  using System.Windows.Shapes;
 
 
15
 
16
  using Window = System.Windows.Window;
17
  using Point = System.Windows.Point;
@@ -25,6 +27,18 @@ namespace WpfEditor
25
  ReviseEdit
26
  }
27
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  public partial class MainWindow : Window
29
  {
30
  private string _currentFilePath = null;
@@ -37,6 +51,8 @@ namespace WpfEditor
37
  private Point _selectionStartPoint;
38
  private Rectangle _selectionRectangle;
39
  private Canvas _selectionCanvas;
 
 
40
 
41
  public MainWindow(EditorMode mode = EditorMode.ImageOCR)
42
  {
@@ -49,6 +65,8 @@ namespace WpfEditor
49
  ImageViewer.MouseLeftButtonDown += ImageViewer_MouseLeftButtonDown;
50
  ImageViewer.MouseMove += ImageViewer_MouseMove;
51
  ImageViewer.MouseLeftButtonUp += ImageViewer_MouseLeftButtonUp;
 
 
52
 
53
  // 初始化选择画布
54
  InitializeSelectionCanvas();
@@ -603,15 +621,19 @@ namespace WpfEditor
603
  double actualHeight = _selectionRectangle.Height / _imageZoomFactor;
604
 
605
  // 显示坐标弹窗
606
- ShowCoordinateDialog(actualLeft, actualTop, actualWidth, actualHeight);
 
 
 
607
  }
608
-
609
- // 清除选择矩形
610
- if (_selectionRectangle != null)
611
  {
 
612
  _selectionCanvas.Children.Remove(_selectionRectangle);
613
- _selectionRectangle = null;
614
  }
 
 
 
615
  }
616
  }
617
 
@@ -626,17 +648,68 @@ namespace WpfEditor
626
 
627
  MessageBox.Show(message, "矩形坐标信息", MessageBoxButton.OK, MessageBoxImage.Information);
628
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
629
  }
630
  }
631
 
632
- public class CursorPositionChangedEventArgs : EventArgs
633
- {
634
- public int Line { get; set; }
635
- public int Column { get; set; }
636
 
637
- public CursorPositionChangedEventArgs(int line, int column)
638
- {
639
- Line = line;
640
- Column = column;
641
- }
642
- }
 
12
  using System.Windows.Media.Imaging;
13
  using static System.Net.Mime.MediaTypeNames;
14
  using System.Windows.Shapes;
15
+ using System.Collections.Generic;
16
+ using System.Windows.Controls.Primitives;
17
 
18
  using Window = System.Windows.Window;
19
  using Point = System.Windows.Point;
 
27
  ReviseEdit
28
  }
29
 
30
+ public class CursorPositionChangedEventArgs : EventArgs
31
+ {
32
+ public int Line { get; set; }
33
+ public int Column { get; set; }
34
+
35
+ public CursorPositionChangedEventArgs(int line, int column)
36
+ {
37
+ Line = line;
38
+ Column = column;
39
+ }
40
+ }
41
+
42
  public partial class MainWindow : Window
43
  {
44
  private string _currentFilePath = null;
 
51
  private Point _selectionStartPoint;
52
  private Rectangle _selectionRectangle;
53
  private Canvas _selectionCanvas;
54
+ // 添加存储多个矩形选框的集合
55
+ private List<Rectangle> _selectionRectangles = new List<Rectangle>();
56
 
57
  public MainWindow(EditorMode mode = EditorMode.ImageOCR)
58
  {
 
65
  ImageViewer.MouseLeftButtonDown += ImageViewer_MouseLeftButtonDown;
66
  ImageViewer.MouseMove += ImageViewer_MouseMove;
67
  ImageViewer.MouseLeftButtonUp += ImageViewer_MouseLeftButtonUp;
68
+ // 添加右键事件
69
+ ImageViewer.MouseRightButtonDown += ImageViewer_MouseRightButtonDown;
70
 
71
  // 初始化选择画布
72
  InitializeSelectionCanvas();
 
621
  double actualHeight = _selectionRectangle.Height / _imageZoomFactor;
622
 
623
  // 显示坐标弹窗
624
+ //ShowCoordinateDialog(actualLeft, actualTop, actualWidth, actualHeight);
625
+
626
+ // 将矩形添加到集合中,而不是清除
627
+ _selectionRectangles.Add(_selectionRectangle);
628
  }
629
+ else if (_selectionRectangle != null)
 
 
630
  {
631
+ // 如果矩形太小,则移除它
632
  _selectionCanvas.Children.Remove(_selectionRectangle);
 
633
  }
634
+
635
+ // 重置当前选择矩形引用,准备下一次选择
636
+ _selectionRectangle = null;
637
  }
638
  }
639
 
 
648
 
649
  MessageBox.Show(message, "矩形坐标信息", MessageBoxButton.OK, MessageBoxImage.Information);
650
  }
651
+
652
+
653
+ // 鼠标右键按下事件
654
+ private void ImageViewer_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
655
+ {
656
+ if (_mode == EditorMode.ReviseEdit && _selectionRectangles.Count > 0)
657
+ {
658
+ Point clickPoint = e.GetPosition(ImageViewer);
659
+ Rectangle clickedRectangle = null;
660
+
661
+ // 查找被点击的矩形选框
662
+ foreach (Rectangle rect in _selectionRectangles)
663
+ {
664
+ if (IsPointOnRectangleBorder(clickPoint, rect))
665
+ {
666
+ clickedRectangle = rect;
667
+ break;
668
+ }
669
+ }
670
+
671
+ if (clickedRectangle != null)
672
+ {
673
+ // 创建右键菜单
674
+ ContextMenu contextMenu = new ContextMenu();
675
+ MenuItem deleteMenuItem = new MenuItem { Header = "删除" };
676
+ deleteMenuItem.Click += (s, args) => DeleteRectangle(clickedRectangle);
677
+ contextMenu.Items.Add(deleteMenuItem);
678
+
679
+ // 显示菜单
680
+ contextMenu.IsOpen = true;
681
+ contextMenu.PlacementTarget = ImageViewer;
682
+ contextMenu.Placement = PlacementMode.MousePoint;
683
+ }
684
+ }
685
+ }
686
+
687
+ // 检查点击位置是否在矩形边线上
688
+ private bool IsPointOnRectangleBorder(Point point, Rectangle rectangle)
689
+ {
690
+ double left = Canvas.GetLeft(rectangle);
691
+ double top = Canvas.GetTop(rectangle);
692
+ double right = left + rectangle.Width;
693
+ double bottom = top + rectangle.Height;
694
+ double tolerance = 5; // 边线容差
695
+
696
+ // 检查是否在矩形边线附近
697
+ bool onLeftBorder = Math.Abs(point.X - left) <= tolerance && point.Y >= top - tolerance && point.Y <= bottom + tolerance;
698
+ bool onRightBorder = Math.Abs(point.X - right) <= tolerance && point.Y >= top - tolerance && point.Y <= bottom + tolerance;
699
+ bool onTopBorder = Math.Abs(point.Y - top) <= tolerance && point.X >= left - tolerance && point.X <= right + tolerance;
700
+ bool onBottomBorder = Math.Abs(point.Y - bottom) <= tolerance && point.X >= left - tolerance && point.X <= right + tolerance;
701
+
702
+ return onLeftBorder || onRightBorder || onTopBorder || onBottomBorder;
703
+ }
704
+
705
+ // 删除指定的矩形选框
706
+ private void DeleteRectangle(Rectangle rectangle)
707
+ {
708
+ _selectionCanvas.Children.Remove(rectangle);
709
+ _selectionRectangles.Remove(rectangle);
710
+ }
711
  }
712
  }
713
 
 
 
 
 
714
 
715
+