用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)框選過程中獲取的鼠標(biāo)坐標(biāo)為基點(diǎn)計(jì)算框選的矩形的4點(diǎn)坐標(biāo),4點(diǎn)坐標(biāo)以順時(shí)針方向布點(diǎn)。
3.通過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)移動并顯示數(shù)據(jù)
- C#簡單獲取全屏中鼠標(biāo)焦點(diǎn)位置坐標(biāo)的方法示例
- C#實(shí)現(xiàn)的鼠標(biāo)鉤子
- C#鍵盤鼠標(biāo)鉤子實(shí)例
- C#實(shí)現(xiàn)鼠標(biāo)移動到曲線圖上顯示值的方法
- C#實(shí)現(xiàn)隨鼠標(biāo)移動窗體實(shí)例
- C#實(shí)現(xiàn)獲取鼠標(biāo)句柄的方法
- C#中winform實(shí)現(xiàn)自動觸發(fā)鼠標(biāo)、鍵盤事件的方法
- C# 鼠標(biāo)穿透窗體功能的實(shí)現(xiàn)方法
- 解決C#獲取鼠標(biāo)相對當(dāng)前窗口坐標(biāo)的實(shí)現(xiàn)方法
- C# 禁用鼠標(biāo)中間鍵的方法
- C#實(shí)現(xiàn)鼠標(biāo)消息捕獲
相關(guān)文章
C語言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn)
這篇文章主要介紹了C語言指針如何實(shí)現(xiàn)字符串逆序反轉(zhuǎn),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
C++ OpenCV實(shí)現(xiàn)圖像雙三次插值算法詳解
圖像雙三次插值的原理,就是目標(biāo)圖像的每一個(gè)像素都是由原圖上相對應(yīng)點(diǎn)周圍的4x4=16個(gè)像素經(jīng)過加權(quán)之后再相加得到的。本文主要介紹了通過C++ OpenCV實(shí)現(xiàn)圖像雙三次插值算法,需要的可以參考一下2021-12-12
c++使用單例模式實(shí)現(xiàn)命名空間函數(shù)案例詳解
這篇文章主要介紹了c++使用單例模式實(shí)現(xiàn)命名空間函數(shù),本案例實(shí)現(xiàn)一個(gè)test命名空間,此命名空間內(nèi)有兩個(gè)函數(shù),分別為getName()和getNameSpace(),本文結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2023-04-04

