时间:2021-05-19
实现步骤:
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;
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
效果如下:C#实现代码?12345678910111213141516171819202122232425262728293031323334353637usi
官方提供了curl、post、php、ruby的实现示例,并没有C#的官方示例。既然提供了post的方式,那么就可以用C#实现,下面是实现代码:ASP.net百
本文实例讲述了C#实现DataList里面嵌套DataList的折叠菜单。分享给大家供大家参考,具体如下:点击前效果图如下:点击后效果图如下:具体实现代码如下:
本文实例讲述了C#实现DevGrid拖拽移动行的方法。分享给大家供大家参考。具体如下:完整实例代码点击此处本站下载。拖拽时带行截图效果实现代码如下://////
本文实例为大家分享了C#实现鼠标裁剪图像的具体代码,供大家参考,具体内容如下C#的图像裁剪很容易操作,这里给个实现的例子。关键是需要处理鼠标的事件和一些更新实现