#region 可移動按鈕“相關代碼”
Point mouse_offset;
private void Button_MouseDown(object sender , MouseEventArgs e)
{
mouse_offset = e.Location; //將當前鼠標相對於“窗體”左上角的座標賦值給mouse_offset
}
private void Button_MouseMove(object sender , MouseEventArgs e)
{
if ( e.Button == MouseButtons.Left )
{
//將相對屏幕座標轉換爲相對工作區的座標。
int left = PointToClient(Control.MousePosition).X - mouse_offset.X;
int top = PointToClient(Control.MousePosition).Y - mouse_offset.Y;
//左右可能越界
if(left<0)
left=0;
if ( left > this.Width )
left = this.Width - ( (Button)sender ).Width;
//上下可能越界
if ( top < 0 )
top = 0;
if ( top > this.Height )
top = this.Height - ( (Button)sender ).Height;
( (Button)sender ).Left = left;
( (Button)sender ).Top = top;
}
}
#endregion
轉載於:https://www.cnblogs.com/AsionTang/archive/2010/11/20/1885705.html