拖拽矩形
来源:互联网 发布:node 模块化开发 编辑:程序博客网 时间:2024/05/22 23:27
public MainWindow()
{
InitializeComponent();
onDraw();
}
protected void onDraw()
{
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 5;
var gc = new GeometryConverter();
myPath.Data = (Geometry)gc.ConvertFromString("m 10,50 L 100,50 100,100 10,100 z");
List<Point> pointList = new List<Point>();
pointList.Add(new Point(10, 50));
pointList.Add(new Point(100, 50));
pointList.Add(new Point(100, 100));
pointList.Add(new Point(10, 100));
myPath.Tag = pointList;
myPath.Cursor = Cursors.Hand;
canvas.Children.Add(myPath);
myPath.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
myPath.MouseMove += new MouseEventHandler(Element_MouseMove);
myPath.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
}
public bool isDragDropInEffect = false;
Point positionInitial;
List<Point> pointLists = null;
void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Path path = sender as Path;
List<Point> pointList = path.Tag as List<Point>;
if (pointLists != null)
{
path.Data = (Geometry)(new GeometryConverter()).
ConvertFromString(string.Format("m {0},{1} L {2},{3} {4},{5} {6},{7} z",
pointLists[pointLists.Count - 4].X, pointLists[pointLists.Count - 4].Y,
pointLists[pointLists.Count - 3].X, pointLists[pointLists.Count - 3].Y,
pointLists[pointLists.Count - 2].X, pointLists[pointLists.Count - 2].Y,
pointLists[pointLists.Count - 1].X, pointLists[pointLists.Count - 1].Y));
}
isDragDropInEffect = true;
positionInitial = e.GetPosition(path);
path.CaptureMouse();
path.Cursor = Cursors.Hand;
}
void Element_MouseMove(object sender, MouseEventArgs e)
{
if (isDragDropInEffect)
{
Path path = sender as Path;
if (path != null)
{
double xPos = e.GetPosition(path).X - positionInitial.X;
double yPos = e.GetPosition(path).Y - positionInitial.Y;
List<Point> pointList = path.Tag as List<Point>;
Point point1 = new Point(pointList[0].X + xPos, pointList[0].Y + yPos);
Point point2 = new Point(pointList[1].X + xPos, pointList[1].Y + yPos);
Point point3 = new Point(pointList[2].X + xPos, pointList[2].Y + yPos);
Point point4 = new Point(pointList[3].X + xPos, pointList[3].Y + yPos);
path.Data = (Geometry)(new GeometryConverter()).
ConvertFromString(string.Format("m {0},{1} L {2},{3} {4},{5} {6},{7} z",
point1.X, point1.Y,
point2.X, point2.Y,
point3.X, point3.Y,
point4.X, point4.Y));
pointList = new List<Point>();
pointList.Add(point1);
pointList.Add(point2);
pointList.Add(point3);
pointList.Add(point4);
path.Tag = pointList;
positionInitial = e.GetPosition(null);
}
}
}
void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragDropInEffect)
{
Path path = sender as Path;
isDragDropInEffect = false;
path.ReleaseMouseCapture();
}
}
{
InitializeComponent();
onDraw();
}
protected void onDraw()
{
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 5;
var gc = new GeometryConverter();
myPath.Data = (Geometry)gc.ConvertFromString("m 10,50 L 100,50 100,100 10,100 z");
List<Point> pointList = new List<Point>();
pointList.Add(new Point(10, 50));
pointList.Add(new Point(100, 50));
pointList.Add(new Point(100, 100));
pointList.Add(new Point(10, 100));
myPath.Tag = pointList;
myPath.Cursor = Cursors.Hand;
canvas.Children.Add(myPath);
myPath.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
myPath.MouseMove += new MouseEventHandler(Element_MouseMove);
myPath.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
}
public bool isDragDropInEffect = false;
Point positionInitial;
List<Point> pointLists = null;
void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Path path = sender as Path;
List<Point> pointList = path.Tag as List<Point>;
if (pointLists != null)
{
path.Data = (Geometry)(new GeometryConverter()).
ConvertFromString(string.Format("m {0},{1} L {2},{3} {4},{5} {6},{7} z",
pointLists[pointLists.Count - 4].X, pointLists[pointLists.Count - 4].Y,
pointLists[pointLists.Count - 3].X, pointLists[pointLists.Count - 3].Y,
pointLists[pointLists.Count - 2].X, pointLists[pointLists.Count - 2].Y,
pointLists[pointLists.Count - 1].X, pointLists[pointLists.Count - 1].Y));
}
isDragDropInEffect = true;
positionInitial = e.GetPosition(path);
path.CaptureMouse();
path.Cursor = Cursors.Hand;
}
void Element_MouseMove(object sender, MouseEventArgs e)
{
if (isDragDropInEffect)
{
Path path = sender as Path;
if (path != null)
{
double xPos = e.GetPosition(path).X - positionInitial.X;
double yPos = e.GetPosition(path).Y - positionInitial.Y;
List<Point> pointList = path.Tag as List<Point>;
Point point1 = new Point(pointList[0].X + xPos, pointList[0].Y + yPos);
Point point2 = new Point(pointList[1].X + xPos, pointList[1].Y + yPos);
Point point3 = new Point(pointList[2].X + xPos, pointList[2].Y + yPos);
Point point4 = new Point(pointList[3].X + xPos, pointList[3].Y + yPos);
path.Data = (Geometry)(new GeometryConverter()).
ConvertFromString(string.Format("m {0},{1} L {2},{3} {4},{5} {6},{7} z",
point1.X, point1.Y,
point2.X, point2.Y,
point3.X, point3.Y,
point4.X, point4.Y));
pointList = new List<Point>();
pointList.Add(point1);
pointList.Add(point2);
pointList.Add(point3);
pointList.Add(point4);
path.Tag = pointList;
positionInitial = e.GetPosition(null);
}
}
}
void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isDragDropInEffect)
{
Path path = sender as Path;
isDragDropInEffect = false;
path.ReleaseMouseCapture();
}
}
0 0
- 拖拽矩形
- 动态创建矩形及拖拽
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 矩形
- 使用Java Swing绘制随鼠标拖拽可见轨迹的矩形(不会一直绘制矩形,而是类似ps中的矩形工具)
- c# 模仿鼠标拖拽矩形框 张宇轩
- JAVA鼠标屏幕绘制拖拽删除矩形
- Android面试题——多线程,同步的实现,wait和sleep的不同
- c++上机第四次实验-【项目3:乘法口诀表】
- 字符转换
- [NOI2005] [BZOJ1500] 维修数列 - splay
- hiho 47 48 拓扑排序
- 拖拽矩形
- php笔记
- 趣味素数-哥德巴赫猜想-java
- Android中处理崩溃异常
- welcome-file-list不起作用
- poj 2355(简单dp)
- ArchLinux上使用Python
- Hibernate 操作数据库
- 技术分析之JQuery