代码之家  ›  专栏  ›  技术社区  ›  Adam P

C#-将控件移动到鼠标位置

  •  3
  • Adam P  · 技术社区  · 15 年前

    我试图让一个控件在用户单击并拖动该控件时跟随光标。问题是1.)控件没有转到鼠标的位置,2.)控件会闪烁并到处飞。我尝试过几种不同的方法,但都失败了。

    我试过:

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

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

    但这两个都不管用。感谢您的帮助,并提前表示感谢!

    4 回复  |  直到 15 年前
        1
  •  10
  •   MusiGenesis    15 年前

    以下是操作方法:

    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、 就像它一样 有所作为。

        2
  •  2
  •   chigook    12 年前

    答案1有错误 1.将鼠标处理程序设置为控件,而不是窗体 像按钮一样移动鼠标 2.不要使用此向量,而是使用控件(Point newlocation=button1.Location;)

    在我的测试中,在这些更改之后,按钮(或其他控件)可以很好地移动。

    奇古克

        3
  •  1
  •   Draken R. Shilling    6 年前

    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);
        }
    }
    

    当用户单击按钮时,控件必须在用户在屏幕中拖动的路径中移动

    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();
        }
    }
    
        4
  •  0
  •   Community CDub    6 年前
    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];
        }
    }   
    
    protected override void MouseUp(object sender, MouseEventArgs e)
    {
        _Offset = Point.Empty;
    }