在主窗体中添加一个button,实现button的移动.
public partial class Form1 : Form
{
private Point beforeP;//开始位置
private Point afterP;//结束位置
public Form1()
{
InitializeComponent();
}
private void btn_MouseDown(object sender, MouseEventArgs e)//控件的鼠标按下事件
{
beforeP = e.Location;//鼠标按下时开始移动,记录鼠标位置
}
private void btn_MouseMove(object sender, MouseEventArgs e)//控件的鼠标移动事件
{
if (e.Button == MouseButtons.Left)
{
afterP.X = btn.Left + (e.X - beforeP.X);
afterP.Y = btn.Top + (e.Y - beforeP.Y);
btn.Location = afterP;//控件最后的位置是控件的起始位置+鼠标的位移
}
}
}
有其他好方法的话,继续收录..