PolygonRoiCanvas: ROI右键删除顶点阻止菜单弹出、IsEditable属性控制编辑状态
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>设置为 true 可抑制下一次右键菜单弹出,由外部(如 ViewportPanelView)在 ContextMenuOpening 中检查</summary>
|
||||
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;
|
||||
|
||||
@@ -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(); }
|
||||
}
|
||||
|
||||
/// <summary>是否可编辑(拖拽顶点、删除顶点)</summary>
|
||||
public bool IsEditable
|
||||
{
|
||||
get => _isEditable;
|
||||
set { _isEditable = value; OnPropertyChanged(); }
|
||||
}
|
||||
|
||||
public string Color
|
||||
{
|
||||
get => _color;
|
||||
|
||||
Reference in New Issue
Block a user