日期:2014-05-17 浏览次数:20831 次
private Point MouseMaggin;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseMaggin = e.Location;
button1.DoDragDrop(button1, DragDropEffects.Move);
}
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
//当Button被拖拽到WinForm上时候,出现鼠标效果
if ((e.Data.GetDataPresent(typeof(Button))))
{
e.Effect = DragDropEffects.Move;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
MoveButton(e);
}
private void MoveButton(DragEventArgs e)
{
//得到拖放对象中的Button
Button btn = e.Data.GetData(typeof(Button)) as Button;
//计算Button相对于From的X,Y坐标。否则直接使用X,Y是屏幕坐标
Point newLocation = this.PointToClient(new Point(e.X, e.Y));
newLocation.Offset(0 - MouseMaggin.X, 0 - MouseMaggin.Y);
btn.Location = newLocation;
}
private void Form1_DragOver(object sender, DragEventArgs e)
{
Point currentLocation = this.PointToClient(new Point(e.X, e.Y));
&nb