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

发布于 2024-08-30 02:23:30 字数 554 浏览 8 评论 0原文

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

这些都不起作用。感谢任何帮助,并提前致谢!

I am trying to get a control to follow the cursor when the user clicks and drag the control. The problem is that 1.) the control doesn't go to the mouse's position, and 2.) the control flickers and flies all over the place. I've tried a few different methods of doing this, but all so far have failed.

I've 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 neither of these work. Any help is appreciated, and thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流星番茄 2024-09-06 02:23:30

操作方法如下:

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,因为它一个区别。

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 is used here for two purposes: keeping track of where the mouse was on the control when you initially clicked it, and also keeping track of whether the mouse button is down or not (so that the control doesn't get dragged when the mouse cursor goes over it and the button isn't down).

You definitely don't want to switch the ifs in this code to whiles, as it will make a difference.

贪了杯 2024-09-06 02:23:30

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

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

奇古克

there are mistakes in the answer 1
1.Set mouse handlers to control ,not to form
like button1_MouseMove
2.do not use this vector,but your control instead(Point newlocation = button1.Location;)
3.you do not need to override handlers.

in my test after these changes button(or other control) moves fine.

Chigook

你的笑 2024-09-06 02:23:30

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

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

Try this to move the object according to the mouse's position and the code below given is to gather the mouse's move path and the location saved in the arraylist to obtain the path where mouse point is moving. You have to declare the arraylist globally.

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 has to move in the path that the user dragged in 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();
    }
}
两相知 2024-09-06 02:23:30
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文