WPF 鼠标左键捕获点,用polyline连接起来

WPF 鼠标左键捕获点,用polyline连接起来

 Polyline lines;
        Line dot;
        Polyline cach;
        public Window1()
        {
            InitializeComponent();
             
            lines=new Polyline();
            lines.Stroke =new SolidColorBrush(Colors.Black);
            canvas.Children.Add(lines);
             
            cach=new Polyline();
            cach.Stroke =new SolidColorBrush(Colors.Black);
             
            canvas.Children.Add(cach);
             
            dot=new Line();
            dot.Stroke =new SolidColorBrush(Colors.Black);
            dot.StrokeDashArray.Add(4);
            canvas.Children.Add(dot);
        }
         
        void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var point= e.GetPosition(canvas);
            lines.Points.Add(point);
        }
         
        void Canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            cach.Points=lines.Points.Clone();
            cach.Points.Add(lines.Points[0]);
            lines.Points.Clear();
            dot.X1=dot.X2=dot.Y1=dot.Y2=0;
        }
        void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (lines.Points.Count>0) {
                var point=lines.Points[lines.Points.Count-1];
                dot.X1=point.X;
                dot.Y1=point.Y;
                point= e.GetPosition(canvas);
                dot.X2=point.X;
                dot.Y2=point.Y;
            }
        }

你可能感兴趣的:(WPF)