简体   繁体   English

用鼠标移动实例化的对象(Visual Studio C#)

[英]Moving an instantiated object with mouse (Visual Studio C#)

Right now if I click a button I create a new picturebox filled with a .png (a car). 现在,如果我单击一个按钮,则会创建一个新的图片框,其中填充了.png(汽车)。 I wish to move the car with the mouse after it is instantiated and I can't figure out how to do that. 我希望在实例化鼠标之后移动汽车,但我不知道该怎么做。 I understand (I think) how to drag a picturebox that is already on the screen, but not one that is generated programmically. 我了解(我认为)如何拖动屏幕上已经存在的图片框,而不是以编程方式生成的图片框。

public void CreatePatrolCar()
    {
        int picX = Properties.Resources.police.Width;
        int picY = Properties.Resources.police.Height;


        PictureBox pc = new PictureBox();
        pc.Image = Properties.Resources.police;
        pc.Size = new Size(picX / 3, picY / 3);
        pc.SizeMode = PictureBoxSizeMode.StretchImage;
        pc.Location = new Point(100, 100);

        Controls.Add(pc);            
    }

I was just working on this yesterday. 我昨天只是在做这个。 I'm not sure if this is the best way but it works. 我不确定这是否是最好的方法,但是它可以工作。 It uses a control + mousedown event to initiate the drag. 它使用Control + mousedown事件来启动拖动。

When the child control is created I add the mouse event handlers below. 创建子控件后,我在下面添加了鼠标事件处理程序。

 private void btnNotes_Click(object sender, EventArgs e)
    {
        if (_editor == null)
        {
            _editor = new VimControl();
            _editor.Location = new Point(400, 200);
            _editor.Size = _editor.MinimumSize;
            _editor.vimTextBox.Text = "Hello World!";
            _editor.vimTextBox.MouseDown += HandleMouseDown;
            _editor.vimTextBox.MouseMove += HandleMouseMove;
            _editor.vimTextBox.MouseUp += HandleMouseUp;
            this.SuspendLayout();
            this.Controls.Add(_editor);
            _editor.BringToFront();
            this.ResumeLayout(true);
        }

        _editor.Show();


    }

#region Drag Child
private Point? _mouseDown = null;
private Point? _mouseLast = null;
private Control frame = null;

/// <summary>
/// Control click to begin drag child.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HandleMouseDown(object sender, MouseEventArgs e)
{
    var child = sender as Control;
    Log.Message("Sender: {0}", child == null ? "Unknown" : child.Name);

    Log.Message("{0} MouseDown at ({1}, {2})", e.Button, e.X, e.Y);
    if (e.Button == MouseButtons.Left && (Control.ModifierKeys & Keys.Control) == Keys.Control)
    {
        _mouseDown = e.Location;
        _mouseLast = e.Location;
        frame = FormHelper.FrameControl(_editor.Size, _editor.Location);
        this.Controls.Add(frame);
        frame.BringToFront();
    }
}

private void HandleMouseMove(object sender, MouseEventArgs e)
{
    var child = sender as Control;
    Log.Message("Sender: {0}", child == null ? "Unknown" : child.Name);

    if (child == null) return;

    if (_mouseDown.HasValue)
    {
        Point delta = MyMath.Delta(_mouseLast.Value, e.Location);
        frame.Left = frame.Left + delta.X;
        frame.Top = frame.Top + delta.Y;
        _mouseLast = e.Location;
    }
}

private void HandleMouseUp(object sender, MouseEventArgs e)
{
    var child = sender as Control;
    Log.Message("My {0} MouseUp at ({1}, {2})", e.Button, e.X, e.Y);
    if (e.Button == MouseButtons.Left && _mouseDown.HasValue)
    {
        _mouseDown = null;
        _mouseLast = e.Location;
        this.SuspendLayout();
        {
            child.Location = frame.Location;
            this.Controls.Remove(frame);
            frame.Dispose();
            frame = null;
        }
        this.ResumeLayout(true);
        child.Show();
    }
}
#endregion

The frame control is just an empty user control that stands in for the child during the drag. 框架控件只是一个空的用户控件,它在拖动过程中代表孩子。 It makes the drag much smoother. 它使拖动更加平滑。

public static UserControl FrameControl(Size size, Point location)
    {
        UserControl frame = new UserControl();
        frame.Location =  location;
        frame.Size =  size;
        frame.BorderStyle = BorderStyle.Fixed3D;
        frame.BackColor = Color.Transparent;

        return frame;
    }

Delta Helper function Delta Helper功能

public static Point Delta(Point p1, Point p2)
{
    return new Point(p2.X - p1.X, p2.Y - p1.Y);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM