2010-04-24 67 views
3

我想要获得一个控件,以便在用户单击并拖动控件时跟随光标。问题是:1)控制器不能进入鼠标的位置; 2)控制器闪烁并飞到了所有的地方。我尝试了一些不同的方法来做到这一点,但迄今为止都失败了。C# - 将控件移动到鼠标的位置

我已经试过:

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

但无论这些工作。任何帮助表示赞赏,并提前致谢!

回答

9

这里是如何做到这一点:

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 s到while S,因为它有所作为。

+0

我已经试过这拖的路径移动,它不会有所作为。我欣赏尽管。 – 2010-04-24 04:12:46

+3

+1:MusiGenesis的代码对我来说就像一个魅力,但有一点修改:我创建了一个新的用户控件 - >重写三个方法OnMouseDown,OnMouseUp和OnMouseMove - >每个方法的第一行是调用基本方法,即base.OnMouseDown(e),base.OnMouseMove(e)和base.OnMouseUp(e)。 - 其余代码随着MusiGenesis讨论。 – 2010-04-24 04:41:29

+1

非常感谢!这个新的答案就像一个魅力!我真的很感激它! – 2010-04-24 04:49:44

2

有在回答1个 1.设置鼠标处理程序,以控制错误,不形成像 button1_MouseMove 2.do不能使用该载体,但是您的控制,而不是(点newlocation = button1.Location) 3你不需要重载处理程序。

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

Chigook

0
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; 

} 
1

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

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