PictureBox moves when it should not in Event MouseDown c #

0

Good morning!

I have a problem with an application that I am developing. I have my own class that inherits from PictureBox, with several events. Among them a MouseDown and a MouseMove.

The fact is that when I click on that object in my class, I wanted the mouse to move to the center of the pictureBox and then start moving it from that central position.

The problem I have is that once I click on it, the pointer moves apparently to the center of the pictureBox, but drag it to the pictureBox, then the mouse pointer remains in the same position inside the pictureBox, but with the pictureBox and the mouse a little moved in that search of the center of it.

I put my code to clarify it a bit more:

Method that places the object

    public void Colocar(Control control, Unidad unidad, Point p)
    {
        unidad.Location = p;
        control.Controls.Add(unidad);
    }

MouseDown Event

bool clickPerformed = false; 
    bool clickMove = false; //Para saber si hay que moverlo
    private Point MouseDownLocation;
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);

        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(this.Location.X + this.Size.Width / 2, this.Location.Y + this.Size.Height / 2);

        clickPerformed = true; 
        Control tempSender = this.Parent; 
        tempSender.Invalidate(); 
        MouseDownLocation = e.Location;
        clickMove = true;
    }

MouseUp Event:

protected override void OnMouseUp(MouseEventArgs e)
    {
        this.Parent.Invalidate();
        clickMove = false;
        base.OnMouseDown(e);
    }

Method to draw a circle around:

    public void DrawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius)
    {
        g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
    }

MouseMove Event:

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        if (clickMove)
        {
            Left = e.X + Left - MouseDownLocation.X;
            Top = e.Y + Top - MouseDownLocation.Y;
        }
    }

Does anyone have a possible solution? Thank you very much in advance!

    
asked by Imrik 08.03.2017 в 08:58
source

0 answers