郵箱地址怎么注冊(cè)seo優(yōu)化seo外包
Winform無邊框窗體拖動(dòng)功能
- 前言
- 正文
- 1、設(shè)置無邊框模式
- 2、無邊框窗體拖動(dòng)方法
- 1、通過Panel控件實(shí)現(xiàn)窗體移動(dòng)
- 2、通過窗體事件實(shí)現(xiàn)窗體移動(dòng)
- 3、調(diào)用系統(tǒng)API實(shí)現(xiàn)窗體移動(dòng)
- 4、重寫WndProc()實(shí)現(xiàn)窗體移動(dòng)
前言
在本文中主要介紹 如何將窗體設(shè)置成無邊框模式、以及實(shí)現(xiàn)無邊框窗體拖動(dòng)功能的幾種方法。
正文
1、設(shè)置無邊框模式
選中要去除邊框的窗體,按F4調(diào)出其屬性面板,在屬性面板中找到 FormBorderStyle ,并選擇 None,即可將窗體設(shè)置成無邊框模式;默認(rèn)是無法隨意拖動(dòng)的,也沒有最大化最小化關(guān)閉按鈕。
2、無邊框窗體拖動(dòng)方法
1、通過Panel控件實(shí)現(xiàn)窗體移動(dòng)
-
實(shí)現(xiàn)方法:在窗體的頭部添加 Panel 控件,通過 Panel 的 MouseDown、MouseMove 事件實(shí)現(xiàn)窗體移動(dòng);
-
實(shí)現(xiàn)原理:直接通過修改窗體位置從而達(dá)到移動(dòng)窗體的效果;
-
具體代碼:
private Point mPoint;//定義一個(gè)位置信息Point用于存儲(chǔ)鼠標(biāo)位置/// <summary>/// 鼠標(biāo)按下/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void uPanel1_MouseDown(object sender, MouseEventArgs e){mPoint = new Point(e.X, e.Y);}/// <summary>/// 鼠標(biāo)移動(dòng)/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void uPanel1_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);}}
-
實(shí)現(xiàn)效果:
2、通過窗體事件實(shí)現(xiàn)窗體移動(dòng)
-
實(shí)現(xiàn)方法:通過窗體MouseDown、MouseMove、MouseUp事件實(shí)現(xiàn)窗體移動(dòng);
-
具體代碼:
//通過窗體MouseDown、MouseMove、MouseUp事件實(shí)現(xiàn)窗體移動(dòng)Point point; //鼠標(biāo)按下時(shí)的點(diǎn)bool isMoving = false;//標(biāo)識(shí)是否拖動(dòng)private void Form1_MouseDown(object sender, MouseEventArgs e){point = e.Location;//按下的點(diǎn)isMoving = true;//啟動(dòng)拖動(dòng)}private void Form1_MouseMove(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left && isMoving){Point pNew = new Point(e.Location.X - point.X, e.Location.Y - point.Y);//Location = new Point(Location.X + pNew.X, Location.Y + pNew.Y);Location += new Size(pNew);}}private void Form1_MouseUp(object sender, MouseEventArgs e){isMoving = false;//停止}
-
實(shí)現(xiàn)效果:
3、調(diào)用系統(tǒng)API實(shí)現(xiàn)窗體移動(dòng)
-
實(shí)現(xiàn)方法:利用 windows 應(yīng)用程序接口 Windows API 實(shí)現(xiàn)窗體移動(dòng);
-
實(shí)現(xiàn)原理:當(dāng)鼠標(biāo)左鍵按下時(shí),讓系統(tǒng)認(rèn)為是在標(biāo)題欄按下的;
-
具體代碼:
using System.Runtime.InteropServices;//調(diào)用系統(tǒng)API[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;//定義鼠標(biāo)左鍵按下public const int HTCAPTION = 0x0002; //HTCAPTION=2 鼠標(biāo)在標(biāo)題欄中/// <summary>/// 鼠標(biāo)按下事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Form1_MouseDown(object sender, MouseEventArgs e){//為當(dāng)前應(yīng)用程序釋放鼠標(biāo)捕獲ReleaseCapture();//發(fā)送消息 讓系統(tǒng)誤以為在標(biāo)題欄上按下鼠標(biāo)SendMessage((IntPtr)this.Handle, VM_NCLBUTTONDOWN, HTCAPTION, 0);}
-
注意事項(xiàng):需要引入命名空間
using System.Runtime.InteropServices;
-
實(shí)現(xiàn)效果:
4、重寫WndProc()實(shí)現(xiàn)窗體移動(dòng)
-
實(shí)現(xiàn)方法:通過重寫 WndProc() 方法實(shí)現(xiàn)窗體移動(dòng);
-
實(shí)現(xiàn)原理:將鼠標(biāo)在客戶區(qū)按下的消息更改為在非客戶區(qū)的標(biāo)題欄按下;
-
具體代碼:
//重寫WndProc://原理:將鼠標(biāo)在客戶區(qū)按下的消息更改為在非客戶區(qū)的標(biāo)題欄按下protected override void WndProc(ref Message m){switch (m.Msg){case 0x0201://鼠標(biāo)左鍵按下的消息m.Msg = 0x00A1;//更改消息為非客戶區(qū)按下鼠標(biāo)m.LParam = IntPtr.Zero;//默認(rèn)值m.WParam = new IntPtr(2);//鼠標(biāo)放在標(biāo)題欄內(nèi)break;}base.WndProc(ref m);}
-
實(shí)現(xiàn)效果: