protected override void DefWndProc(ref Message m) { switch (m.Msg) { case WM_COPYDATA: COPYDATASTRUCT myStr = new COPYDATASTRUCT(); Type myType = myStr.GetType(); myStr = (COPYDATASTRUCT)m.GetLParam(myType); //m中获取LParam参数以myType类型的方式,让后转换问结构体。 if (myStr.lpData != null) { MainUrl = myStr.lpData; changeImage(myStr.lpData); } break; case 0x0083: if (Convert.ToBoolean(m.WParam.ToInt32())) { NCCALCSIZE_PARAMS t = (NCCALCSIZE_PARAMS)m.GetLParam(typeof(NCCALCSIZE_PARAMS)); int CaptionHeight = GetSystemMetrics(4); int Border3DWidth = System.Windows.Forms.SystemInformation.Border3DSize.Width; //这里使用FW或者API都可以取到窗体的相关属性 int BorderWidth = System.Windows.Forms.SystemInformation.BorderSize.Width; //尝试过了,只有修改RECT[0]才有效果撒… Graphics currentGraphics = Graphics.FromHwnd(this.Handle); double dpixRatio = currentGraphics.DpiX / 96; //获取屏幕DPI放大倍数 int x = t.rect0.top - this.Height; t.rect0.top = t.rect0.top - (CaptionHeight) - (int)((double)7 * dpixRatio) - 1; Marshal.StructureToPtr(t, m.LParam, false); // 结构体转指针 // base.WndProc(ref m); //t.rect0.top = t.rect0.top - CaptionHeight - Border3DWidth - BorderWidth - 5; //t.rect0.top = t.rect0.top - 35; //t.rect0.left = t.rect0.left ; //t.rect0.right = t.rect0.right; //59d8d69e44858dad3d90875bb8d5e097 //t.rect0.bottom = t.rect0.bottom ; } break; default: break; } base.DefWndProc(ref m); }
protected override void WndProc(ref Message m) { if (!VisualStyleRenderer.IsSupported || !_aeroEnabled) { base.WndProc(ref m); return; } int WM_NCCALCSIZE = 0x83; IntPtr result; int dwmHandled = DwmDefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam, out result); if (dwmHandled == 1) { m.Result = result; return; } if (m.Msg == WM_NCCALCSIZE && (int)m.WParam == 1) { NCCALCSIZE_PARAMS nccsp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); // Adjust (shrink) the client rectangle to accommodate the border: nccsp.rect0.Top += 0; nccsp.rect0.Bottom += 0; nccsp.rect0.Left += 0; nccsp.rect0.Right += 0; if (!_setMargins) { //Set what client area would be for passing to DwmExtendIntoClientArea _dwmMargins.cyTopHeight = nccsp.rect2.Top - nccsp.rect1.Top; _dwmMargins.cxLeftWidth = nccsp.rect2.Left - nccsp.rect1.Left; _dwmMargins.cyBottomHeight = _buttonSize.Height + 2; _dwmMargins.cxRightWidth = nccsp.rect1.Right - nccsp.rect2.Right; _setMargins = true; } Marshal.StructureToPtr(nccsp, m.LParam, false); m.Result = IntPtr.Zero; } else { base.WndProc(ref m); } }
/// <summary> /// WNDs the proc. /// </summary> /// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param> protected override void WndProc(ref Message m) { int WM_NCCALCSIZE = 0x83; int WM_NCHITTEST = 0x84; IntPtr result = default(IntPtr); int dwmHandled = Window.DwmDefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam, out result); if (dwmHandled == 1) { m.Result = result; return; } if (m.Msg == WM_NCCALCSIZE && m.WParam.ToInt32() == 1) { NCCALCSIZE_PARAMS nccsp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); // Adjust (shrink) the client rectangle to accommodate the border: nccsp.rect0.Top += 0; nccsp.rect0.Bottom += 0; nccsp.rect0.Left += 0; nccsp.rect0.Right += 0; if (!_marginOk) { //Set what client area would be for passing to DwmExtendIntoClientArea. //Also remember that at least one of these values NEEDS TO BE > 1, else //it won't work. _dwmMargins.cyTopHeight = 0; _dwmMargins.cxLeftWidth = 0; _dwmMargins.cyBottomHeight = 3; _dwmMargins.cxRightWidth = 0; _marginOk = true; } Marshal.StructureToPtr(nccsp, m.LParam, false); m.Result = IntPtr.Zero; } else if (m.Msg == WM_NCHITTEST && m.Result.ToInt32() == 0) { m.Result = HitTestNCA(m.HWnd, m.WParam, m.LParam); } else { base.WndProc(ref m); } }
protected override void WndProc(ref System.Windows.Forms.Message m) { const int WM_NCPAINT = 0x85; switch (m.Msg) { case WM_NCPAINT: base.WndProc(ref m); if (_rectangeUpdated) { IntPtr hdc = GetWindowDC(m.HWnd); if ((int)hdc != 0) { Graphics g = Graphics.FromHdc(hdc); g.DrawImage(Properties.Resources.Welcome_Scan, _rectange); g.DrawLine(new Pen(Color.Blue), new Point(1, 1), new Point(50, 50)); g.Flush(); ReleaseDC(m.HWnd, hdc); _rectangeUpdated = false; } } break; case WM_NCCALCSIZE: { base.WndProc(ref m); if (m.WParam != IntPtr.Zero && _titleHight != 0) { if (m.HWnd == this.Handle) { NCCALCSIZE_PARAMS rcsize = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); _rectange = new Rectangle(_borderWidth, _titleHight, rcsize.rcNewWindow.right - rcsize.rcNewWindow.left, rcsize.rcNewWindow.bottom - rcsize.rcNewWindow.top); _rectangeUpdated = true; } } m.Result = new IntPtr(1); } break; default: base.WndProc(ref m); break; } }
public static void ChangeClientRectalgle(ref Message message, Padding padding) { if (message.Msg == WindowMessage.WM_NCCALCSIZE) { if (message.WParam != IntPtr.Zero) { NCCALCSIZE_PARAMS rcsize = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(message.LParam, typeof(NCCALCSIZE_PARAMS)); AdjustClientRect(ref rcsize.rcNewWindow, padding); Marshal.StructureToPtr(rcsize, message.LParam, false); } else { RECT rcsize = (RECT)Marshal.PtrToStructure(message.LParam, typeof(RECT)); AdjustClientRect(ref rcsize, padding); Marshal.StructureToPtr(rcsize, message.LParam, false); } message.Result = new IntPtr(1); } }
static void HandleNcCalcSize(IntPtr hwnd, IntPtr wParam, IntPtr lParam) { if (wParam == BasicValues.TRUE) { var wpl = default(WINDOWPLACEMENT); wpl.length = (uint)Marshal.SizeOf(typeof(WINDOWPLACEMENT)); if (User32.GetWindowPlacement(hwnd, ref wpl)) { // detect if maximizd and set to workspace remove padding if (wpl.showCmd == ShowWindowOption.SW_MAXIMIZE) { // in multi-monitor case where app is minimized to a monitor on the right/bottom // the MonitorFromWindow will incorrectly return the leftmost monitor due to the minimized // window being set to the far left, so this routine now uses the proposed rect to correctly // identify the real nearest monitor to calc the nc size. NCCALCSIZE_PARAMS para = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(lParam, typeof(NCCALCSIZE_PARAMS)); var windowRect = para.rectProposed; IntPtr hMonitor = User32.MonitorFromRect(ref windowRect, MonitorOption.MONITOR_DEFAULTTONEAREST);// MonitorFromWindow(hWnd, 2); if (hMonitor != IntPtr.Zero) { MONITORINFO lpmi = new MONITORINFO(); lpmi.cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFO)); if (User32.GetMonitorInfo(hMonitor, ref lpmi)) { var workArea = lpmi.rcWork; User32Ex.AdjustForAutoHideTaskbar(hMonitor, ref workArea); Debug.WriteLine("NCCalc original = {0}x{1} @ {2}x{3}, new ={4}x{5} @ {6}x{7}", para.rectProposed.Width, para.rectProposed.Height, para.rectProposed.left, para.rectProposed.top, workArea.Width, workArea.Height, workArea.left, workArea.top); para.rectProposed = workArea; Marshal.StructureToPtr(para, lParam, true); } } } } } }
protected virtual void WmNcCalcSize(ref Message m) { if (m.WParam != IntPtr.Zero) { NCCALCSIZE_PARAMS ncsize = (NCCALCSIZE_PARAMS)m.GetLParam( typeof(NCCALCSIZE_PARAMS)); ncsize.rgrc0.Left += _borderWidth; ncsize.rgrc0.Right -= _borderWidth; ncsize.rgrc0.Top += _borderWidth; ncsize.rgrc0.Bottom -= _borderWidth; Marshal.StructureToPtr(ncsize, m.LParam, false); } else { RECT rc = (RECT)m.GetLParam(typeof(RECT)); rc.Left += _borderWidth; rc.Right -= _borderWidth; rc.Top += _borderWidth; rc.Bottom -= _borderWidth; Marshal.StructureToPtr(rc, m.LParam, true); } m.Result = Result.FALSE; }
protected virtual void OnWmNcCalcSize(ref Message m, NuGenWndProcDelegate baseWndProc) { Debug.Assert(m != null, "m != null"); Debug.Assert(baseWndProc != null, "baseWndProc != null"); Debug.Assert(User32.IsWindow(m.HWnd), "User32.IsWindow(m.HWnd)"); baseWndProc(ref m); if (m.WParam == Common.FALSE) { RECT rect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); Marshal.StructureToPtr( (RECT)this.GetClientRectangle(m.HWnd, (Rectangle)rect), m.LParam, false ); m.Result = Common.FALSE; } else { NCCALCSIZE_PARAMS calcSizeParams = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); WINDOWPOS wndPos = (WINDOWPOS)Marshal.PtrToStructure(calcSizeParams.lppos, typeof(WINDOWPOS)); RECT bufferRect = (RECT)this.GetClientRectangle( m.HWnd, new Rectangle(wndPos.x, wndPos.y, wndPos.width, wndPos.height) ); calcSizeParams.rectProposed = bufferRect; calcSizeParams.rectBeforeMove = bufferRect; Marshal.StructureToPtr(calcSizeParams, m.LParam, false); m.Result = new IntPtr(WinUser.WVR_VALIDRECTS); } }
/// <summary> /// Calculates the size of the window frame and client area of the RichTextBox /// </summary> private void WmNcCalcSize(ref Message m) { // let the richtextbox control draw the scrollbar if necessary. base.WndProc(ref m); // we visual styles are not enabled and BorderStyle is not Fixed3D then we have nothing more to do. if (!RenderWithVisualStyles()) { return; } // contains detailed information about WM_NCCALCSIZE message NCCALCSIZE_PARAMS par = new NCCALCSIZE_PARAMS(); // contains the window frame RECT RECT windowRect; if (m.WParam == IntPtr.Zero) // LParam points to a RECT struct { windowRect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); } else // LParam points to a NCCALCSIZE_PARAMS struct { par = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); windowRect = par.rgrc0; } // contains the client area of the control RECT contentRect; // get the DC IntPtr hDC = GetWindowDC(Handle); // open theme data IntPtr hTheme = OpenThemeData(Handle, "EDIT"); // find out how much space the borders needs if (GetThemeBackgroundContentRect(hTheme, hDC, EP_EDITTEXT, GetState() , ref windowRect , out contentRect) == S_OK) { // shrink the client area the make more space for containing text. contentRect.Left += 1 + Padding.Left; contentRect.Top += 1 + Padding.Top; contentRect.Right -= 1 + Padding.Right; contentRect.Bottom -= 1 + Padding.Bottom; // remember the space of the borders _borderRect = new RECT(contentRect.Left - windowRect.Left , contentRect.Top - windowRect.Top , windowRect.Right - contentRect.Right , windowRect.Bottom - contentRect.Bottom); // update LParam of the message with the new client area if (m.WParam == IntPtr.Zero) { Marshal.StructureToPtr(contentRect, m.LParam, false); } else { par.rgrc0 = contentRect; Marshal.StructureToPtr(par, m.LParam, false); } // force the control to redraw it´s client area m.Result = new IntPtr(WVR_REDRAW); } // release theme data handle CloseThemeData(hTheme); // release DC ReleaseDC(Handle, hDC); }
protected void CustomProc(ref Message m) { switch (m.Msg) { case WM_PAINT: { PAINTSTRUCT ps = new PAINTSTRUCT(); if (!_bPainting) { _bPainting = true; BeginPaint(m.HWnd, ref ps); PaintThis(ps.hdc, ps.rcPaint); EndPaint(m.HWnd, ref ps); _bPainting = false; base.WndProc(ref m); } else { base.WndProc(ref m); } break; } case WM_CREATE: { GetFrameSize(); FrameChanged(); m.Result = MSG_HANDLED; base.WndProc(ref m); break; } case WM_NCCALCSIZE: { if (m.WParam != IntPtr.Zero && m.Result == IntPtr.Zero) { if (_bExtendIntoFrame) { NCCALCSIZE_PARAMS nc = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); nc.rect0.Top -= (_tMargins.cyTopHeight > CaptionHeight ? CaptionHeight : _tMargins.cyTopHeight); nc.rect1 = nc.rect0; Marshal.StructureToPtr(nc, m.LParam, false); m.Result = (IntPtr)WVR_VALIDRECTS; } base.WndProc(ref m); } else { base.WndProc(ref m); } break; } case WM_SYSCOMMAND: { UInt32 param; if (IntPtr.Size == 4) { param = (UInt32)(m.WParam.ToInt32()); } else { param = (UInt32)(m.WParam.ToInt64()); } if ((param & 0xFFF0) == SC_RESTORE) { this.Height = _iStoreHeight; } else if (this.WindowState == FormWindowState.Normal) { _iStoreHeight = this.Height; } base.WndProc(ref m); break; } case WM_NCHITTEST: { if (m.Result == (IntPtr)HIT_CONSTANTS.HTNOWHERE) { IntPtr res = IntPtr.Zero; if (DwmDefWindowProc(m.HWnd, (uint)m.Msg, m.WParam, m.LParam, ref res)) { m.Result = res; } else { m.Result = (IntPtr)HitTest(); } } else { base.WndProc(ref m); } break; } case WM_DWMCOMPOSITIONCHANGED: case WM_ACTIVATE: { DwmExtendFrameIntoClientArea(this.Handle, ref _tMargins); m.Result = MSG_HANDLED; base.WndProc(ref m); break; } default: { base.WndProc(ref m); break; } } }
protected override void WndProc(ref Message m) { if (!isEnabled || IsDisposed) { base.WndProc(ref m); return; } var msg = m.Msg; switch (msg) { case (int)WindowsMessages.WM_NCACTIVATE: if (m.WParam == Win32DataUtils.FALSE) { m.Result = MESSAGE_HANDLED; } User32.InvalidateWindow(parentWindowHWnd); break; case (int)WindowsMessages.WM_ACTIVATEAPP: case (int)WindowsMessages.WM_MOVE: base.WndProc(ref m); User32.InvalidateWindow(parentWindowHWnd); break; case (int)WindowsMessages.WM_SIZE: if (m.WParam == (IntPtr)0 && isMaximized) { isMaximized = false; isMinimized = false; //fix: In Win7 or higher, drag a window maximized will casue ncpaint error, so force window to calculate size of the nonclient area. //fix: 在Win7系统下面如果窗体最大化,拖动窗体会造成非客户区绘制错误,因为没有触发WM_NCCALCSIZE,所以强制触发这个消息来使非客户去重新计算大小。 User32.SendFrameChanged(parentWindowHWnd); } if (m.WParam == (IntPtr)2) { isMaximized = true; isMinimized = false; User32.SendFrameChanged(parentWindowHWnd); } //User32.InvalidateWindow(parentWindowHWnd); base.WndProc(ref m); break; case (int)WindowsMessages.WM_NCLBUTTONDOWN: if (m.WParam == (IntPtr)2 && isMaximized) { User32.SendFrameChanged(parentWindowHWnd); } base.WndProc(ref m); break; case (int)WindowsMessages.WM_SYSCOMMAND: if (m.WParam == (IntPtr)SystemCommandFlags.SC_MAXIMIZE) { isMaximized = true; isMinimized = false; } else if (m.WParam == (IntPtr)SystemCommandFlags.SC_MINIMIZE) { isMinimized = true; } else if (m.WParam == (IntPtr)SystemCommandFlags.SC_RESTORE) { if (isMinimized) { isMinimized = false; } else { isMaximized = false; } } base.WndProc(ref m); break; case (int)WindowsMessages.WM_NCCALCSIZE: if (m.WParam != FALSE) { NCCALCSIZE_PARAMS ncsize = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(ncsize.lppos, typeof(WINDOWPOS)); if (!isFrameSizeStored) { isFrameSizeStored = true; ncCaptionHeight = ncsize.rectClientBeforeMove.top - ncsize.rectProposed.top;; ncFrameWidth = ncsize.rectClientBeforeMove.left - ncsize.rectProposed.left; ncFrameHeight = ncsize.rectBeforeMove.bottom - ncsize.rectClientBeforeMove.bottom; } RECT rc = ncsize.rectProposed; ncsize.rectProposed = CalculateFrameSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); ncsize.rectBeforeMove = ncsize.rectProposed; Marshal.StructureToPtr(ncsize, m.LParam, false); m.Result = WVR_VALIDRECTS; User32.InvalidateWindow(parentWindowHWnd); } else { RECT rc = (RECT)m.GetLParam(typeof(RECT)); rc = CalculateFrameSize(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); Marshal.StructureToPtr(rc, m.LParam, true); m.Result = MESSAGE_PROCESS; } base.WndProc(ref m); break; case (int)WindowsMessages.WM_NCPAINT: if (User32.IsWindowVisible(parentWindowHWnd)) { m.Result = MESSAGE_HANDLED; DrawNCArea(m.WParam); } break; case (int)WindowsMessages.WM_NCUAHDRAWCAPTION: case (int)WindowsMessages.WM_NCUAHDRAWFRAME: User32.InvalidateWindow(parentWindowHWnd); break; default: base.WndProc(ref m); break; } }
private IntPtr GlowWindowHost_WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { //Prepare RECT rect, clnRect; //Handle message switch ((WindowMessages)msg) { case WindowMessages.WM_MOVE: if (User32.GetWindowRect(hwnd, out rect)) { //Set position LeftGlow_SizePos(leftHandle.Handle, rect); TopGlow_SizePos(topHandle.Handle, rect); RightGlow_SizePos(rightHandle.Handle, rect); BottomGlow_SizePos(bottomHandle.Handle, rect); } break; case WindowMessages.WM_SIZE: if (User32.GetWindowRect(hwnd, out rect)) { //Set visibility int wmSizeWParam = wParam.ToInt32(); switch (wParam.ToInt32()) { case 1: case 2: BordersVisible = false; break; case 0: BordersVisible = true; break; } //Set size LeftGlow_SizePos(leftHandle.Handle, rect); TopGlow_SizePos(topHandle.Handle, rect); RightGlow_SizePos(rightHandle.Handle, rect); BottomGlow_SizePos(bottomHandle.Handle, rect); //Check if (wmSizeWParam != this.wmSizeWParam) { this.wmSizeWParam = wmSizeWParam; Point sz = new Point(lParam.ToInt32()); User32.SetWindowPos(hwnd, IntPtr.Zero, 0, 0, sz.X, sz.Y, 0x0020 | 0x0002); } } break; case WindowMessages.WM_NCCALCSIZE: handled = true; if (wParam != IntPtr.Zero) //check if wParam is TRUE { //Copy NCCALCSIZE_PARAMS from memory NCCALCSIZE_PARAMS nccsp = Marshal.PtrToStructure <NCCALCSIZE_PARAMS>(lParam); //Request client rectangle clnRect = GlowWindowHost_RequestClientRectangle(nccsp.rgrc0); nccsp.rgrc0 = clnRect; //Copy NCCALCSIZE_PARAMS into memory Marshal.StructureToPtr(nccsp, lParam, true); } else { //Get window rect from lParam rect = Marshal.PtrToStructure <RECT>(lParam); //Request client rectangle clnRect = GlowWindowHost_RequestClientRectangle(rect); //Copy RECT into memory Marshal.StructureToPtr(clnRect, lParam, true); } //Return return(IntPtr.Zero); case WindowMessages.WM_NCHITTEST: Point htPoint = new Point(lParam.ToInt32()); if (User32.GetWindowRect(hwnd, out RECT wndRect)) { //Hit-test DpiScale dpi = VisualTreeHelper.GetDpi(this); HitTestResult result = VisualTreeHelper.HitTest(this, new WpfPoint( (htPoint.X - wndRect.Left) * dpi.DpiScaleX, (htPoint.Y - wndRect.Top) * dpi.DpiScaleY)); if (result?.VisualHit is UIElement element) { if (element.GetValue(NonClientActionProperty) is NonClientHitAction action) { //Set handled = true; //Handle action switch (action) { case NonClientHitAction.Caption: return((IntPtr)2); case NonClientHitAction.Icon: return((IntPtr)3); case NonClientHitAction.Top: return((IntPtr)12); case NonClientHitAction.Left: return((IntPtr)10); case NonClientHitAction.Right: return((IntPtr)11); case NonClientHitAction.Bottom: return((IntPtr)15); case NonClientHitAction.TopLeft: return((IntPtr)13); case NonClientHitAction.TopRight: return((IntPtr)14); case NonClientHitAction.BottomLeft: return((IntPtr)16); case NonClientHitAction.BottomRight: return((IntPtr)17); default: return((IntPtr)1); } } } } break; case WindowMessages.WM_ACTIVATE: bool isActive = true; bool isGlowWindowActive = (lParam == leftHandle.Handle || lParam == topHandle.Handle || lParam == rightHandle.Handle || lParam == bottomHandle.Handle); if (wParam == IntPtr.Zero && !isGlowWindowActive) { isActive = false; } //Check if (isActive != this.isActive) { WpfColor color = new WpfColor(); if (isActive) { color = ActiveGlowColor; } else { color = InactiveGlowColor; } //Set icon CurrentIcon = isActive ? ActiveIcon : InactiveIcon; //Render glowTextures.Render(Color.FromArgb(color.R, color.G, color.B)); //Re-draw edges GlowWindow_DrawLayeredWindow(leftHandle.Handle); GlowWindow_DrawLayeredWindow(topHandle.Handle); GlowWindow_DrawLayeredWindow(rightHandle.Handle); GlowWindow_DrawLayeredWindow(bottomHandle.Handle); //Set this.isActive = isActive; InvalidateVisual(); } break; case WindowMessages.WM_DESTROY: hostDictionary.Remove(hwnd); //Unregister break; } //Return return(IntPtr.Zero); }
protected override void WndProc(ref Message m) { if (DesignMode) { base.WndProc(ref m); return; } if (m.Msg == WM_NCCALCSIZE) { if (m.WParam.ToInt32() == 1) { MARGINS borderless = new MARGINS() { cxLeftWidth = 1, cxRightWidth = 1, cyBottomHeight = 1, cyTopHeight = 1 }; DwmExtendFrameIntoClientArea(Handle, ref borderless); WINDOWPLACEMENT pl = new WINDOWPLACEMENT(); GetWindowPlacement(m.HWnd, ref pl); if (pl.ShowCmd == ShowWindowCommands.Maximize) { NCCALCSIZE_PARAMS p = Marshal.PtrToStructure <NCCALCSIZE_PARAMS>(m.LParam); p.rgrc[0].Left += 7; p.rgrc[0].Right -= 8; p.rgrc[0].Top += 7; p.rgrc[0].Bottom -= 8; Marshal.StructureToPtr(p, m.LParam, true); } } m.Result = IntPtr.Zero; return; } else if (m.Msg == WM_NCHITTEST) { Point mousePos = PointToClient(new Point((m.LParam.ToInt32() & 0xFFFF), (m.LParam.ToInt32() >> 16))); foreach (CaptionButton b in CaptionButtons) { if (mousePos.X >= b.X && mousePos.X < b.X + b.Width && mousePos.Y >= b.Y && mousePos.Y < b.Y + b.Height) { m.Result = new IntPtr(b.HitTestCode); return; } } m.Result = new IntPtr(mousePos.X >= 0 && mousePos.X < Width && mousePos.Y >= 0 && mousePos.Y < Height ? HT_CAPTION : HT_NOWHERE); if (SizeBorder && WindowState == FormWindowState.Normal) { int res = m.Result.ToInt32(); int col = 0; if (mousePos.X >= ClientSize.Width - 5) { col = 2; } else if (mousePos.X > 5) { col = 1; } int row = 0; if (mousePos.Y >= ClientSize.Height - 5) { row = 2; } else if (mousePos.Y > 5) { row = 1; } if (col == 0) { if (row == 0) { res = HT_TOPLEFT; } if (row == 1) { res = HT_LEFT; } if (row == 2) { res = HT_BOTTOMLEFT; } } if (col == 1) { if (row == 0) { res = HT_TOP; } if (row == 2) { res = HT_BOTTOM; } } if (col == 2) { if (row == 0) { res = HT_TOPRIGHT; } if (row == 1) { res = HT_RIGHT; } if (row == 2) { res = HT_BOTTOMRIGHT; } } m.Result = new IntPtr(res); } return; } base.WndProc(ref m); }
/// <summary> /// </summary> protected override void WndProc(ref Message m) { switch (m.Msg) { case WinUser.WM_NCCALCSIZE: { if (m.WParam == IntPtr.Zero) { RECT rect = (RECT)m.GetLParam(typeof(RECT)); this.CalculateClientBounds(ref rect); Marshal.StructureToPtr(rect, m.LParam, true); } else { NCCALCSIZE_PARAMS calc = (NCCALCSIZE_PARAMS)m.GetLParam(typeof(NCCALCSIZE_PARAMS)); this.CalculateClientBounds(ref calc.rectProposed); Marshal.StructureToPtr(calc, m.LParam, true); } m.Result = new IntPtr(WinUser.WVR_REDRAW); break; } case WinUser.WM_NCHITTEST: { /* Map all mouse messages to client area. */ m.Result = (IntPtr)WinUser.HTCLIENT; return; } case WinUser.WM_NCPAINT: { base.DefWndProc(ref m); IntPtr hDC = User32.GetWindowDC(m.HWnd); if (hDC == IntPtr.Zero) { return; } RECT bounds = new RECT(); User32.GetWindowRect(m.HWnd, ref bounds); if (bounds.Width < 1 || bounds.Height < 1) { return; } using (NuGenNativeGrfx grfx = new NuGenNativeGrfx(hDC, bounds.Size)) { this.DrawHeader(grfx.Graphics, this.RectangleToClient(bounds)); grfx.DrawToTargetGraphics(); } User32.ReleaseDC(m.HWnd, hDC); m.Result = IntPtr.Zero; return; } } base.WndProc(ref m); }
protected override void WndProc(ref Message m) { if (this.Owner.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None) { this.Owner.DefWndProcInternal(ref m); return; } switch (m.Msg) { #region WM_NCCALCSIZE (修改客户端区域) case WM.WM_NCCALCSIZE: if (m.WParam != IntPtr.Zero) { NCCALCSIZE_PARAMS rcsize = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); rcsize.rcNewWindow.Left += (this.Owner.BorderWidthInternal - 8); rcsize.rcNewWindow.Top += this.Owner.CaptionHeightInternal - SystemInformation.CaptionHeight - 6; rcsize.rcNewWindow.Right -= (this.Owner.BorderWidthInternal - 8); rcsize.rcNewWindow.Bottom -= (this.Owner.BorderWidthInternal - 8); Marshal.StructureToPtr(rcsize, m.LParam, false); } m.Result = new IntPtr(1); break; #endregion #region WM_NCLBUTTONDOWN (标题栏区域鼠标左键按下) case WM.WM_NCLBUTTONDOWN: WmCaptionMouseDown(ref m, MouseButtons.Left, 1); this.stateMouseClick = true; this.stateMouseDown = true; this.stateMouseDownButton = MouseButtons.Left; return; #endregion #region WM_NCRBUTTONDOWN (标题栏区域鼠标右键按下) case WM.WM_NCRBUTTONDOWN: WmCaptionMouseDown(ref m, MouseButtons.Right, 1); this.stateMouseClick = true; this.stateMouseDown = true; this.stateMouseDownButton = MouseButtons.Left; return; #endregion #region WM_NCMOUSEMOVE (标题栏区域鼠标移动) case WM.WM_NCMOUSEMOVE: if (stateMouseDown && MouseButtons == System.Windows.Forms.MouseButtons.None) { WmCaptionMouseUp(ref m, this.stateMouseDownButton, 1); this.stateMouseDown = false; } else { WmCaptionMouseMove(ref m, MouseButtons, 0); } break; #endregion #region WM_NCLBUTTONUP (标题栏区域鼠标左键弹起) case WM.WM_NCLBUTTONUP: WmCaptionMouseUp(ref m, MouseButtons.Left, 1); return; #endregion #region WM_NCRBUTTONUP (标题栏区域鼠标右键弹起) case WM.WM_NCRBUTTONUP: WmCaptionMouseUp(ref m, MouseButtons.Right, 1); return; #endregion #region WM_NCLBUTTONDBLCLK (标题栏区域鼠标左键双击) case WM.WM_NCLBUTTONDBLCLK: WmCaptionMouseDown(ref m, MouseButtons.Left, 2); this.stateMouseDoubleClick = true; return; #endregion #region WM_NCRBUTTONDBLCLK (标题栏区域鼠标右键双击) case WM.WM_NCRBUTTONDBLCLK: WmCaptionMouseDown(ref m, MouseButtons.Right, 2); this.stateMouseDoubleClick = true; return; #endregion #region WM_NCMOUSELEAVE (鼠标离开) case WM.WM_NCMOUSELEAVE: if (!this.stateMouseDown) { if (this.DUIControlShare.MouseMoveDUIControl != null) { this.DUIControlShare.MouseMoveDUIControl.DoMouseLeave(EventArgs.Empty); this.DUIControlShare.MouseMoveDUIControl = null; } this.stateMouseClick = false; this.stateMouseDoubleClick = false; } break; #endregion #region WM_NCMOUSEHOVER (鼠标停留) case WM.WM_NCMOUSEHOVER: this.DoMouseHover(EventArgs.Empty); return; #endregion #region WM_NCPAINT (标题栏绘制) //case WM.WM_SETCURSOR: case WM.WM_NCUAHDRAWCAPTION: this.Invalidate(); return; case WM.WM_NCACTIVATE: this.Owner.DefWndProcInternal(ref m); this.Invalidate(); return; case WM.WM_NCPAINT: this.Invalidate(); return; #endregion #region WM_SYSCOMMAND (系统命令,最大化最小化关闭之类的) case WM.WM_SYSCOMMAND: if (m.WParam.ToInt32() == SC.SC_RESTORE) { this.Owner.DoNormal(); return; } if (m.WParam.ToInt32() == SC.SC_MINIMIZE) { this.Owner.DoMinimize(); return; } if (m.WParam.ToInt32() == SC.SC_MAXIMIZE) { this.Owner.DoMaximize(); return; } if (m.WParam.ToInt32() == SC.SC_MOUSEMENU) { return; } break; #endregion } //base.WndProc(ref m); this.Owner.DefWndProcInternal(ref m); }
private void WmNccalcsize(ref Message m) { // we visual styles are not enabled and BorderStyle is not Fixed3D then we have nothing more to do. if (!UxThemeManager.VisualStylesEnabled()) { return; } // contains detailed information about WM_NCCALCSIZE message NCCALCSIZE_PARAMS par = new NCCALCSIZE_PARAMS(); // contains the window frame RECT RECT windowRect; if (m.WParam == IntPtr.Zero) // LParam points to a RECT struct { windowRect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); } else // LParam points to a NCCALCSIZE_PARAMS struct { par = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); windowRect = par.rgrc0; } // contains the client area of the control RECT contentRect; // get the DC IntPtr hDC = Win32.GetWindowDC(this.Handle); // find out how much space the borders needs if (m_ThemeManager[this].GetThemeBackgroundContentRect(UxThemeElements.COMBOBOX, hDC, (int)ComboBoxPart.Border, (int)ComboBoxState.Normal, ref windowRect, out contentRect)) { // shrink the client area the make more space for containing text. contentRect.Inflate(-1, -1); // remember the space of the borders this.borderRect = new RECT(contentRect.Left - windowRect.Left , contentRect.Top - windowRect.Top , windowRect.Right - contentRect.Right , windowRect.Bottom - contentRect.Bottom); // update LParam of the message with the new client area if (m.WParam == IntPtr.Zero) { Marshal.StructureToPtr(contentRect, m.LParam, false); } else { par.rgrc0 = contentRect; Marshal.StructureToPtr(par, m.LParam, false); } // force the control to redraw it´s client area m.Result = new IntPtr(0x200 | 0x100); } // release DC Win32.ReleaseDC(this.Handle, hDC); base.WndProc(ref m); }
/// <summary> /// Calculates the size of the window frame and client area of the RichTextBox /// </summary> private void WmNcCalcSize(ref Message m) { // let the richtextbox control draw the scrollbar if necessary. base.WndProc(ref m); // we visual styles are not enabled and BorderStyle is not Fixed3D then we have nothing more to do. if (!RenderWithVisualStyles()) return; // contains detailed information about WM_NCCALCSIZE message NCCALCSIZE_PARAMS par = new NCCALCSIZE_PARAMS(); // contains the window frame RECT RECT windowRect; if (m.WParam == IntPtr.Zero) // LParam points to a RECT struct { windowRect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); } else // LParam points to a NCCALCSIZE_PARAMS struct { par = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); windowRect = par.rgrc0; } // contains the client area of the control RECT contentRect; // get the DC IntPtr hDC = GetWindowDC(Handle); // open theme data IntPtr hTheme = OpenThemeData(Handle, "EDIT"); // find out how much space the borders needs if (GetThemeBackgroundContentRect(hTheme, hDC, EP_EDITTEXT, GetState() , ref windowRect , out contentRect) == S_OK) { // shrink the client area the make more space for containing text. contentRect.Left += 1 + Padding.Left; contentRect.Top += 1 + Padding.Top; contentRect.Right -= 1 + Padding.Right; contentRect.Bottom -= 1 + Padding.Bottom; // remember the space of the borders _borderRect = new RECT(contentRect.Left - windowRect.Left , contentRect.Top - windowRect.Top , windowRect.Right - contentRect.Right , windowRect.Bottom - contentRect.Bottom); // update LParam of the message with the new client area if (m.WParam == IntPtr.Zero) { Marshal.StructureToPtr(contentRect, m.LParam, false); } else { par.rgrc0 = contentRect; Marshal.StructureToPtr(par, m.LParam, false); } // force the control to redraw it´s client area m.Result = new IntPtr(WVR_REDRAW); } // release theme data handle CloseThemeData(hTheme); // release DC ReleaseDC(Handle, hDC); }
protected override void WndProc(ref Message m) { const uint WM_NCHITTEST = 0x0084; const uint WM_MOUSEMOVE = 0x0200; const uint HTLEFT = 10; const uint HTRIGHT = 11; const uint HTBOTTOMRIGHT = 17; const uint HTBOTTOM = 15; const uint HTBOTTOMLEFT = 16; const uint HTTOP = 12; const uint HTTOPLEFT = 13; const uint HTTOPRIGHT = 14; const int WM_NCLBUTTONDOWN = 0xA1; const int HT_CAPTION = 0x2; const int WM_NCCALCSIZE = 0x83; const int RESIZE_HANDLE_SIZE = 10; bool handled = false; if (m.Msg == WM_NCHITTEST || m.Msg == WM_MOUSEMOVE) { Size formSize = this.Size; Point screenPoint = new Point(m.LParam.ToInt32()); Point clientPoint = this.PointToClient(screenPoint); Dictionary <uint, Rectangle> boxes = new Dictionary <uint, Rectangle>() { { HTBOTTOMLEFT, new Rectangle(0, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) }, { HTBOTTOM, new Rectangle(RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, formSize.Width - 2 * RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) }, { HTBOTTOMRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) }, { HTRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2 * RESIZE_HANDLE_SIZE) }, { HTTOPRIGHT, new Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) }, { HTTOP, new Rectangle(RESIZE_HANDLE_SIZE, 0, formSize.Width - 2 * RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) }, { HTTOPLEFT, new Rectangle(0, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE) }, { HTLEFT, new Rectangle(0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2 * RESIZE_HANDLE_SIZE) } }; foreach (KeyValuePair <uint, Rectangle> hitBox in boxes) { if (hitBox.Value.Contains(clientPoint)) { m.Result = (IntPtr)hitBox.Key; handled = true; break; } } } if (m.Msg == WM_NCCALCSIZE) { if (m.WParam.Equals(IntPtr.Zero)) { RECT rc = (RECT)m.GetLParam(typeof(RECT)); Rectangle r = rc.ToRectangle(); //r.Inflate(0, 0); Marshal.StructureToPtr(new RECT(r), m.LParam, true); } else { NCCALCSIZE_PARAMS csp = (NCCALCSIZE_PARAMS)m.GetLParam(typeof(NCCALCSIZE_PARAMS)); Rectangle r = csp.rgrc0.ToRectangle(); //r.Inflate(0,0); csp.rgrc0 = new RECT(r); Marshal.StructureToPtr(csp, m.LParam, true); } m.Result = IntPtr.Zero; handled = true; } if (!handled) { base.WndProc(ref m); } }
protected override void WndProc(ref Message m) { #region WndProc debug code // Use the following code in debug mode to view the windows messages by name optionally excluding some: #if DEBUG && SHOWMSGS string s = MsgType.GetMessageName(m.Msg, false, // Messages containing the following substrings will be ignored: "WM_USER", "WM_REFLECT", "NOTIFY", "TIMER", "HIT", "MOUSE", "CURSOR", "FOCUS", "IME_", "_KEY", "_CHAR", "_GETDLG"); Debug.WriteLineIf(s.Length > 0, s); #endif #endregion WndProc debug code switch (m.Msg) { #region case WM_NCCALCSIZE: case (int)Win32Messages.WM_NCCALCSIZE: #region Non-client area definition /* * The non-client area is the part of the window managed by the operating * system and responsible for the appearance of borders, size-grippers and * title-bars. This can be managed in Windows Forms by overriding WndProc and * handling the WM_NCCALCSIZE, WM_NCPAINT, etc. message family. */ #endregion Non-client area definition #region WM_NCCALCSIZE message parameters /* * Parameters: * * wParam * If wParam is TRUE, it specifies that the application should indicate which part of the * client area contains valid information. The system copies the valid information to the * specified area within the new client area. * * If wParam is FALSE, the application does not need to indicate the valid part of the client area. * * lParam * If wParam is TRUE, lParam points to an NCCALCSIZE_PARAMS structure that contains * information an application can use to calculate the new size and position of the client rectangle. * If wParam is FALSE, lParam points to a RECT structure. On entry, the structure * contains the proposed window rectangle for the window. On exit, the structure should * contain the screen coordinates of the corresponding window client area. * * Return Value * If the wParam parameter is FALSE, the application should return zero. * * If wParam is TRUE and an application returns zero, the old client area is preserved and * is aligned with the upper-left corner of the new client area. */ #region Return values /* * If wParam is TRUE, the application should return zero or a combination of the following values: * WVR_ALIGNTOP Specifies that the client area of the window is to be preserved and aligned with * the top of the new position of the window. For example, to align the client area * to the upper-left corner, return the WVR_ALIGNTOP and WVR_ALIGNLEFT values. * WVR_ALIGNRIGHT Specifies that the client area of the window is to be preserved and aligned with * the right side of the new position of the window. For example, to align the client * area to the lower-right corner, return the WVR_ALIGNRIGHT and WVR_ALIGNBOTTOM values. * WVR_ALIGNLEFT Specifies that the client area of the window is to be preserved and aligned with * the left side of the new position of the window. For example, to align the client * area to the lower-left corner, return the WVR_ALIGNLEFT and WVR_ALIGNBOTTOM values. * WVR_ALIGNBOTTOM Specifies that the client area of the window is to be preserved and aligned with * the bottom of the new position of the window. For example, to align the client area * to the top-left corner, return the WVR_ALIGNTOP and WVR_ALIGNLEFT values. * WVR_HREDRAW Used in combination with any other values, except WVR_VALIDRECTS, causes the window * to be completely redrawn if the client rectangle changes size horizontally. This * value is similar to CS_HREDRAW class style * WVR_VREDRAW Used in combination with any other values, except WVR_VALIDRECTS, causes the window * to be completely redrawn if the client rectangle changes size vertically. This value * is similar to CS_VREDRAW class style * WVR_REDRAW This value causes the entire window to be redrawn. It is a combination of WVR_HREDRAW * and WVR_VREDRAW values. * WVR_VALIDRECTS This value indicates that, upon return from WM_NCCALCSIZE, the rectangles specified * by the rgrc[1] and rgrc[2] members of the NCCALCSIZE_PARAMS structure contain valid * destination and source area rectangles, respectively. The system combines these * rectangles to calculate the area of the window to be preserved. The system copies * any part of the window image that is within the source rectangle and clips the image * to the destination rectangle. Both rectangles are in parent-relative or screen-relative * coordinates. This flag cannot be combined with any other flags. * This return value allows an application to implement more elaborate client-area * preservation strategies, such as centering or preserving a subset of the client area. */ #endregion Return values #endregion WM_NCCALCSIZE message parameters int adjustment = this.BorderStyle == BorderStyle.FixedSingle ? 2 : 0; if ((int)m.WParam == 0) // False { #region Marshal.PtrToStructure summary /* Marshal.PtrToStructure * Summary: * Marshals data from an unmanaged block of memory to a newly allocated managed * object of the specified type. * * Parameters: * ptr: A pointer to an unmanaged block of memory. * structureType: * The System.Type of object to be created. * This type object must represent a formatted class or a structure. * * Returns: * A managed object containing the data pointed to by the ptr parameter. */ #endregion Marshal.PtrToStructure summary RECT rect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); // Adjust (shrink) the client rectangle to accommodate the border: rect.Top += m_BorderWidth - adjustment; rect.Bottom -= m_BorderWidth - adjustment; rect.Left += m_BorderWidth - adjustment; rect.Right -= m_BorderWidth - adjustment; #region Marshal.StructureToPtr summary /* Marshal.StructureToPtr * Summary: * Marshals data from a managed object to an unmanaged block of memory. * * Parameters: * ptr: * A pointer to an unmanaged block of memory, which must be allocated before * this method is called. * structure: * A managed object holding the data to be marshaled. * This object must be an instance of a formatted class. * fDeleteOld: * true to have the System.Runtime.InteropServices.Marshal.DestroyStructure * (System.IntPtr,System.Type) method called on the ptr parameter before * this method executes. Note that passing false can lead to a memory leak. */ #endregion Marshal.StructureToPtr summary Marshal.StructureToPtr(rect, m.LParam, false); m.Result = IntPtr.Zero; } else if ((int)m.WParam == 1) // True { #region Marshal.PtrToStructure summary /* Marshal.PtrToStructure * Summary: * Marshals data from an unmanaged block of memory to a newly allocated managed * object of the specified type. * * Parameters: * ptr: A pointer to an unmanaged block of memory. * structureType: * The System.Type of object to be created. * This type object must represent a formatted class or a structure. * * Returns: * A managed object containing the data pointed to by the ptr parameter. */ #endregion Marshal.PtrToStructure summary nccsp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS)); // Adjust (shrink) the client rectangle to accommodate the border: nccsp.rect0.Top += m_BorderWidth - adjustment; nccsp.rect0.Bottom -= m_BorderWidth - adjustment; nccsp.rect0.Left += m_BorderWidth - adjustment; nccsp.rect0.Right -= m_BorderWidth - adjustment; #region Marshal.StructureToPtr summary /* Marshal.StructureToPtr * Summary: * Marshals data from a managed object to an unmanaged block of memory. * * Parameters: * ptr: * A pointer to an unmanaged block of memory, which must be allocated before * this method is called. * structure: * A managed object holding the data to be marshaled. * This object must be an instance of a formatted class. * fDeleteOld: * true to have the System.Runtime.InteropServices.Marshal.DestroyStructure * (System.IntPtr,System.Type) method called on the ptr parameter before * this method executes. Note that passing false can lead to a memory leak. */ #endregion Marshal.StructureToPtr summary Marshal.StructureToPtr(nccsp, m.LParam, false); m.Result = IntPtr.Zero; } base.WndProc(ref m); break; #endregion case WM_NCCALCSIZE case (int)Win32Messages.WM_PAINT: // Hide the caret if the text is readonly: hideCaret = this.ReadOnly; base.WndProc(ref m); break; case (int)Win32Messages.WM_NCPAINT: base.WndProc(ref m); doPaint = true; #region DEBUG paintCount++ #if DEBUG paintCount++; #endif #endregion DEBUG paintCount++ break; #region Deprecated code // This message is manually posted when the control is resized. It is necessary to "redraw" the // control whenever this occurs. //case REDRAW_MSG_NUMBER: // base.WndProc(ref m); // Redraw(); // break; #endregion Deprecated code default: base.WndProc(ref m); break; } }