C# 窗体。拖动事件
C# winforms. Drag events
我需要 运行 在关闭(结束)拖动点击时进行编码。这个活动怎么称呼?
这是主要示例,请查看以下屏幕截图以获取更多信息:
我做了我可以把汽车拖到其他图片框上,如下所示:
再重复一次 - 我需要知道事件是什么,然后你不去拖拽图片框?
我猜你在说 DragDrop. You can find example here: How to: Enable Drag-and-Drop Operations with the Windows Forms RichTextBox Control
没有关于何时在控件上释放拖动的事件,但您实际上并不需要一个。这就是我为模拟(我认为)您正在寻找的东西所做的。我使用了 this Whosebug answer
提供的代码
private Point? _mouseLocation;
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.MouseDown += this.pictureBox1_MouseDown;
this.pictureBox1.MouseUp += this.pictureBox1_MouseUp;
this.pictureBox1.MouseMove += this.pictureBox1_MouseMove;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ( this._mouseLocation.HasValue)
{
this.pictureBox1.Left = e.X + this.pictureBox1.Left - this._mouseLocation.Value.X;
this.pictureBox1.Top = e.Y + this.pictureBox1.Top - this._mouseLocation.Value.Y;
}
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
this._mouseLocation = null;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//Check if you've left-clicked if you want
this._mouseLocation = e.Location;
}
使用 this._mouseLocation = null;
将鼠标位置设置为空是您的 "drag released" 代码。
我需要 运行 在关闭(结束)拖动点击时进行编码。这个活动怎么称呼?
这是主要示例,请查看以下屏幕截图以获取更多信息:
我做了我可以把汽车拖到其他图片框上,如下所示:
再重复一次 - 我需要知道事件是什么,然后你不去拖拽图片框?
我猜你在说 DragDrop. You can find example here: How to: Enable Drag-and-Drop Operations with the Windows Forms RichTextBox Control
没有关于何时在控件上释放拖动的事件,但您实际上并不需要一个。这就是我为模拟(我认为)您正在寻找的东西所做的。我使用了 this Whosebug answer
提供的代码private Point? _mouseLocation;
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.MouseDown += this.pictureBox1_MouseDown;
this.pictureBox1.MouseUp += this.pictureBox1_MouseUp;
this.pictureBox1.MouseMove += this.pictureBox1_MouseMove;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ( this._mouseLocation.HasValue)
{
this.pictureBox1.Left = e.X + this.pictureBox1.Left - this._mouseLocation.Value.X;
this.pictureBox1.Top = e.Y + this.pictureBox1.Top - this._mouseLocation.Value.Y;
}
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
this._mouseLocation = null;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//Check if you've left-clicked if you want
this._mouseLocation = e.Location;
}
使用 this._mouseLocation = null;
将鼠标位置设置为空是您的 "drag released" 代码。