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

所属分类: 软件编程 / C 语言 阅读数: 91
收藏 0 赞 0 分享

实现步骤:

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++中变量类型的深入分析

本篇文章是对c++中的变量类型进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

从汇编看c++的默认析构函数的使用详解

本篇文章是对c++中默认析构函数的使用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

基于c++中的默认拷贝函数的使用详解

本篇文章对c++中默认拷贝函数的使用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

解析c++中的默认operator=操作的详解

本篇文章是对c++中的默认operator=操作的应用进行了详细的分析介绍。需要的朋友参考下
收藏 0 赞 0 分享

解析c++中参数对象与局部对象的析构顺序的详解

本篇文章是对c++中参数对象与局部对象的析构顺序进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入c++中临时对象的析构时机的详解

本篇文章对c++中临时对象的析构时机进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解析内存对齐 Data alignment: Straighten up and fly right的详解

对于所有直接操作内存的程序员来说,数据对齐都是很重要的问题.数据对齐对你的程序的表现甚至能否正常运行都会产生影响
收藏 0 赞 0 分享

深入内存对齐的详解

本篇文章是对内存对齐进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入C语言把文件读入字符串以及将字符串写入文件的解决方法

本篇文章是对C语言把文件读入字符串以及将字符串写入文件的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入Windows下的回车是回车换行(\r\n)还是换行回车(\n\r)的详解

本篇文章对Windows下的回车是回车换行(\r\n)还是换行回车(\n\r)进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多