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; } }