17

我试图通过拖动来移动名为pictureBox1 的控件。问题是,当它移动时,它会不断从鼠标周围的一个位置移动到另一个位置,但它确实跟随它......这是我的代码。如果你能帮助我,我将不胜感激

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    bool selected = false;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        selected = true;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (selected == true)
        {
            pictureBox1.Location = e.Location;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        selected = false;
    }

}
4

3 回答 3

43

一切你需要的:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private Point MouseDownLocation;


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

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

}
于 2013-05-05T13:25:22.117 回答
9

您还可以使用扩展名:

public static class CmponentsExtensions
{
  //Management of mouse drag and drop
    #region Menu and Mouse
    private static bool mouseDown;
    private static Point lastLocation;

    /// <summary>
    /// To enable control to be moved around with mouse
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="control"></param>
    public static void moveItselfWithMouse<T>(this T control) where T: Control
    { 
        control.MouseDown += (o, e)=> { mouseDown = true; lastLocation = e.Location; };
        control.MouseMove += (o, e) => 
        {
            if (mouseDown)
            {
                control.Location = new Point((control.Location.X - lastLocation.X) + e.X, (control.Location.Y - lastLocation.Y) + e.Y);
                control.Update();
            }
        };
        control.MouseUp += (o, e) => { mouseDown = false; } ;
    }


    public static void moveOtherWithMouse<T>(this T control, Control movedObject) where T : Control
    {
        control.MouseDown += (o, e) => { mouseDown = true; lastLocation = e.Location; };
        control.MouseMove += (o, e) =>
        {
            if (mouseDown)
            { 
                movedObject.Location = new Point((movedObject.Location.X - lastLocation.X) + e.X, (movedObject.Location.Y - lastLocation.Y) + e.Y);
                movedObject.Update();
            }
        };
        control.MouseUp += (o, e) => { mouseDown = false; };
    }

    #endregion
}

然后你需要通过一些控制来使用它:

在这种情况下,pictureBox1 移动了整个表单

pictureBox1.moveOtherWithMouse(this);

在这种情况下,您只移动图片框:

pictureBox1.moveItselfWithMouse();
于 2019-06-01T11:15:43.523 回答
5

试试这个在运行时使用鼠标移动图片框控件

 private void pictureBox7_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                xPos = e.X;
                yPos = e.Y;
            }
        }

        private void pictureBox7_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            PictureBox p = sender as PictureBox;

            if (p != null)
            {
                if (e.Button == MouseButtons.Left)
                {
                    p.Top += (e.Y - yPos);
                    p.Left += (e.X - xPos);
                }
            }

        }
于 2015-03-21T06:41:01.727 回答