C#之設定無邊框後如何移動表單

來源:互聯網
上載者:User

標籤:

轉載:http://www.cnblogs.com/techmango/archive/2012/03/31/2427523.html

 

第一種,利用windows的訊息機制來實現:

首先﹐.定義鼠標左鍵按下時的Message標識﹔其次﹐在Form1_MouseDown方法﹐讓操作系統誤以為是按下標題列。

1.定義鼠標左鍵按下時的Message標識

1    private const int WM_NCLBUTTONDOWN = 0XA1;   //.定義鼠標左鍵按下2    private const int HTCAPTION     = 2;

2.讓操作系統誤以為是按下標題列

1 private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)2    {   3     //為當前的應用程式釋放鼠標鋪獲4     ReleaseCapture();5     //發送訊息﹐讓系統誤以為在標題列上按下鼠標6     SendMessage((int)this.Handle,WM_NCLBUTTONDOWN,HTCAPTION,0);7    }

3.申明程式中所Windows的API函數

1    [DllImport("user32.dll",EntryPoint="SendMessageA")]2    private static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam);3 4    [DllImport("user32.dll")]5    private static extern int ReleaseCapture();

 

第二種,通過自定滑鼠左鍵按下時產生的事件:

 * 首先將表單的邊框樣式修改為None,讓表單沒有標題列
 * 實現這個效果使用了三個事件:滑鼠按下、滑鼠彈起、滑鼠移動
 * 滑鼠按下時更改變數isMouseDown標記表單可以隨滑鼠的移動而移動
 * 滑鼠移動時根據滑鼠的移動量更改表單的location屬性,實現表單移動
 * 滑鼠彈起時更改變數isMouseDown標記表單不可以隨滑鼠的移動而移動
 */

1 private bool isMouseDown = false;2 private Point FormLocation;     //form的location3 private Point mouseOffset;      //滑鼠的按下位置

 

1 private void Form1_MouseDown(object sender, MouseEventArgs e)2 {3     if (e.Button == MouseButtons.Left)4     {5         isMouseDown = true;6         FormLocation = this.Location;7         mouseOffset = Control.MousePosition;8     }9 }

 

1 private void Form1_MouseUp(object sender, MouseEventArgs e)2 {3     isMouseDown = false;4 }

 

 1 private void Form1_MouseMove(object sender, MouseEventArgs e) 2 { 3     int _x = 0; 4     int _y = 0; 5     if (isMouseDown) 6     { 7         Point pt = Control.MousePosition; 8         _x = mouseOffset.X - pt.X; 9         _y = mouseOffset.Y - pt.Y;10 11         this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);12     }13 }

 

C#之設定無邊框後如何移動表單(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.