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状态加上自己的功能即可,绘制功能在基类中完成了
相关推荐
绿龙术士1 天前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
wangnaisheng3 天前
【WPF】Opacity 属性的使用
wpf
姬激薄3 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)3 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心4 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo4 天前
9.1.领域驱动设计
wpf
大道随心4 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠4 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go5 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet5 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf