1)去掉窗體灰色邊框:
在設計窗口中,將Form的FormBorderStyle設置為 FixedSingle 。但是設置后窗口無法實現拖動邊框改變大小。需要在Form.cs加入以下代碼:
private const int WM_NCLBUTTONDBLCLK = 163; private const int WM_NCHITTEST = 132; /// <summary> /// 邊框改變大小 /// </summary> /// <param name="m"></param> protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_NCLBUTTONDBLCLK://WM_NCLBUTTONDBLCLK=163 <0xA3>攔截鼠標非客戶區左鍵雙擊消息,決定窗體是否最大化顯示 if (this.MaximizeBox) { base.WndProc(ref m); this.Invalidate(); } return; case WM_NCHITTEST://WM_NCHITTEST=132 <0x84> base.WndProc(ref m);//如果去掉這一行代碼,窗體將失去MouseMove..等事件 //Point lpint = new Point((int)m.LParam);//可以得到鼠標坐標,這樣就可以決定怎么處理這個消息了,是移動窗體,還是縮放,以及向哪向的縮放 //if (lpint.Y < 30) // m.Result = (IntPtr)0x2;//托動HTCAPTION=2 <0x2> if (WindowState != FormWindowState.Maximized) { Point p2 = this.PointToClient(MousePosition);//鼠標相對於窗體的坐標 //當然可以托動也可以改變大小了 //label1.Text = p2.X + "," + p2.Y; //HTLEFT=10 <0xA> 左邊框 if (p2.X < 5 && p2.Y > 5 && p2.Y < this.Height - 5) m.Result = (IntPtr)0xA; else if (p2.Y < 5 && p2.X > 5 && p2.X < this.Width - 5) m.Result = (IntPtr)0xC; //HTTOP=12 <0xC> 上邊框 else if (p2.X < 5 && p2.Y < 5) m.Result = (IntPtr)0xD; //HTTOPLEFT=13 <0xD> else if (p2.X >= this.Width - 5 && p2.Y < 5) m.Result = (IntPtr)0xE; //HTTOPRIGHT=14 <0xE> else if (p2.X > this.Width - 5 && p2.Y > 5 && p2.Y < this.Height - 5) m.Result = (IntPtr)0xB; //HTRIGHT=11 <0xB> else if (p2.Y >= this.Height - 5 && p2.X > 5 && p2.X < this.Width - 5) m.Result = (IntPtr)0xF; //HTBOTTOM=15 <0xF> else if (p2.X < 5 && p2.Y >= this.Height - 5) m.Result = (IntPtr)0x10; //HTBOTTOMLEFT=16 <0x10> else if (p2.X > this.Width - 5 && p2.Y >= this.Height - 5) m.Result = (IntPtr)0x11; //HTBOTTOMRIGHT=17 <0x11> //HTBORDER=18 <0x12> //HTMINBUTTON=8 <0x8> 最小化按鈕 //HTMAXBUTTON=9 <0x9> 最大化按鈕 //HTCLOSE=20 <0x14> 關閉按鈕 } return; default: base.WndProc(ref m); return; } }