From 8b2cf01fe240ce41b5b26f2b1c7daac735b1c737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F?= Date: Tue, 28 Apr 2026 17:38:21 +0800 Subject: [PATCH] =?UTF-8?q?PolygonRoiCanvas:=20ROI=E5=8F=B3=E9=94=AE?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=A1=B6=E7=82=B9=E9=98=BB=E6=AD=A2=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E5=BC=B9=E5=87=BA=E3=80=81IsEditable=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=8E=A7=E5=88=B6=E7=BC=96=E8=BE=91=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/PolygonRoiCanvas.xaml.cs | 34 +++++++++++++++++-- .../Models/ROIShape.cs | 8 +++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml.cs b/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml.cs index 917ae39..2473333 100644 --- a/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml.cs +++ b/XP.ImageProcessing.RoiControl/Controls/PolygonRoiCanvas.xaml.cs @@ -1947,14 +1947,44 @@ namespace XP.ImageProcessing.RoiControl.Controls // BGA 模式下阻止 ContextMenu 弹出 if (CurrentMeasureMode == Models.MeasureMode.BgaVoid && _bgaCurrent != null) { + SuppressContextMenu = true; e.Handled = true; } + // 外部请求抑制右键菜单 + if (SuppressContextMenu) + { + e.Handled = true; + } + // ROI 选中状态下,右键点击顶点附近时删除顶点并阻止菜单 + if (SelectedROI is PolygonROI poly && poly.IsSelected && poly.IsEditable && poly.Points.Count > 3) + { + var pos = e.GetPosition(mainCanvas); + double threshold = 15; + int nearestIndex = -1; + double nearestDist = double.MaxValue; + for (int i = 0; i < poly.Points.Count; i++) + { + var pt = poly.Points[i]; + double dx = pt.X - pos.X, dy = pt.Y - pos.Y; + double dist = Math.Sqrt(dx * dx + dy * dy); + if (dist < nearestDist) { nearestDist = dist; nearestIndex = i; } + } + if (nearestIndex >= 0 && nearestDist < threshold) + { + poly.Points.RemoveAt(nearestIndex); + SuppressContextMenu = true; + e.Handled = true; + } + } } + /// 设置为 true 可抑制下一次右键菜单弹出,由外部(如 ViewportPanelView)在 ContextMenuOpening 中检查 + public bool SuppressContextMenu { get; set; } + private void ROI_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { - // 选择ROI - if (sender is FrameworkElement element && element.DataContext is ROIShape roi) + // 选择ROI(仅可编辑时) + if (sender is FrameworkElement element && element.DataContext is ROIShape roi && roi.IsEditable) { SelectedROI = roi; e.Handled = true; diff --git a/XP.ImageProcessing.RoiControl/Models/ROIShape.cs b/XP.ImageProcessing.RoiControl/Models/ROIShape.cs index 0e5c7a8..ca87788 100644 --- a/XP.ImageProcessing.RoiControl/Models/ROIShape.cs +++ b/XP.ImageProcessing.RoiControl/Models/ROIShape.cs @@ -21,6 +21,7 @@ namespace XP.ImageProcessing.RoiControl.Models public abstract class ROIShape : INotifyPropertyChanged { private bool _isSelected; + private bool _isEditable = true; private string _id = Guid.NewGuid().ToString(); private string _color = "Red"; @@ -36,6 +37,13 @@ namespace XP.ImageProcessing.RoiControl.Models set { _isSelected = value; OnPropertyChanged(); } } + /// 是否可编辑(拖拽顶点、删除顶点) + public bool IsEditable + { + get => _isEditable; + set { _isEditable = value; OnPropertyChanged(); } + } + public string Color { get => _color;