拖拽矩形

来源:互联网 发布: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();
            }
        }
0 0