本例以四个Button控件作为代表,实现在程序运行时,用鼠标拖动控件,控件的位置随着鼠标的移动而移动。
首先,在Form中加入四个Button控件,并将Button的属性AllowDrop改为True,然后具体实现请看下面代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test_0626
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_MouseDown(object sender, MouseEventArgs e)
{
button4.Tag = e.Location; //得到按钮中鼠标的坐标
DoDragDrop(button4, DragDropEffects.Move); //开始拖放操作
}
private void button5_MouseDown(object sender, MouseEventArgs e)
{
button5.Tag = e.Location;
DoDragDrop(button5, DragDropEffects.Move);
}
private voi