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

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

  1. 基类定义Mouse的状态方法,以及把界面控件传入
   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. 框选交互的基类
  /// <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状态加上自己的功能即可,绘制功能在基类中完成了

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-07 10:48:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-07 10:48:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-07 10:48:03       82 阅读
  4. Python语言-面向对象

    2024-04-07 10:48:03       91 阅读

热门阅读

  1. 算法| ss 二叉树

    2024-04-07 10:48:03       36 阅读
  2. 软件工程,系统设计

    2024-04-07 10:48:03       36 阅读
  3. 软件工程

    2024-04-07 10:48:03       31 阅读
  4. 【C语言】生命周期&作用域选择题

    2024-04-07 10:48:03       39 阅读
  5. 深入解析Python的lxml库:高效处理XML和HTML的利器

    2024-04-07 10:48:03       39 阅读
  6. 创建线程的几种方式,及线程的生命周期?

    2024-04-07 10:48:03       34 阅读
  7. 数码视讯Q7盒子刷armbian遇到的坑之二

    2024-04-07 10:48:03       38 阅读
  8. 实现精简的通用环形缓冲器或环形队列

    2024-04-07 10:48:03       39 阅读
  9. 碧桂园服务:以长期主义走出稳健增长曲线

    2024-04-07 10:48:03       41 阅读