C#WinForm無邊框表單移動方法、模仿按一下滑鼠標題列移動表單位置

來源:互聯網
上載者:User

標籤:滑鼠   圖片   tcap   ice   無邊框   blog   工程檔案   lan   表單移動   

C#WinForm無邊框表單移動方法、模仿按一下滑鼠標題列移動表單位置

這裡介紹倆種辦法

方法一:直接通過修改表單位置從而達到移動表單的效果

方法二:直接偽裝發送單擊工作列訊息,讓應用程式誤以為單擊工作列從而移動表單

建立表單用於測試

方法一1.定義一個位置資訊Point用於儲存滑鼠位置
1         private Point mPoint;
2.給表單等控制項增加MouseDown和MouseMove事件
 1         /// <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         }

 

方法二:1.引入下面代碼 前提需要引入命名空間 using System.Runtime.InteropServices
1         [DllImport("user32.dll")]2         public static extern bool ReleaseCapture();3 4         [DllImport("user32.dll")]5         public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);6 7         private const int VM_NCLBUTTONDOWN = 0XA1;//定義滑鼠左鍵按下8         private const int HTCAPTION = 2;
2.增加滑鼠按下事件發送訊息,讓系統誤以為按下是標題列
 1         /// <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             //為當前應用程式釋放滑鼠捕獲 9             ReleaseCapture();10             //發送訊息 讓系統誤以為在標題列上按下滑鼠11             SendMessage((IntPtr)this.Handle, VM_NCLBUTTONDOWN, HTCAPTION, 0);12         }
測試效果

 

 原始碼工程檔案下載

C#WinForm無邊框表單移動方法、模仿按一下滑鼠標題列移動表單位置

聯繫我們

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