用鼠标移动实例化对象(Visual Studio C#)
本文关键字:Visual Studio 对象 鼠标 移动 实例化 | 更新日期: 2023-09-27 17:57:27
现在,如果我点击一个按钮,我会创建一个新的图片框,里面装满.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);
}
我昨天刚刚在做这个。我不确定这是否是最好的方法,但它有效。它使用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);
}