protected override void WndProc(ref Message m) { switch (m.Msg) { case (int)NativeMethods.WindowMessages.WM_NCPAINT: PaintNonClientArea(m.HWnd, m.WParam); m.Result = NativeMethods.TRUE; return; case (int)NativeMethods.WindowMessages.WM_NCCALCSIZE: if (m.WParam == NativeMethods.FALSE) { //NativeMethods.RECT ncRect = (NativeMethods.RECT)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.RECT)); NativeMethods.RECT ncRect = (NativeMethods.RECT)m.GetLParam(typeof(NativeMethods.RECT)); Rectangle proposed = ncRect.Rect; OnNonClientAreaCalcSize(ref proposed); ncRect = NativeMethods.RECT.FromRectangle(proposed); Marshal.StructureToPtr(ncRect, m.LParam, false); } else if (m.WParam == NativeMethods.TRUE) { //NativeMethods.NCCALCSIZE_PARAMS ncParams = (NativeMethods.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.NCCALCSIZE_PARAMS)); NativeMethods.NCCALCSIZE_PARAMS ncParams = (NativeMethods.NCCALCSIZE_PARAMS)m.GetLParam(typeof(NativeMethods.NCCALCSIZE_PARAMS)); Rectangle proposed = ncParams.rectProposed.Rect; OnNonClientAreaCalcSize(ref proposed); ncParams.rectProposed = NativeMethods.RECT.FromRectangle(proposed); Marshal.StructureToPtr(ncParams, m.LParam, false); } //m.Result = IntPtr.Zero; return; case (int)NativeMethods.WindowMessages.WM_NCHITTEST: Point screenPoint = new Point(m.LParam.ToInt32()); Point clientPoint = PointToWindow(screenPoint); int dragsize = 3; m.Result = IntPtr.Zero; if (this.FormBorderStyle == FormBorderStyle.Sizable || this.FormBorderStyle == FormBorderStyle.SizableToolWindow) { if (clientPoint.X <= dragsize) { if (clientPoint.Y <= dragsize) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTTOPLEFT); } else if (clientPoint.Y >= this.Height - dragsize) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTBOTTOMLEFT); } else { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTLEFT); } } else if (clientPoint.X >= this.Width - dragsize) { if (clientPoint.Y <= dragsize) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTTOPRIGHT); } else if (clientPoint.Y >= this.Height - dragsize) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTBOTTOMRIGHT); } else { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTRIGHT); } } else if (clientPoint.Y <= dragsize) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTTOP); } else if (clientPoint.Y >= this.Height - dragsize) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTBOTTOM); } } else { if ((clientPoint.X <= dragsize || clientPoint.X >= this.Width - dragsize) && (clientPoint.Y <= dragsize || clientPoint.Y >= this.Height - dragsize)) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTBORDER); } } if (m.Result == IntPtr.Zero && clientPoint.Y <= TopBorderHeight) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTCAPTION); } if (m.Result == IntPtr.Zero) { m.Result = new IntPtr((int)NativeMethods.NCHITTEST.HTCLIENT); } return; case (int)NativeMethods.WindowMessages.WM_NCACTIVATE: bool active = (m.WParam == NativeMethods.TRUE); if (WindowState == FormWindowState.Minimized) { DefWndProc(ref m); } else { PaintNonClientArea(m.HWnd, (IntPtr)1); m.Result = NativeMethods.TRUE; } return; case (int)NativeMethods.WindowMessages.WM_SETTEXT: DefWndProc(ref m); PaintNonClientArea(m.HWnd, (IntPtr)1); return; case (int)NativeMethods.WindowMessages.WM_NCMOUSEMOVE: clientPoint = this.PointToWindow(new Point(m.LParam.ToInt32())); OnNonClientMouseMove(new MouseEventArgs(MouseButtons.None, 0, clientPoint.X, clientPoint.Y, 0)); m.Result = IntPtr.Zero; return; case (int)NativeMethods.WindowMessages.WM_NCMOUSELEAVE: OnNonClientMouseLeave(EventArgs.Empty); return; case (int)NativeMethods.WindowMessages.WM_NCLBUTTONDOWN: Point pt = this.PointToWindow(new Point(m.LParam.ToInt32())); NonClientMouseEventArgs args = new NonClientMouseEventArgs(MouseButtons.Left, 1, pt.X, pt.Y, 0, m.WParam.ToInt32()); OnNonClientMouseDown(args); if (!args.Handled) { DefWndProc(ref m); } m.Result = NativeMethods.TRUE; return; case (int)NativeMethods.WindowMessages.WM_SYSCOMMAND: int sc = m.WParam.ToInt32(); if (sc == (int)NativeMethods.SystemCommands.SC_MAXIMIZE || sc == (int)NativeMethods.SystemCommands.SC_MAXIMIZE2) { tmpclientsize = this.ClientSize; } else if (sc == (int)NativeMethods.SystemCommands.SC_MINIMIZE) { tmpclientsize = this.ClientSize; } else if (sc == (int)NativeMethods.SystemCommands.SC_RESTORE || sc == (int)NativeMethods.SystemCommands.SC_RESTORE2) { this.ClientSize = tmpclientsize; } DefWndProc(ref m); return; case 174: return; } base.WndProc(ref m); }
protected virtual void OnNonClientMouseDown(NonClientMouseEventArgs args) { }