通过鼠标移动控件

时间:2011-02-08 15:59:08

标签: c#

我要用鼠标移动一个按钮,一切正常,但是当我在按钮窗口上移动鼠标时,按钮的左上方(左上角)将位于光标位置。

我不希望这种情况发生。我的代码中的错误在哪里?

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        clicked = true;
    }

}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    if (clicked)
    {
        Point p = new Point();//in form coordinates
        p.X =  e.X + button1.Left;
        p.Y =  e.Y + button1.Top;
        button1.Left = p.X;
        button1.Top = p.Y ;

    }

}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    clicked = false;   
}

4 个答案:

答案 0 :(得分:8)

这就是你所需要的一切

    private Point MouseDownLocation;

    private void MyControl_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private void MyControl_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            this.Left = e.X + this.Left - MouseDownLocation.X;
            this.Top = e.Y + this.Top - MouseDownLocation.Y;
        }
    }

答案 1 :(得分:3)

我找到了......

这是完整的代码:

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        Point p = ConvertFromChildToForm(e.X, e.Y, button1);
        iOldX = p.X;
        iOldY = p.Y;
        iClickX = e.X;
        iClickY = e.Y;
        clicked = true;
    }

}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
    if (clicked)
    {
        Point p = new Point();//in form coordinates
        p.X =  e.X + button1.Left;
        p.Y =  e.Y + button1.Top;
        button1.Left = p.X - iClickX;
        button1.Top = p.Y - iClickY;

    }

}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    clicked = false;   
}

答案 2 :(得分:1)

我不知道我是否做得对,但以防万一...如果问题是将光标定位在按钮(或其他组件)的中心,你可以通过考虑宽度和高度:

 private void button1_MouseMove(object sender, MouseEventArgs e) {
      if (clicked) {
        Point p = new Point(); //in form coordinates
        p.X = e.X + button1.Left - (button1.Width/2);
        p.Y = e.Y + button1.Top - (button1.Height/2);
        button1.Left = p.X;
        button1.Top = p.Y;
      }
    }

答案 3 :(得分:1)

我认为这是您需要的完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoveButton
{
public partial class Form1 : Form
{
    bool clicked = false;
    Point iOld = new Point();
    Point iClick = new Point();
    public Form1()
    {
        InitializeComponent();
    }
    private Point ConvertFromChildToForm(int x, int y, Control control)
    {
        Point p = new Point(x, y);
        control.Location = p;
        return p;
    }
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Point p = ConvertFromChildToForm(e.X, e.Y, button1);
            iOld.X = p.X;
            iOld.Y = p.Y;
            iClick.X = e.X;
            iClick.Y = e.Y;
            clicked = true;
        }

    }

    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
        if (clicked)
        {
            Point p = new Point();//in form coordinates
            p.X = e.X + button1.Left;
            p.Y = e.Y + button1.Top;
            button1.Left = p.X - iClick.X;
            button1.Top = p.Y - iClick.Y;

        }

    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        clicked = false;
    }
   }
}