国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

如何用C# 實現鼠標框選效果

2020-02-24 14:30:17
字體:
來源:轉載
供稿:網友

本文是武林小編將為大家介紹關于如何用C# 實現鼠標框選效果多內容,希望本篇文章能幫助到大家,看完后希望大家點個贊哦。

實現步驟:

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# 實現鼠標框選效果。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 泰来县| 孟州市| 崇明县| 东兰县| 普宁市| 江陵县| 新郑市| 峨山| 德格县| 禹州市| 西峡县| 漳浦县| 湛江市| 开江县| 文山县| 安顺市| 图们市| 乌拉特中旗| 塔河县| 衡水市| 通化县| 洪湖市| 南开区| 垦利县| 甘肃省| 讷河市| 景洪市| 平武县| 乌拉特后旗| 蒙阴县| 剑阁县| 柳江县| 咸丰县| 泸溪县| 雅安市| 连山| 交城县| 郎溪县| 大方县| 江达县| 镇坪县|