方法一:直接通過修改窗體位置從而達到移動窗體的效果
方法二:直接偽裝發送單擊任務欄消息,讓應用程序誤以為單擊任務欄從而移動窗體
?
方法一
1.定義一個位置信息Point用于存儲鼠標位置
private Point mPoint;
2.給窗體等控件增加MouseDown和MouseMove事件
?
/// <summary>
/// 鼠標按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void panel1_MouseDown(object sender, MouseEventArgs e)
{mPoint = new Point(e.X, e.Y);
}/// <summary>
/// 鼠標移動
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void panel1_MouseMove(object sender, MouseEventArgs e)
{if (e.Button == MouseButtons.Left){this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);}
}
方法二:?
1.引入下面代碼 前提需要引入命名空間using System.Runtime.InteropServices
[DllImport("user32.dll")]
public?static?extern?bool?ReleaseCapture();[DllImport("user32.dll")]
public?static?extern?bool?SendMessage(IntPtr hwnd,?int?wMsg,?int?wParam,?int?lParam);private?const?int?VM_NCLBUTTONDOWN = 0XA1;//定義鼠標左鍵按下
private?const?int?HTCAPTION = 2;
2.增加鼠標按下事件發送消息,讓系統誤以為按下是標題欄
/// <summary>
/// 鼠標按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
//為當前應用程序釋放鼠標捕獲
ReleaseCapture();
//發送消息 讓系統誤以為在標題欄上按下鼠標
SendMessage((IntPtr)this.Handle, VM_NCLBUTTONDOWN, HTCAPTION, 0);
}
?