private
Point
downPoint;
private
Rectangle
downRectangle;
private
Rectangle
lastRectangle;
private void
pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
downPoint = e.Location;
downRectangle =
new Rectangle(0, 0, ((Control)sender).Width, pictureBox1.Height);
downRectangle.Offset(((Control)sender).PointToScreen(new Point(0, 0)));
ControlPaint.DrawReversibleFrame(
downRectangle, Color.White, FrameStyle.Thick);
lastRectangle = downRectangle;
}
private void
pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
ControlPaint.DrawReversibleFrame(
lastRectangle, Color.White, FrameStyle.Thick);
Rectangle rectangle = downRectangle;
rectangle.Offset(e.X - downPoint.X, e.Y - downPoint.Y);
ControlPaint.DrawReversibleFrame(
rectangle, Color.White, FrameStyle.Thick);
lastRectangle = rectangle;
}
private void
pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
ControlPaint.DrawReversibleFrame(
lastRectangle, Color.White, FrameStyle.Thick);
pictureBox1.Location = new Point(
((Control)sender).Location.X + e.X - downPoint.X,
((Control)sender).Location.Y + e.Y - downPoint.Y);
}
本文介绍了一个使用C#实现的简单图片拖动功能。通过MouseDown、MouseMove和MouseUp事件处理程序,用户可以在PictureBox中拖动图片。代码展示了如何捕捉鼠标事件,并通过ControlPaint绘制临时边框来提供视觉反馈。

1472

被折叠的 条评论
为什么被折叠?



