C# 移動視窗 適用於有標題列和無標題列表單 超級簡單版

來源:互聯網
上載者:User

適用於有標題列和無標題列表單,適用於表單內控制項,當然 Form 也不例外,只要能添加 MouseDown、MouseMove 事件,無需添加 MouseUp 事件,也無需考慮 Form 是否有標題列。

本方法採用 Control.MousePosition,而沒有採用 MouseEventArgs e 擷取資料,是因為這樣無需考慮表單有無標題列和邊框的大小,也無需考慮是表單引發事件,還是表單內控制項引發事件。

 

 

代碼


// 擷取表單的螢幕座標和滑鼠游標的位置(螢幕座標) 
private void MainForm_MouseDown(object sender, MouseEventArgs e)

     mousePoint = Control.MousePosition; 
     formPoint = this.Location;
 }
 
  // 滑鼠游標的螢幕座標
 Point mousePoint;
 
 // 表單的螢幕座標
 Point formPoint;

 // 響應滑鼠移動,並移動視窗
 private void MainForm_MouseMove(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left)
     {
        Point mousePos = Control.MousePosition;
        this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
     }
 }
 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.