拖拽矩形
2016-04-24 22:44
225 查看
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();
}
}
相关文章推荐
- hiho 47 48 拓扑排序
- 字符转换
- NullPointerException
- Annotation(注解)代替web.xml完成servlet配置
- php笔记
- 经验总结
- TCP/IP、Http、Socket的区别
- 《Java程序设计》第8周学习总结
- 读者写者问题
- 技术分析之JavaScript
- Java企业实训 - 01 - Java前奏
- [改善Java代码]构造函数尽量简化
- Java企业实训 - 01 - Java前奏
- u3d脚本入门二,键盘控制物体上下左右和缩放(原创)
- Kullback-Leibler Divergence
- Android 自定义水平滚动的容器
- 链表“倒数”问题两个解决思路
- 学会写博客
- python 开发环境搭建
- 编写可维护的JavaScript—语句和表达式&变量、函数和运算符