protected override void WndProc(ref Message m) { switch (m.Msg) { case (int)NativeMethods.WindowMessages.WM_NCHITTEST: WmNcHitTest(ref m); break; case (int)NativeMethods.WindowMessages.WM_NCPAINT: case (int)NativeMethods.WindowMessages.WM_NCCALCSIZE: break; case (int)NativeMethods.WindowMessages.WM_WINDOWPOSCHANGED: _inPosChanged = true; base.WndProc(ref m); _inPosChanged = false; break; case (int)NativeMethods.WindowMessages.WM_GETMINMAXINFO: WmGetMinMaxInfo(ref m); break; case (int)NativeMethods.WindowMessages.WM_NCACTIVATE: WmNcActive(ref m); SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW); //API函数加载,实现窗体边框阴影效果 break; case (int)NativeMethods.WindowMessages.WM_SYSCOMMAND: int wparam = m.LParam.ToInt32(); Point point = new Point( NativeMethods.LOWORD(wparam), NativeMethods.HIWORD(wparam)); point = PointToClient(point); if (IconRect.Contains(point)) { m.WParam = IntPtr.Zero; if (IconClick != null) { IconClick(this, EventArgs.Empty); } return; } base.WndProc(ref m); break; default: base.WndProc(ref m); break; } }
private void DrawIcon(Graphics graphics, Rectangle rectangle3) { IconRect.Inflate(-1, -1); if (iconImage == null) { using (Pen pen = new Pen(SystemColors.ControlDark)) { pen.DashStyle = DashStyle.Dash; graphics.DrawRectangle(pen, rectangle3); } } else { graphics.DrawImage(iconImage, rectangle3); } }
private void WmNcHitTest(ref Message m) { int wparam = m.LParam.ToInt32(); Point point = new Point( NativeMethods.LOWORD(wparam), NativeMethods.HIWORD(wparam)); point = PointToClient(point); if (IconRect.Contains(point)) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTSYSMENU); return; } if (_canResize) { if (point.X < 5 && point.Y < 5) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTTOPLEFT); return; } if (point.X > Width - 5 && point.Y < 5) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTTOPRIGHT); return; } if (point.X < 5 && point.Y > Height - 5) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTBOTTOMLEFT); return; } if (point.X > Width - 5 && point.Y > Height - 5) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTBOTTOMRIGHT); return; } if (point.Y < 3) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTTOP); return; } if (point.Y > Height - 3) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTBOTTOM); return; } if (point.X < 3) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTLEFT); return; } if (point.X > Width - 3) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTRIGHT); return; } } if (point.Y < CaptionHeight) { if (!ControlBoxManager.CloseBoxRect.Contains(point) && !ControlBoxManager.MaximizeBoxRect.Contains(point) && !ControlBoxManager.MinimizeBoxRect.Contains(point) && !ControlBoxManager.SysBottomRect.Contains(point)) { m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTCAPTION); return; } } m.Result = new IntPtr( (int)NativeMethods.NCHITTEST.HTCLIENT); }
private bool WmNcHitTest(ref Message m) { int para = m.LParam.ToInt32(); int x0 = WinAPI.LOWORD(para); int y0 = WinAPI.HIWORD(para); Point p = PointToClient(new Point(x0, y0)); if (Resizable) { if (TopLeftRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTTOPLEFT); return(true); } if (TopRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTTOP); return(true); } if (TopRightRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTTOPRIGHT); return(true); } if (LeftRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTLEFT); return(true); } if (RightRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTRIGHT); return(true); } if (BottomLeftRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTBOTTOMLEFT); return(true); } if (BottomRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTBOTTOM); return(true); } if (BottomRightRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTBOTTOMRIGHT); return(true); } } if (IconRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTSYSMENU); return(true); } if (CloseBoxRect.Contains(p) || MaxBoxRect.Contains(p) || MinBoxRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTCLIENT); return(true); } if (IsMdiContainer && _mdiBarController != null) { if (_mdiBarController.HitTestBounds.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTCLIENT); return(true); } } if (CaptionRect.Contains(p)) { m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTCAPTION); return(true); } m.Result = new IntPtr((int)WinAPI.NCHITTEST.HTCLIENT); return(true); }