Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在picturebox中发布移动形状_C#_Picturebox - Fatal编程技术网

C# 在picturebox中发布移动形状

C# 在picturebox中发布移动形状,c#,picturebox,C#,Picturebox,我在尝试移动picturebox中的形状时遇到问题,选中时形状会正确移动,但被困在一个小区域中,我无法在整个picturebox中移动它。我想知道我的问题是在绘画事件还是移动事件中,因为我在整个picturebox中绘制形状时没有问题 代码如下: private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left)

我在尝试移动picturebox中的形状时遇到问题,选中时形状会正确移动,但被困在一个小区域中,我无法在整个picturebox中移动它。我想知道我的问题是在绘画事件还是移动事件中,因为我在整个picturebox中绘制形状时没有问题

代码如下:

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
                pictureBox1.Cursor = Cursors.Hand;

                MyPoint tempPtDep = new MyPoint(e.X, e.Y);
                ((Shape)listBox1.SelectedItem).gsPtAcc = new MyPoint(
                      Math.Min(ptdep.x, tempPtDep.x),
                      Math.Min(ptdep.y, tempPtDep.y));

                pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {

        if (shapeList != null)
        {
            Graphics g = e.Graphics;
            foreach (Shape elem in shapeList)
            {
                if(elem.Equals(listBox1.SelectedItem as Shape))
                {

                    pen = new Pen(Color.RoyalBlue, 3);
                    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                }
                else
                    pen = new Pen(elem.Couleur, 3);


                if (elem is Segment)
                {
                    seg = elem as Segment;
                    seg.WDraw(g, pen, seg.gsPtAcc);
                }
                else if (elem is Circle)
                {
                    cercle = elem as Circle;
                    cercle.WDraw(g, pen, cercle.gsPtAcc);
                }
                else if (elem is Square)
                {
                    carre = elem as Square;
                    carre.WDraw(g, pen, carre.gsPtAcc);
                }
            }
        }            
    }

看来你的
Min
s和
ptdep
是罪魁祸首。代码中的
ptdep
来自何处?从鼠标向下,它从事件arg获取鼠标位置。因此,您在当前鼠标位置和单击鼠标位置的两个方向(x,y)上谈论
Min
。这是想干什么?我猜你只能向上向左移动。这似乎是这里的问题。你是在尝试实现拖放吗?不,不是真的,那么我应该怎么做才能实现拖放(数学不是我的强项:s)在我看来,如果你只是想将
形状移动到鼠标当前所在的位置,那么整个
Min
部分是不必要的。事件get仅在您通过
PictureBox
时触发,因此请尝试
((Shape)listBox1.SelectedItem)。gsPtAcc=new MyPoint(temptdep.x,temptdep.y)。还有其他一些事情需要注意,比如根本不检查是否选择了任何东西,或者选择的确实是一个形状。因为我不知道你为什么要抓取
鼠标\u向下
点,你到底想做什么,很明显代码丢失了,这是我能给你的最好建议。