用C# 實(shí)現(xiàn)鼠標(biāo)框選效果的實(shí)現(xiàn)代碼
實(shí)現(xiàn)步驟:
1.實(shí)現(xiàn)整個(gè)鼠標(biāo)框選的幾個(gè)事件(down、move、up),當(dāng)鼠標(biāo)點(diǎn)下記錄鼠標(biāo)框選的起點(diǎn),鼠標(biāo)抬起結(jié)束操作。
2.以鼠標(biāo)框選過(guò)程中獲取的鼠標(biāo)坐標(biāo)為基點(diǎn)計(jì)算框選的矩形的4點(diǎn)坐標(biāo),4點(diǎn)坐標(biāo)以順時(shí)針?lè)较虿键c(diǎn)。
3.通過(guò)Shape.Path類實(shí)現(xiàn)在類上畫出此矩形。
代碼如下:
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#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù)
- C#簡(jiǎn)單獲取全屏中鼠標(biāo)焦點(diǎn)位置坐標(biāo)的方法示例
- C#實(shí)現(xiàn)的鼠標(biāo)鉤子
- C#鍵盤鼠標(biāo)鉤子實(shí)例
- C#實(shí)現(xiàn)鼠標(biāo)移動(dòng)到曲線圖上顯示值的方法
- C#實(shí)現(xiàn)隨鼠標(biāo)移動(dòng)窗體實(shí)例
- C#實(shí)現(xiàn)獲取鼠標(biāo)句柄的方法
- C#中winform實(shí)現(xiàn)自動(dòng)觸發(fā)鼠標(biāo)、鍵盤事件的方法
- C# 鼠標(biāo)穿透窗體功能的實(shí)現(xiàn)方法
- 解決C#獲取鼠標(biāo)相對(duì)當(dāng)前窗口坐標(biāo)的實(shí)現(xiàn)方法
- C# 禁用鼠標(biāo)中間鍵的方法
- C#實(shí)現(xiàn)鼠標(biāo)消息捕獲
相關(guān)文章
簡(jiǎn)單說(shuō)說(shuō)STL的內(nèi)存管理
<STL 源碼剖析>將其描述為空間配置器,理由是allocator可以將其它存儲(chǔ)介質(zhì)(例如硬盤)做為stl 容器的存儲(chǔ)空間。由于內(nèi)存是allocator管理的主要部分,因此,本文以STL內(nèi)存管理為出發(fā)點(diǎn)介紹allocator2013-09-09關(guān)于PCL出現(xiàn)"無(wú)法找到?pcl_commond.dll?文件程序無(wú)法執(zhí)行"的問(wèn)題及解決方法
這篇文章主要介紹了PCL出現(xiàn)"無(wú)法找到?pcl_commond.dll?文件程序無(wú)法執(zhí)行"的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07c語(yǔ)言合并兩個(gè)已排序數(shù)組的示例(c語(yǔ)言數(shù)組排序)
如何將兩個(gè)已排序數(shù)組合并成一個(gè)排序數(shù)組,下面我們給出使用c語(yǔ)言合并兩個(gè)已排序數(shù)組的示例,需要的朋友可以參考下2014-03-03C語(yǔ)言計(jì)算Robots機(jī)器人行走路線
這篇文章介紹了C語(yǔ)言計(jì)算Robots機(jī)器人行走路線,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12C語(yǔ)言編程中實(shí)現(xiàn)二分查找的簡(jiǎn)單入門實(shí)例
這篇文章主要介紹了C語(yǔ)言編程中實(shí)現(xiàn)二分查找的簡(jiǎn)單入門實(shí)例,需要的朋友可以參考下2015-12-12從txt中讀入數(shù)據(jù)到數(shù)組中(fscanf)的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇從txt中讀入數(shù)據(jù)到數(shù)組中(fscanf)的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12