用C# 实现鼠标框选效果的实现代码

时间:2023-04-18 14:08:26 

实现步骤:

1.实现整个鼠标框选的几个事件(down、move、up),当鼠标点下记录鼠标框选的起点,鼠标抬起结束操作。

2.以鼠标框选过程中获取的鼠标坐标为基点计算框选的矩形的4点坐标,4点坐标以顺时针方向布点。

3.通过Shape.Path类实现在类上画出此矩形。

代码如下:


namespace HostDemo {
 public class HostCanvas : Canvas {
  public HostCanvas() {
   InitializeComponent();
  }

  private void InitializeComponent() {
   this.Loaded += OnLoad;
   this.MouseDown += OnMouseDown;
   this.MouseMove += OnMouseMove;
   this.MouseUp += OnMouseUp;
   locus = new Path();
   locus.Fill = new SolidColorBrush(Color.FromArgb(1, 255, 255, 255));
   locus.Stroke = Brushes.Red;
   locus.StrokeThickness = 1;
   locus.IsManipulationEnabled = true;
  }

  void OnMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
   ispath = false;
  }

  void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
   if(ispath){
    endpoint = e.GetPosition(this);
    locus.Data = DrawingRect(startpoint,endpoint);
   }
  }

  void OnMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
   if(!this.Children.Contains(locus)) this.Children.Add(locus);
   if (locus.Data != null) locus.Data = null;
   startpoint = e.GetPosition(this);
   ispath = true;
  }

  void OnLoad(object sender, System.Windows.RoutedEventArgs e) {
   this.Background = new SolidColorBrush(Color.FromArgb(35, 255, 255, 255));
  }

  private PathGeometry DrawingRect(Point beginpoint, Point closepoint) {
   PathGeometry result = new PathGeometry(); 
   PathFigure figure = new PathFigure();
   figure.IsClosed = true;
   figure.StartPoint = beginpoint;
   PathSegmentCollection pathSegmentCollection = new PathSegmentCollection();
   PathFigureCollection pathFigureCollection = new PathFigureCollection();  
   LineSegment m1 = new LineSegment();
   m1.Point = new Point(closepoint.X, beginpoint.Y);
   LineSegment m2 = new LineSegment();
   m2.Point = closepoint;
   LineSegment m3 = new LineSegment();
   m3.Point = new Point(beginpoint.X, closepoint.Y);
   pathSegmentCollection.Add(m1);
   pathSegmentCollection.Add(m2);
   pathSegmentCollection.Add(m3);
   figure.Segments = pathSegmentCollection;
   pathFigureCollection.Add(figure);
   result.Figures = pathFigureCollection;

   return result();
  }

  private Path locus;
  private bool ispath = false;
  private Point startpoint;
  private Point endpoint;
 }
}

标签:C#,鼠标框选
0
投稿

猜你喜欢

  • springmvc请求转发和重定向问题(携带参数和不携带参数)

    2022-09-17 13:53:59
  • Android Webview使用小结

    2022-12-06 07:26:19
  • 详解使用Spring Cloud Consul实现服务的注册和发现

    2023-06-08 03:46:23
  • C语言数据结构之二叉树详解

    2021-08-18 20:56:41
  • Java解码H264格式视频流中的图片

    2023-11-24 23:58:24
  • Android Monkey压力测试详细介绍

    2021-10-24 08:02:37
  • java实现日期拆分的方法

    2023-06-19 00:28:59
  • java中javamail收发邮件实现方法

    2022-12-15 02:03:03
  • C# 使用WPF 用MediaElement控件实现视频循环播放

    2022-04-28 03:34:26
  • android shape实现阴影或模糊边效果

    2022-10-14 02:01:09
  • android球形水波百分比控件代码

    2021-06-20 06:03:34
  • Spring Security保护用户密码常用方法详解

    2023-01-24 17:06:18
  • SpringBoot搭配AOP实现自定义注解

    2022-04-07 01:21:59
  • Java框架MyBatis接口编程过程解析

    2022-09-18 07:47:04
  • Android使用WebView.loadUri()打开网页的方法

    2022-07-18 06:17:18
  • Android画板开发之橡皮擦功能

    2022-10-23 02:48:52
  • Android Studio升级到3.0后遇到的坑

    2022-01-23 00:59:07
  • C#面向对象设计的七大原则

    2021-10-21 04:15:49
  • C#创建临时文件的方法

    2023-06-16 14:32:36
  • Android Studio3.0新特性及安装图文教程

    2021-06-21 02:11:48
  • asp之家 软件编程 m.aspxhome.com