C#WinForm無邊框窗體移動方法、模仿滑鼠單擊標題欄移動窗體位置
阿新 • • 發佈:2022-03-30
定義一個位置資訊Point用於儲存滑鼠位置
private Point mPoint;
給窗體等控制元件增加MouseDown和MouseMove事件
/// <summary> 2 /// 滑鼠按下 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void panel1_MouseDown(object sender, MouseEventArgs e)7 { 8 mPoint = new Point(e.X, e.Y); 9 } 10 11 /// <summary> 12 /// 滑鼠移動 13 /// </summary> 14 /// <param name="sender"></param> 15 /// <param name="e"></param> 16 private void panel1_MouseMove(object sender, MouseEventArgs e)17 { 18 if (e.Button == MouseButtons.Left) 19 { 20 this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y); 21 } 22 }
這樣就可以隨意移動位置了。