WPF封装一个鼠标事件控件界面框选的功能

WPF封装一个鼠标事件控件界面框选的功能

  1. 基类定义Mouse的状态方法,以及把界面控件传入
csharp 复制代码
   public abstract class MouseEventBase<T> where T : UserControl
   {
       public MouseEventBase(T associatedControl, Rectangle rect)
       {
           map = associatedControl;
           SelectionRect = rect;
       }

       public MouseEventBase(T associatedControl, Canvas canvas)
       {
           map = associatedControl;
           SelectionPolygon = canvas;
       }

       protected T map { get; private set; }

       protected Rectangle? SelectionRect;
       protected Canvas? SelectionPolygon;
       protected double Width => map.ActualWidth;
       protected double Height => map.ActualHeight;
       public ToolMode PageMode { get; set; }

       public virtual void MouseDown(Point imageLocation, Point mapLocation) { }
       public virtual void MouseMove(Point imageLocation, Point mapLocation) { }
       public virtual void MouseUp(Point imageLocation, Point mapLocation) { }
   }
  1. 框选交互的基类
csharp 复制代码
  /// <summary>
  /// 框选基类
  /// </summary>
  public abstract class BoxSelectionBase : MouseEventBase<UserControl>
  {
      /// <summary>
      /// 是否是正常框选,或者反向框选
      /// </summary>
      protected bool BoxDirection = true;

      private Point startPoint;

      public BoxSelectionBase(UserControl map, Rectangle rect) : base(map, rect)
      {
          PageMode = ToolMode.None;
          SelectionRect = rect;
      }

      public override void MouseDown(Point imageLocation, Point mapLocation)
      {
          startPoint = mapLocation;
          SelectionRect!.Width = 0;
          SelectionRect.Height = 0;
          // 显示选择框
          SelectionRect.Visibility = Visibility.Visible;
          SelectionRect.Margin = new Thickness(startPoint.X, startPoint.Y, 0, 0);
          SelectionRect.Width = 0;
          SelectionRect.Height = 0;
      }
      public override void MouseMove(Point imageLocation, Point mapLocation)
      {
          var minX = startPoint.X < mapLocation.X ? startPoint.X : mapLocation.X;
          var minY = startPoint.Y < mapLocation.Y ? startPoint.Y : mapLocation.Y;
          var maxX = startPoint.X > mapLocation.X ? startPoint.X : mapLocation.X;
          var maxY = startPoint.Y > mapLocation.Y ? startPoint.Y : mapLocation.Y;

          var left = Math.Max(0, Math.Min(minX, Width));
          var top = Math.Max(0, Math.Min(minY, Height));
          maxX = Math.Max(0, Math.Min(maxX, Width));
          maxY = Math.Max(0, Math.Min(maxY, Height));
          var width = maxX - left;
          var height = maxY - top;

          SelectionRect!.Margin = new Thickness(left, top, 0, 0);
          SelectionRect.Width = width;
          SelectionRect.Height = height;

          BoxDirection = mapLocation.X >= startPoint.X;
      }

      public override void MouseUp(Point imageLocation, Point mapLocation)
      {
          SelectionRect!.Visibility = Visibility.Collapsed;
      }
  1. 实际使用只需要继承Mouse状态加上自己的功能即可,绘制功能在基类中完成了
相关推荐
狮恒8 小时前
OpenHarmony Flutter 分布式数据管理:跨设备数据同步与一致性保障方案
分布式·flutter·wpf·openharmony
Macbethad11 小时前
工业设备IO模拟程序
wpf
狮恒16 小时前
OpenHarmony Flutter 分布式设备发现与连接:无感组网与设备协同管理方案
分布式·flutter·wpf·openharmony
云和数据.ChenGuang16 小时前
鸿蒙负一屏的技术定位与核心价值
华为·wpf·harmonyos
狮恒16 小时前
OpenHarmony Flutter 分布式数据管理实战:全场景数据一致性与高效流转方案
wpf
狮恒17 小时前
OpenHarmony Flutter 分布式音视频:跨设备流传输与实时协同交互方案
分布式·flutter·wpf·openharmony
狮恒18 小时前
OpenHarmony Flutter 分布式安全与隐私保护:跨设备可信交互与数据防泄漏方案
分布式·flutter·wpf·openharmony
狮恒18 小时前
OpenHarmony Flutter 分布式智能协同:基于 AI 的跨端场景感知与自适应交互方案
wpf
狮恒20 小时前
OpenHarmony Flutter 分布式任务调度:跨设备资源协同与负载均衡方案
分布式·flutter·wpf·openharmony
嗝o゚20 小时前
Flutter适配鸿蒙多屏异构UI开发实战
flutter·开源·wpf·harmonyos