C # - moving a control to mouse position

I am trying to take control of a cursor when a user clicks and drags a control. The problem is that 1.) the control does not move to the mouse position and 2.) the control flickers and flies everywhere. I tried several different ways to do this, but all still failed.

I tried:

protected override void OnMouseDown(MouseEventArgs e)
{
     while (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
          this.Location = e.Location;
     }
}

and

protected override void OnMouseMove(MouseEventArgs e)
{
     while (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
          this.Location = e.Location;
      }
}

but none of them work. Any help is appreciated and thanks in advance!

+3
source share
4 answers

Here's how to do it:

private Point _Offset = Point.Empty;

protected override void MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        _Offset = new Point(e.X, e.Y);
    }
}

protected override void MouseMove(object sender, MouseEventArgs e)
{
    if (_Offset != Point.Empty)
    {
        Point newlocation = this.Location;
        newlocation.X += e.X - _Offset.X;
        newlocation.Y += e.Y - _Offset.Y;
        this.Location = newlocation; 
    }
}

protected override void MouseUp(object sender, MouseEventArgs e)
{
    _Offset = Point.Empty;
}

_Offset : , , , , ( , , ).

if while s, .

+9

1 1. , button1_MouseMove 2. , ( newlocation = button1.Location;) 3. .

, ( ) .

Chigook

+2

Try moving the object according to the position of the mouse, and the code below is to collect the path to move the mouse and the place saved in arraylist to get the path that the mouse point moves. U should declare a list of arrays around the world.

 private void panel1_MouseMove(object sender, MouseEventArgs e)
        {



                if (e.Button == MouseButtons.Left)
                {
                    ArrayList inList = new ArrayList();
                    inList.Add(e.X);
                    inList.Add(e.Y);
                    list.Add(inList);
                }

       }

when the user clicks the button, the control should move along the path that the user dragged onto the screen

private void button1_Click_2(object sender, EventArgs e)
  {
       foreach (ArrayList li in list)
            {



                pic_trans.Visible = true;
                pic_trans.Location = new Point(Convert.ToInt32(li[0]), Convert.ToInt32(li[1]));
                pic_trans.Show();
             }
  }
+1
source
 private Point ptMouseDown=new Point();


 protected override void MouseDown(object sender, MouseEventArgs e)

{

    if (e.Button == MouseButtons.Left)

    {

        ptMouseDown = new Point(e.X, e.Y);

    }
}

protected override void MouseMove(object sender, MouseEventArgs e)
{

    if (_Offset != Point.Empty)

    {

        Pointf[] ptArr=new Pointf[]{this.Location};
        Point Diff=new Point(e.X-ptMouseDown.X,e.Y-ptMouseDown.Y);
        Matrix mat=new Matrix();
        mat.Translate(Diff.X,Diff.Y);
        mat.TransFromPoints(ptArr);
        this.Location=ptArr[0];
    }
}

    enter code here



protected override void MouseUp(object sender, MouseEventArgs e)

{

    _Offset = Point.Empty;

}
0
source

Source: https://habr.com/ru/post/1742559/


All Articles