现在需要在picturebox中用鼠标左键点击一系列点,并按顺序连接起来,然后双击鼠标左键按下最后一个点弹出对话框以求得距离

解决方案 »

  1.   

    如何响应mousedown,mouseup,doubleclick
      

  2.   

    private System.Drawing.Drawing2D.GraphicsPath  path=new System.Drawing.Drawing2D.GraphicsPath ();
            private Point org=Point.Empty;
            private double distance = 0;
           
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (org == Point.Empty)
                    {
                        org = e.Location;
                    }
                    else
                    {
                        this.distance += this.GetDistance(org, e.Location);
                        path.AddLine(org, e.Location);
                        this.org = e.Location;
                        this.pictureBox1.Invalidate();
                       
                   }
                }
            }        private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.DrawPath(Pens.Red, this.path);
            }        private double GetDistance(Point pt1, Point pt2)
            {
                int h= pt1.X - pt2.X;
                int v = pt1.Y - pt2.Y;
                return Math.Sqrt(h * h + v * v);
            }        private void pictureBox1_DoubleClick(object sender, EventArgs e)
            {
                MessageBox.Show(this.distance.ToString());
                this.path = new System.Drawing.Drawing2D.GraphicsPath();
                this.org = Point.Empty;
                this.distance = 0;
            }
      

  3.   

    谢谢jointan() 兄弟,如果再添加一个求面积的按钮呢
      

  4.   

    如果求面积就复杂了,用算法写,估计5000元不一定有人给你写.
    但还是有一个非常低效的求面积方法(而且精度差),就是在另一个BITMAP里面填充这个Path,然后用测点颜色的方式求面积.
      

  5.   

    哈哈,好的,谢谢你啊,jointan兄