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

时间:2016-07-18 03:16:20

标签: c# winforms

现在,如果我点击一个按钮,我会创建一个装满.png(汽车)的新图片框。我希望在实例化之后用鼠标移动汽车,我无法弄清楚如何做到这一点。我理解(我认为)如何拖动已经在屏幕上的图片框,但不是那个以程序方式生成的图片框。

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

1 个答案:

答案 0 :(得分:0)

我昨天刚刚开始工作。我不确定这是不是最好的方式,但它有效。它使用control + mousedown事件来启动拖动。

创建子控件时,我在下面添加鼠标事件处理程序。

 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

框架控件只是一个代表孩子的空用户控件 在拖曳期间。它使拖动更顺畅。

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功能

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