通过鼠标任意拖动.NET Winform窗体中的控件

在Winform窗体中通过鼠标拖动,改变控件的位置。在拖动过程中,跟随鼠标显示一个与被拖动控件大小一样的黑框,用以模拟拖动效果。如下图:
以下是源代码。这里拖动了一个Button控件。如果需要,还可以在拖动时改变光标。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace WindowsApplication1
  9. {
  10.     public partial class FormDrag : Form
  11.      {
  12.         //将被拖动的控件
  13.         private Control control;
  14.         public FormDrag()
  15.          {
  16.              InitializeComponent();
  17.             this.Paint += new System.Windows.Forms.PaintEventHandler(this.FormDrag_Paint);
  18.              control = new Button();
  19.              control.MouseDown += new MouseEventHandler(control_MouseDown);
  20.              control.MouseMove += new MouseEventHandler(control_MouseMove);
  21.              control.MouseUp += new MouseEventHandler(control_MouseUp);
  22.             this.Controls.Add(control);            
  23.          }
  24.        
  25.         //鼠标按下坐标(control控件的相对坐标)
  26.          Point mouseDownPoint = Point.Empty;
  27.         //显示拖动效果的矩形
  28.          Rectangle rect = Rectangle.Empty;
  29.         //是否正在拖拽
  30.         bool isDrag = false;
  31.         void control_MouseDown(object sender, MouseEventArgs e)
  32.          {
  33.             if (e.Button == MouseButtons.Left)
  34.              {
  35.                  mouseDownPoint = e.Location;
  36.                 //记录控件的大小
  37.                  rect = control.Bounds;
  38.              }
  39.          }
  40.         void control_MouseMove(object sender, MouseEventArgs e)
  41.          {
  42.             if (e.Button == MouseButtons.Left)
  43.              {
  44.                  isDrag = true;
  45.                 //重新设置rect的位置,跟随鼠标移动
  46.                  rect.Location = getPointToForm(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y));
  47.                 this.Refresh();
  48.                 
  49.              }
  50.          }
  51.         void control_MouseUp(object sender, MouseEventArgs e)
  52.          {
  53.             if (e.Button == MouseButtons.Left)
  54.              {
  55.                 if (isDrag)
  56.                  {
  57.                      isDrag = false;
  58.                     //移动control到放开鼠标的地方
  59.                      control.Location = rect.Location;
  60.                     this.Refresh();
  61.                  }
  62.                  reset();
  63.              }
  64.          }
  65.         //重置变量
  66.         private void reset()
  67.          {
  68.              mouseDownPoint = Point.Empty;
  69.              rect = Rectangle.Empty;
  70.              isDrag = false;
  71.          }
  72.         //窗体重绘
  73.         private void FormDrag_Paint(object sender, PaintEventArgs e)
  74.          {
  75.             if (rect != Rectangle.Empty)
  76.              {
  77.                 if (isDrag)
  78.                  {//画一个和Control一样大小的黑框
  79.                      e.Graphics.DrawRectangle(Pens.Black, rect);
  80.                  }
  81.                 else
  82.                  {
  83.                      e.Graphics.DrawRectangle(new Pen(this.BackColor), rect);
  84.                  }
  85.              }
  86.          }
  87.         //把相对与control控件的坐标,转换成相对于窗体的坐标。
  88.         private Point getPointToForm(Point p)
  89.          {
  90.             return this.PointToClient(control.PointToScreen(p));
  91.          }
  92.         
  93.      }
  94. }
如果你想要实现winform picbox通过鼠标左键拖动图片,但不想拖动控件本身,可以按照以下步骤进行: 1. 在窗体上添加一个PictureBox控件,并设置其SizeMode属性为AutoSize,以自适应图片大小。 2. 在PictureBox的MouseDown事件,判断鼠标左键是否按下,如果是,则记录下鼠标的位置和图片的位置。 3. 在PictureBox的MouseMove事件,判断鼠标左键是否按下,如果是,则计算鼠标移动的距离,并将图片的位置加上这个距离,实现拖动效果。 4. 在PictureBox的MouseUp事件,将记录的位置信息清空,以便下一次拖动。 以下是示例代码: ```csharp public partial class Form1 : Form { private Point mouseOffset; // 鼠标位置与图片位置的差值 private Point imageLocation; // 图片的位置 public Form1() { InitializeComponent(); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOffset = new Point(-e.X, -e.Y); imageLocation = pictureBox1.ImageLocation; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); pictureBox1.ImageLocation = imageLocation; pictureBox1.ImageLocation = mousePos; } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { mouseOffset = Point.Empty; imageLocation = Point.Empty; } } ``` 这样,就可以通过鼠标左键拖动图片,而不会拖动控件本身。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值