c#桌面程序的窗口美化少不了自己重绘窗口,去掉标题栏自己添加元素是最简单的实现,不过没有了标题栏鼠标拖动窗体活动成了问题,百度搜到两种c#实现重绘的方法。
一、重绘WndProc
1 protected override void WndProc(ref Message m) 2 { 3 if (m.Msg == 0x0201) //鼠标左键按下去的消息 4 { 5 m.Msg = 0x00A1; //更改消息为非客户区按下鼠标 6 m.LParam = IntPtr.Zero; //默认值 7 m.WParam = new IntPtr(2); //鼠标放在标题栏内 8 } 9 base.WndProc(ref m); 10 }
二、调用API函数
1 using System.Runtime.InteropServices; 2 //然后,在程序中声明我们要用到的API函数及要用到的参数常量。 3 4 [DllImport("user32")] 5 private static extern bool ReleaseCapture(); 6 7 [DllImport("user32")] 8 private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 9 10 public const int WM_SYSCOMMAND = 0x0112; 11 public const int SC_MOVE = 0Xf010; 12 public const int HTCAPTION = 0x0002; 13 14 //其中,WM_SYSCOMMAND常量代表要向窗口发送消息,SC_MOVE常代表要向窗口发送移动的消息。 15 //然后,接下来就很简单了。在Form的MouseDown事件中加入以下代码即可: 16 17 ReleaseCapture(); 18 SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION,0);