【问题标题】:C# - Moving a control to the mouse's positionC# - 将控件移动到鼠标的位置
【发布时间】:2018-09-26 17:41:21
【问题描述】:

当用户单击并拖动控件时,我试图让控件跟随光标。问题是 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;
      }
}

但这些都不起作用。任何帮助表示赞赏,并提前感谢!

【问题讨论】:

    标签: c# controls mouse position flicker


    【解决方案1】:

    这是怎么做的:

    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 在这里用于两个目的:跟踪最初单击控件时鼠标在控件上的位置,以及跟踪鼠标按钮是否按下(这样控件不会当鼠标光标移过它并且按钮未按下时被拖动)。

    您绝对想将此代码中的ifs 切换为whiles,因为它有所作为。

    【讨论】:

    • 我已经尝试过了,但没有任何区别。不过我很感激你的努力。
    • +1:MusiGenesis' 代码对我来说就像一个魅力,但稍作修改:我创建了一个新的用户控件 -> 覆盖三个方法 OnMouseDown、OnMouseUp 和 OnMouseMove -> 第一行在这些方法中的每一个中都是对基本方法的调用,即 base.OnMouseDown(e)、base.OnMouseMove(e) 和 base.OnMouseUp(e)。 -> 其余代码按照 MusiGenesis 所讨论的那样进行。
    • 非常感谢!这个新答案就像一个魅力!我真的很感激!
    【解决方案2】:

    答案有错误 1 1.设置鼠标处理程序来控制,而不是形成 像 button1_MouseMove 2.不要使用这个向量,而是你的控件(Point newlocation = button1.Location;) 3.你不需要重写处理程序。

    在我的测试中,这些更改按钮(或其他控件)移动正常。

    奇谷

    【讨论】:

      【解决方案3】:

      尝试这样根据鼠标的位置移动对象,下面给出的代码是收集鼠标的移动路径和保存在arraylist中的位置,以获得鼠标点移动的路径。您必须全局声明数组列表。

      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】:
        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;
        }
        

        【讨论】:

          猜你喜欢
          • 2019-03-12
          • 1970-01-01
          • 2011-06-12
          • 1970-01-01
          • 2020-11-23
          • 2017-10-22
          • 2012-11-11
          • 2014-01-06
          • 1970-01-01
          相关资源
          最近更新 更多