private void ResizeChild(WINDOWPOS wpos) { if (this.Controls.Count == 1) { Control child = this.Controls[0]; // stop window redraw to avoid flicker SendMessage(new HandleRef(child, child.Handle), WM_SETREDRAW, 0, 0); // start a new stack of SetWindowPos calls SetWindowPos(new HandleRef(child, child.Handle), new HandleRef(null, IntPtr.Zero), 0, 0, wpos.cx, wpos.cy, SWP_NOACTIVATE | SWP_NOZORDER); // turn window repainting back on SendMessage(new HandleRef(child, child.Handle), WM_SETREDRAW, 1, 0); // send repaint message to this control and its children this.Invalidate(true); } }
internal void SetWindowPos(IntPtr hDWP, WINDOWPOS pos) { var left = 0; var top = 0; var width = pos.cx; var height = pos.cy; var isPosChanged = false; var isSizeChanged = false; switch (_side) { case ShadowDockPositon.Top: left = pos.x - ShadowSize; top = pos.y - ShadowSize; if (_decorator.IsRoundedCornerStyle) { top += ROUND_CORNER_SIZE; left += ROUND_CORNER_SIZE; } else if (_decorator.IsBorderLineStyle) { top += 1; left += 1; } break; case ShadowDockPositon.Bottom: left = pos.x - ShadowSize; top = pos.y + pos.cy; if (_decorator.IsRoundedCornerStyle) { top -= ROUND_CORNER_SIZE; left += ROUND_CORNER_SIZE; } else if (_decorator.IsBorderLineStyle) { top -= 1; left += 1; } break; case ShadowDockPositon.Left: left = pos.x - ShadowSize; top = pos.y; if (_decorator.IsRoundedCornerStyle) { top += ROUND_CORNER_SIZE; left += ROUND_CORNER_SIZE; } else if (_decorator.IsBorderLineStyle) { top += 1; left += 1; } break; case ShadowDockPositon.Right: left = pos.x + pos.cx; top = pos.y; if (_decorator.IsRoundedCornerStyle) { top += ROUND_CORNER_SIZE; left -= ROUND_CORNER_SIZE; } else if (_decorator.IsBorderLineStyle) { top += 1; left -= 1; } break; } if (/*!isNoMoveFlag && */ (top != _view_top || left != _view_left)) { _view_top = top; _view_left = left; isPosChanged = true; } if (pos.cx != 0 && pos.cy != 0 /* && !isNoSizeFlag*/) { if (_side == ShadowDockPositon.Top || _side == ShadowDockPositon.Bottom) { height = ShadowSize; width = width + ShadowSize * 2; if (_decorator.IsRoundedCornerStyle) { width -= 10; } else if (_decorator.IsBorderLineStyle) { width -= 2; } } else { width = ShadowSize; if (_decorator.IsRoundedCornerStyle) { height -= 10; } else if (_decorator.IsBorderLineStyle) { height -= 2; } } if (width != _view_width || height != _view_height) { _view_width = width; _view_height = height; DiscardDeviceResource(); isSizeChanged = true; } } var dwpFlags = (SetWindowPosFlags)pos.flags | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOZORDER; if (!isSizeChanged) { dwpFlags |= SetWindowPosFlags.SWP_NOSIZE; } else { Render(); } if (!isPosChanged) { dwpFlags |= SetWindowPosFlags.SWP_NOMOVE; } User32.DeferWindowPos(hDWP, Handle, !IsTopMost ? HWND_NOTOPMOST : HWND_TOPMOST, left, top, width, height, (uint)dwpFlags); User32.DeferWindowPos(hDWP, Handle, _parentHandle, 0, 0, 0, 0, (uint)(SWP_NOSIZE_NOMOVE | SetWindowPosFlags.SWP_NOACTIVATE)); }
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 WINDOWPOS EnforceAspect(WINDOWPOS wpos) { int w = wpos.cx - windowOffset.X; int h = wpos.cy - windowOffset.Y; if (wpos.cx == lastDims.X && wpos.cy == lastDims.Y) { } else { wpos.cx = ((((w) / 16) + 1) * 16) + windowOffset.X; wpos.cy = ((((h) / 16) + 1) * 16) + windowOffset.Y; if (wpos.cx > maxSize.X) wpos.cx = maxSize.X; if (wpos.cy > maxSize.Y) wpos.cy = maxSize.Y; lastDims = new Point(wpos.cx, wpos.cy); } return wpos; }
//this is a child window for the whole Dialog protected override void WndProc(ref Message m) { RECT currentSize = new RECT(); const SetWindowPosFlags flags = SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOMOVE;//| SetWindowPosFlags.SWP_NOREPOSITION | SetWindowPosFlags.SWP_ASYNCWINDOWPOS | SetWindowPosFlags.SWP_SHOWWINDOW | SetWindowPosFlags.SWP_DRAWFRAME; switch ((Msg)m.Msg) { case Msg.WM_SHOWWINDOW: InitControls(); NativeMethods.GetWindowRect(new HandleRef(this, _hFileDialogHandle), ref currentSize); //restore original sizes int top = (_CustomControl.Parent == null) ? currentSize.top : _CustomControl.Parent.Top; int right = (_CustomControl.Parent == null) ? currentSize.right : _CustomControl.Parent.Right; RECT currentClientSize = new RECT(); NativeMethods.GetClientRect(new HandleRef(this, _hFileDialogHandle), ref currentClientSize); int dy = (int)(currentSize.Height - currentClientSize.Height); int dx = (int)(currentSize.Width - currentClientSize.Width); int Height = 0; int Width = 0; switch (_CustomControl.FileDlgStartLocation) { case AddonWindowLocation.Bottom: Width = Math.Max(_CustomControl.OriginalCtrlSize.Width + dx, (int)FileDialogControlBase.OriginalDlgWidth); NativeMethods.SetWindowPos(_hFileDialogHandle, (IntPtr)ZOrderPos.HWND_BOTTOM, right, top, Width, (int)currentSize.Height, flags); break; case AddonWindowLocation.Right: Height = Math.Max(_CustomControl.OriginalCtrlSize.Height + dy, (int)FileDialogControlBase.OriginalDlgHeight); NativeMethods.SetWindowPos(_hFileDialogHandle, (IntPtr)ZOrderPos.HWND_BOTTOM, right, top, (int)currentSize.Width, Height, flags); break; } break; case Msg.WM_SIZE: { NativeMethods.GetClientRect(new HandleRef(this, _hFileDialogHandle), ref currentSize); switch (_CustomControl.FileDlgStartLocation) { case AddonWindowLocation.Bottom: if (!mInitializated && FileDialogControlBase.OriginalDlgWidth == 0) { FileDialogControlBase.OriginalDlgWidth = currentSize.Width; } if (currentSize.Width != _CustomControl.Width) { _CustomControl.Width = (int)currentSize.Width; } break; case AddonWindowLocation.Right: if (!mInitializated && FileDialogControlBase.OriginalDlgHeight == 0) { FileDialogControlBase.OriginalDlgHeight = currentSize.Height; } if (currentSize.Height != _CustomControl.Height) { _CustomControl.Height = (int)currentSize.Height; } break; } } break; case Msg.WM_SIZING: NativeMethods.GetClientRect(new HandleRef(this, _hFileDialogHandle), ref currentSize); switch (_CustomControl.FileDlgStartLocation) { case AddonWindowLocation.Right: if (currentSize.Height != _CustomControl.Height) { NativeMethods.SetWindowPos(_CustomControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, 0, 0, (int)_CustomControl.Width, (int)currentSize.Height, UFLAGSSIZEEX); } break; case AddonWindowLocation.Bottom: if (currentSize.Height != _CustomControl.Height) { NativeMethods.SetWindowPos(_CustomControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, 0, 0, (int)currentSize.Width, (int)_CustomControl.Height, UFLAGSSIZEEX); } break; case AddonWindowLocation.BottomRight: if (currentSize.Width != _CustomControl.Width || currentSize.Height != _CustomControl.Height) { NativeMethods.SetWindowPos(_CustomControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, (int)currentSize.Width, (int)currentSize.Height, (int)currentSize.Width, (int)currentSize.Height, UFLAGSSIZEEX); } break; } break; case Msg.WM_WINDOWPOSCHANGING: if (!mIsClosing) { if (!mInitializated && !mResized) { // Resize OpenDialog to make fit our extra form WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { switch (_CustomControl.FileDlgStartLocation) { case AddonWindowLocation.Right: mOriginalSize = new Size(pos.cx, pos.cy); pos.cx += _CustomControl.Width; Marshal.StructureToPtr(pos, m.LParam, true); currentSize = new RECT(); NativeMethods.GetClientRect(new HandleRef(this, _hFileDialogHandle), ref currentSize); if (_CustomControl.Height < (int)currentSize.Height) { _CustomControl.Height = (int)currentSize.Height; } break; case AddonWindowLocation.Bottom: mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += _CustomControl.Height; Marshal.StructureToPtr(pos, m.LParam, true); currentSize = new RECT(); NativeMethods.GetClientRect(new HandleRef(this, _hFileDialogHandle), ref currentSize); if (_CustomControl.Width < (int)currentSize.Width) { _CustomControl.Width = (int)currentSize.Width; } break; case AddonWindowLocation.BottomRight: mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += _CustomControl.Height; pos.cx += _CustomControl.Width; Marshal.StructureToPtr(pos, m.LParam, true); break; } mResized = true; // Don't resize again } } } break; case Msg.WM_IME_NOTIFY: if (m.WParam == (IntPtr)ImeNotify.IMN_CLOSESTATUSWINDOW) { mIsClosing = true; NativeMethods.SetWindowPos(_hFileDialogHandle, IntPtr.Zero, 0, 0, 0, 0, UFLAGSHIDE); NativeMethods.GetWindowRect(new HandleRef(this, _hFileDialogHandle), ref _DialogWindowRect); NativeMethods.SetWindowPos(_hFileDialogHandle, IntPtr.Zero, (int)(_DialogWindowRect.left), (int)(_DialogWindowRect.top), (int)(mOriginalSize.Width), (int)(mOriginalSize.Height), FileDialogControlBase.MSFileDialogWrapper.UFLAGSSIZE); } break; case Msg.WM_PAINT: break; case Msg.WM_NCCREATE: break; case Msg.WM_CREATE: break; case Msg.WM_ACTIVATE: if (_WatchForActivate && !mIsClosing) //WM_NCACTIVATE works too { //Now the Open/Save Dialog is visible and about to enter the modal loop _WatchForActivate = false; //Now we save the real dialog window handle _hFileDialogHandle = m.LParam; ReleaseHandle(); //release the dummy window AssignHandle(_hFileDialogHandle); //assign the native open file handle to grab the messages #pragma warning disable 0197, 0414 NativeMethods.GetWindowRect(new HandleRef(this, _hFileDialogHandle), ref _CustomControl._OpenDialogWindowRect); #pragma warning restore 0197, 0414 _CustomControl._hFileDialogHandle = _hFileDialogHandle; } break; case Msg.WM_COMMAND: switch (NativeMethods.GetDlgCtrlID(m.LParam)) { case (int)ControlsId.ButtonOk: //OK break; case (int)ControlsId.ButtonCancel: //Cancel break; case (int)ControlsId.ButtonHelp: //help break; case 0: break; default: break; } //switch(NativeMethods.GetDlgCtrlID(m.LParam)) ends break; default: break; }//switch ((Msg)m.Msg) ends base.WndProc(ref m); }
protected override IntPtr WndProc(IntPtr hWnd, uint message, IntPtr wParam, IntPtr lParam) { WM wmMsg = (WM)message; switch (wmMsg) { case WM.PARENTNOTIFY: { WM wParmLw = (WM)LOWORD((int)wParam); switch (wParmLw) { case WM.LBUTTONDOWN: _dwmFramelessController?.InitiateWindowDrag(hWnd, lParam); return(IntPtr.Zero); default: break; } } break; case WM.DWMCOMPOSITIONCHANGED: _dwmFramelessController?.HandleCompositionchanged(); return(IntPtr.Zero); case WM.LBUTTONDOWN: /* Allow window dragging from any point */ var handlerNullable = _framelessInfo?.Handle; if (handlerNullable.HasValue) { ReleaseCapture(); SendMessageW(handlerNullable.Value, WM.NCLBUTTONDOWN, (IntPtr)HT.CAPTION, IntPtr.Zero); } return(IntPtr.Zero); case WM.NCACTIVATE: /* DefWindowProc won't repaint the window border if lParam (normally a * HRGN) is -1. This is recommended in: * https://blogs.msdn.microsoft.com/wpfsdk/2008/09/08/custom-window-chrome-in-wpf/ */ return(base.WndProc(hWnd, message, wParam, new IntPtr(-1))); case WM.NCCALCSIZE: _dwmFramelessController?.HandleNCCalcsize(wParam, lParam); return(IntPtr.Zero); case WM.NCHITTEST: if (_framelessInfo != null) { _framelessInfo.ResizeInMotion = true; var hitTestResultNullable = _dwmFramelessController?.HandleNCHittest(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)); _framelessInfo.ResizeInMotion = false; return(hitTestResultNullable.HasValue ? hitTestResultNullable.Value : IntPtr.Zero); } return(IntPtr.Zero); case WM.NCPAINT: /* Only block WM_NCPAINT when composition is disabled. If it's blocked * when composition is enabled, the window shadow won't be drawn. */ var isComposistionEnabedNullable = _framelessInfo?.IsCompositionEnabled; var isComposistionEnabed = isComposistionEnabedNullable.HasValue ? isComposistionEnabedNullable.Value : false; if (!isComposistionEnabed) { return(IntPtr.Zero); } break; case WM.NCUAHDRAWCAPTION: case WM.NCUAHDRAWFRAME: /* These undocumented messages are sent to draw themed window borders. * Block them to prevent drawing borders over the client area. */ return(IntPtr.Zero); case WM.SETICON: case WM.SETTEXT: /* Disable painting while these messages are handled to prevent them * from drawing a window caption over the client area, but only when * composition and theming are disabled. These messages don't paint * when composition is enabled and blocking WM_NCUAHDRAWCAPTION should * be enough to prevent painting when theming is enabled. */ isComposistionEnabedNullable = _framelessInfo?.IsCompositionEnabled; isComposistionEnabed = isComposistionEnabedNullable.HasValue ? isComposistionEnabedNullable.Value : false; var isThemeEnabledNullable = _framelessInfo?.IsThemeEnabled; var isThemeEnabledEnabed = isThemeEnabledNullable.HasValue ? isThemeEnabledNullable.Value : false; if (!isComposistionEnabed && !isThemeEnabledEnabed) { var msgResult = _dwmFramelessController?.HandleMessageInvisible(wmMsg, wParam, lParam); return(msgResult.HasValue ? msgResult.Value : IntPtr.Zero); } break; case WM.THEMECHANGED: _dwmFramelessController?.HandleThemechanged(); break; case WM.WINDOWPOSCHANGED: WINDOWPOS windPos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS)); _dwmFramelessController?.HandleWindowPosChanged(windPos); return(IntPtr.Zero); case WM.CAPTURECHANGED: if (lParam == IntPtr.Zero) { _dwmFramelessController?.ResetDragOperation(); } return(IntPtr.Zero); default: break; } return(base.WndProc(hWnd, message, wParam, lParam)); }
protected override void WndProc(ref Message m) { try { switch (m.Msg) { case WM.WM_PAINT: if (!_bPainting) { PAINTSTRUCT ps = new PAINTSTRUCT(); _bPainting = true; NativeMethods.BeginPaint(m.HWnd, ref ps); DrawScrollBar(m.HWnd, _maskControl.Handle); NativeMethods.ValidateRect(m.HWnd, ref ps.rcPaint); NativeMethods.EndPaint(m.HWnd, ref ps); _bPainting = false; m.Result = Result.TRUE; } else { base.WndProc(ref m); } break; case SBM.SBM_SETSCROLLINFO: DrawScrollBar(m.HWnd, _maskControl.Handle, true, false); base.WndProc(ref m); break; case WM.WM_STYLECHANGED: DrawScrollBar(m.HWnd, _maskControl.Handle, false, true); base.WndProc(ref m); break; case WM.WM_LBUTTONDOWN: _lastMouseDownHistTest = ScrollBarHitTest(m.HWnd); DrawScrollBar(m.HWnd, _maskControl.Handle); base.WndProc(ref m); break; case WM.WM_LBUTTONUP: case WM.WM_MOUSEMOVE: DrawScrollBar(m.HWnd, _maskControl.Handle); base.WndProc(ref m); break; case WM.WM_MOUSELEAVE: DrawScrollBar(m.HWnd, _maskControl.Handle); base.WndProc(ref m); break; case WM.WM_WINDOWPOSCHANGED: WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure( m.LParam, typeof(WINDOWPOS)); bool hide = (pos.flags & SWP.SWP_HIDEWINDOW) != 0; bool show = (pos.flags & SWP.SWP_SHOWWINDOW) != 0; if (hide) { _maskControl.SetVisibale(false); } else if (show) { _maskControl.SetVisibale(true); } _maskControl.CheckBounds(m.HWnd); base.WndProc(ref m); break; default: base.WndProc(ref m); break; } } catch { } }
protected override void WndProc(ref Message m) { switch (m.Msg) { case (int)Msg.WM_SHOWWINDOW: mInitializated = true; InitControls(); mSourceControl.OnShow(EventArgs.Empty); break; case (int)Msg.WM_WINDOWPOSCHANGING: if (!mIsClosing) { if (!mInitializated) { WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); if (mSourceControl.StartLocation == AddonWindowLocation.Right) { if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { mOriginalSize = new Size(pos.cx, pos.cy); pos.cx += mSourceControl.Width; Marshal.StructureToPtr(pos, m.LParam, true); } } if (mSourceControl.StartLocation == AddonWindowLocation.Bottom) { if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += mSourceControl.Height; Marshal.StructureToPtr(pos, m.LParam, true); } } } RECT currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); switch (mSourceControl.StartLocation) { case AddonWindowLocation.Right: { var loc = new Point((int)(currentSize.Width - mSourceControl.Width), 0); if (mSourceControl.Location != loc) { mSourceControl.Location = loc; } if (mSourceControl.Height != (int)currentSize.Height) { mSourceControl.Height = (int)currentSize.Height; } break; } case AddonWindowLocation.Bottom: { var loc = new Point(0, (int)(currentSize.Height - mSourceControl.Height)); if (mSourceControl.Location != loc) { mSourceControl.Location = loc; } if (mSourceControl.Width != (int)currentSize.Width) { mSourceControl.Width = (int)currentSize.Width; } break; } case AddonWindowLocation.None: { if (mSourceControl.Width != (int)currentSize.Width) { mSourceControl.Width = (int)currentSize.Width; } if (mSourceControl.Height != (int)currentSize.Height) { mSourceControl.Height = (int)currentSize.Height; } break; } } } break; case (int)Msg.WM_IME_NOTIFY: if (m.WParam == (IntPtr)ImeNotify.IMN_CLOSESTATUSWINDOW) { mIsClosing = true; mSourceControl.OnClosingDialog(); Win32.SetWindowPos(mOpenDialogHandle, IntPtr.Zero, 0, 0, 0, 0, UFLAGSHIDE); Win32.GetWindowRect(mOpenDialogHandle, ref mOpenDialogWindowRect); Win32.SetWindowPos(mOpenDialogHandle, IntPtr.Zero, (int)(mOpenDialogWindowRect.left), (int)(mOpenDialogWindowRect.top), (int)(mOriginalSize.Width), (int)(mOriginalSize.Height), UFLAGSSIZE); } break; } base.WndProc(ref m); }
protected override void WndProc(ref Message m) { //Console.WriteLine(m.ToString()); switch (m.Msg) { case (int)Msg.WM_SHOWWINDOW: mInitializated = true; InitControls(); break; case (int)Msg.WM_SIZING: RECT currentSize; switch (mSourceControl.StartLocation) { case AddonWindowLocation.Right: currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); if (currentSize.Height != mSourceControl.Height) { Win32.SetWindowPos(mSourceControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, 0, 0, (int)mSourceControl.Width, (int)currentSize.Height, UFLAGSSIZEEX); } break; case AddonWindowLocation.Bottom: currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); if (currentSize.Height != mSourceControl.Height) { Win32.SetWindowPos(mSourceControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, 0, 0, (int)currentSize.Width, (int)mSourceControl.Height, UFLAGSSIZEEX); } break; case AddonWindowLocation.None: currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); if (currentSize.Width != mSourceControl.Width || currentSize.Height != mSourceControl.Height) { Win32.SetWindowPos(mSourceControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, 0, 0, (int)currentSize.Width, (int)currentSize.Height, UFLAGSSIZEEX); } break; } break; case (int)Msg.WM_WINDOWPOSCHANGING: if (!mIsClosing) { if (!mInitializated) { // Resize OpenDialog to make fit our extra form WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); if (mSourceControl.StartLocation == AddonWindowLocation.Right) { if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { mOriginalSize = new Size(pos.cx, pos.cy); pos.cx += mSourceControl.Width; Marshal.StructureToPtr(pos, m.LParam, true); currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); mSourceControl.Height = (int)currentSize.Height; } } if (mSourceControl.StartLocation == AddonWindowLocation.Bottom) { if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += mSourceControl.Height; Marshal.StructureToPtr(pos, m.LParam, true); currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); mSourceControl.Width = (int)currentSize.Width; } } } } break; case (int)Msg.WM_IME_NOTIFY: if (m.WParam == (IntPtr)ImeNotify.IMN_CLOSESTATUSWINDOW) { mIsClosing = true; mSourceControl.OnClosingDialog(); Win32.SetWindowPos(mOpenDialogHandle, IntPtr.Zero, 0, 0, 0, 0, UFLAGSHIDE); Win32.GetWindowRect(mOpenDialogHandle, ref mOpenDialogWindowRect); Win32.SetWindowPos(mOpenDialogHandle, IntPtr.Zero, (int)(mOpenDialogWindowRect.left), (int)(mOpenDialogWindowRect.top), (int)(mOriginalSize.Width), (int)(mOriginalSize.Height), UFLAGSSIZE); } break; } base.WndProc(ref m); }
public void SnapToDesktopBorder( Form theClientForm, IntPtr LParam, int theWidthAdjustment) { if (theClientForm == null) { throw new ArgumentNullException(); } // Snap client to the top, left, bottom or right desktop border // as the form is moved near that border. try { // Marshal the LPARAM value which is a WINDOWPOS struct WINDOWPOS theNewPosition = new WINDOWPOS(); theNewPosition = (WINDOWPOS)Marshal.PtrToStructure( LParam, theNewPosition.GetType()); if (theNewPosition.y.Equals(0) || theNewPosition.x.Equals(0)) { return; } // Adjust the client size for borders and caption bar Rectangle theClientRectangle = theClientForm.RectangleToScreen(theClientForm.ClientRectangle); theClientRectangle.Width += SystemInformation.FrameBorderSize.Width - theWidthAdjustment; theClientRectangle.Height += SystemInformation.FrameBorderSize.Height + SystemInformation.CaptionHeight; // Now get the screen working area (without taskbar) Rectangle theWorkingRectangle = Screen.FromControl(theClientForm).WorkingArea; // Left border if (theNewPosition.x >= theWorkingRectangle.X - SNAP_OFFSET && theNewPosition.x <= theWorkingRectangle.X + SNAP_OFFSET) { theNewPosition.x = theWorkingRectangle.X; } // Get screen bounds and taskbar height (when taskbar is horizontal) Rectangle theScreenRectangle = Screen.FromControl(theClientForm).Bounds; int theTaskBarHeight = theScreenRectangle.Height = theWorkingRectangle.Height; // Top border (check if taskbar is on top or bottom via WorkingRect.Y) if (theNewPosition.y >= -SNAP_OFFSET && (theWorkingRectangle.Y > 0 && theNewPosition.y <= (theTaskBarHeight + SNAP_OFFSET)) || (theWorkingRectangle.Y <= 0 && theNewPosition.y <= SNAP_OFFSET)) { if (theTaskBarHeight > 0) { theNewPosition.y = theWorkingRectangle.Y; // Horizontal Taskbar } else { theNewPosition.y = 0; // Vertical Taskbar } } // Right border if (theNewPosition.x + theClientRectangle.Width <= theWorkingRectangle.Right + SNAP_OFFSET && theNewPosition.x + theClientRectangle.Width >= theWorkingRectangle.Right - SNAP_OFFSET) { theNewPosition.x = theWorkingRectangle.Right - (theClientRectangle.Width + SystemInformation.FrameBorderSize.Width); } // Bottom border if (theNewPosition.y + theClientRectangle.Height <= theWorkingRectangle.Bottom + SNAP_OFFSET && theNewPosition.y + theClientRectangle.Height >= theWorkingRectangle.Bottom - SNAP_OFFSET) { theNewPosition.y = theWorkingRectangle.Bottom - (theClientRectangle.Height + SystemInformation.FrameBorderSize.Height); } // Marshal it back Marshal.StructureToPtr(theNewPosition, LParam, true); } catch { throw; } }
protected override void WndProc(ref Message m) { RECT currentSize = default(RECT); Msg msg = (Msg)m.Msg; if (msg <= Msg.WM_SHOWWINDOW) { if (msg <= Msg.WM_SIZE) { if (msg != Msg.WM_CREATE) { if (msg == Msg.WM_SIZE) { NativeMethods.GetClientRect(new HandleRef(this, this._hFileDialogHandle), ref currentSize); AddonWindowLocation fileDlgStartLocation = this._CustomControl.FileDlgStartLocation; if (fileDlgStartLocation != AddonWindowLocation.Right) { if (fileDlgStartLocation == AddonWindowLocation.Bottom) { if (!this.mInitializated && FileDialogControlBase.OriginalDlgWidth == 0u) { FileDialogControlBase.OriginalDlgWidth = currentSize.Width; } if ((ulong)currentSize.Width != (ulong)((long)this._CustomControl.Width)) { this._CustomControl.Width = (int)currentSize.Width; } } } else { if (!this.mInitializated && FileDialogControlBase.OriginalDlgHeight == 0u) { FileDialogControlBase.OriginalDlgHeight = currentSize.Height; } if ((ulong)currentSize.Height != (ulong)((long)this._CustomControl.Height)) { this._CustomControl.Height = (int)currentSize.Height; } } } } } else if (msg != Msg.WM_ACTIVATE) { if (msg != Msg.WM_PAINT) { if (msg == Msg.WM_SHOWWINDOW) { this.InitControls(); NativeMethods.GetWindowRect(new HandleRef(this, this._hFileDialogHandle), ref currentSize); int top = (this._CustomControl.Parent == null) ? currentSize.top : this._CustomControl.Parent.Top; int right = (this._CustomControl.Parent == null) ? currentSize.right : this._CustomControl.Parent.Right; RECT currentClientSize = default(RECT); NativeMethods.GetClientRect(new HandleRef(this, this._hFileDialogHandle), ref currentClientSize); int dy = (int)(currentSize.Height - currentClientSize.Height); int dx = (int)(currentSize.Width - currentClientSize.Width); AddonWindowLocation fileDlgStartLocation = this._CustomControl.FileDlgStartLocation; if (fileDlgStartLocation != AddonWindowLocation.Right) { if (fileDlgStartLocation == AddonWindowLocation.Bottom) { int Width = Math.Max(this._CustomControl.OriginalCtrlSize.Width + dx, (int)FileDialogControlBase.OriginalDlgWidth); NativeMethods.SetWindowPos(this._hFileDialogHandle, (IntPtr)1, right, top, Width, (int)currentSize.Height, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOZORDER); } } else { int Height = Math.Max(this._CustomControl.OriginalCtrlSize.Height + dy, (int)FileDialogControlBase.OriginalDlgHeight); NativeMethods.SetWindowPos(this._hFileDialogHandle, (IntPtr)1, right, top, (int)currentSize.Width, Height, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOZORDER); } } } } else if (this._WatchForActivate && !this.mIsClosing) { this._WatchForActivate = false; this._hFileDialogHandle = m.LParam; this.ReleaseHandle(); base.AssignHandle(this._hFileDialogHandle); NativeMethods.GetWindowRect(new HandleRef(this, this._hFileDialogHandle), ref this._CustomControl._OpenDialogWindowRect); this._CustomControl._hFileDialogHandle = this._hFileDialogHandle; } } else if (msg <= Msg.WM_NCCREATE) { if (msg != Msg.WM_WINDOWPOSCHANGING) { if (msg != Msg.WM_NCCREATE) { } } else if (!this.mIsClosing && !this.mInitializated && !this.mResized) { WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); if (pos.flags != 0u && (pos.flags & 1u) != 1u) { switch (this._CustomControl.FileDlgStartLocation) { case AddonWindowLocation.BottomRight: this.mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += this._CustomControl.Height; pos.cx += this._CustomControl.Width; Marshal.StructureToPtr <WINDOWPOS>(pos, m.LParam, true); break; case AddonWindowLocation.Right: this.mOriginalSize = new Size(pos.cx, pos.cy); pos.cx += this._CustomControl.Width; Marshal.StructureToPtr <WINDOWPOS>(pos, m.LParam, true); currentSize = default(RECT); NativeMethods.GetClientRect(new HandleRef(this, this._hFileDialogHandle), ref currentSize); if (this._CustomControl.Height < (int)currentSize.Height) { this._CustomControl.Height = (int)currentSize.Height; } break; case AddonWindowLocation.Bottom: this.mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += this._CustomControl.Height; Marshal.StructureToPtr <WINDOWPOS>(pos, m.LParam, true); currentSize = default(RECT); NativeMethods.GetClientRect(new HandleRef(this, this._hFileDialogHandle), ref currentSize); if (this._CustomControl.Width < (int)currentSize.Width) { this._CustomControl.Width = (int)currentSize.Width; } break; } this.mResized = true; } } } else if (msg != Msg.WM_COMMAND) { if (msg != Msg.WM_SIZING) { if (msg == Msg.WM_IME_NOTIFY) { if (m.WParam == (IntPtr)1) { this.mIsClosing = true; NativeMethods.SetWindowPos(this._hFileDialogHandle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_HIDEWINDOW | SetWindowPosFlags.SWP_NOOWNERZORDER); NativeMethods.GetWindowRect(new HandleRef(this, this._hFileDialogHandle), ref this._DialogWindowRect); NativeMethods.SetWindowPos(this._hFileDialogHandle, IntPtr.Zero, this._DialogWindowRect.left, this._DialogWindowRect.top, this.mOriginalSize.Width, this.mOriginalSize.Height, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOOWNERZORDER); } } } else { NativeMethods.GetClientRect(new HandleRef(this, this._hFileDialogHandle), ref currentSize); switch (this._CustomControl.FileDlgStartLocation) { case AddonWindowLocation.BottomRight: if ((ulong)currentSize.Width != (ulong)((long)this._CustomControl.Width) || (ulong)currentSize.Height != (ulong)((long)this._CustomControl.Height)) { NativeMethods.SetWindowPos(this._CustomControl.Handle, (IntPtr)1, (int)currentSize.Width, (int)currentSize.Height, (int)currentSize.Width, (int)currentSize.Height, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_DEFERERASE | SetWindowPosFlags.SWP_ASYNCWINDOWPOS); } break; case AddonWindowLocation.Right: if ((ulong)currentSize.Height != (ulong)((long)this._CustomControl.Height)) { NativeMethods.SetWindowPos(this._CustomControl.Handle, (IntPtr)1, 0, 0, this._CustomControl.Width, (int)currentSize.Height, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_DEFERERASE | SetWindowPosFlags.SWP_ASYNCWINDOWPOS); } break; case AddonWindowLocation.Bottom: if ((ulong)currentSize.Height != (ulong)((long)this._CustomControl.Height)) { NativeMethods.SetWindowPos(this._CustomControl.Handle, (IntPtr)1, 0, 0, (int)currentSize.Width, this._CustomControl.Height, SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_DEFERERASE | SetWindowPosFlags.SWP_ASYNCWINDOWPOS); } break; } } } else { switch (NativeMethods.GetDlgCtrlID(m.LParam)) { } } base.WndProc(ref m); }
private static IntPtr WindowHookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { CustomChrome window = GetWindow(hWnd) as CustomChrome; switch (msg) { // Toggle the DropShadowEffect when window is snapped or maximized case WM_SIZE: { int resizing = (int)wParam; if (resizing == SIZE_RESTORED) { MonitorArea monitorArea = GetMonitorArea(hWnd); if (monitorArea != null) { // LOWORD int width = ((int)lParam & 0x0000ffff); // HIWORD int height = (int)((int)lParam & 0xffff0000) >> 16; // Detect if window was snapped to screen side of current monitor // or if spanning width on multiple monitors (to avoid unsnapping) if (height == monitorArea.Work.Height || width >= SystemParameters.VirtualScreenWidth) { window.IsSnapped = true; UpdateResizeBorder(window, monitorArea, window.Left, window.Left + width); } else { window.IsSnapped = false; ShowDropShadow(window); EnableResizeBorder(window); } } } else if (resizing == SIZE_MAXIMIZED) { // Required when maximized from dragging window DisableResizeBorder(window); } } break; // To handle proper resizing of the custom window case WM_GETMINMAXINFO: { MonitorArea monitorArea = GetMonitorArea(hWnd); if (monitorArea != null) { MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO)); mmi.ptMaxPosition.x = monitorArea.Offset.x; mmi.ptMaxPosition.y = monitorArea.Offset.y; mmi.ptMaxSize.x = monitorArea.Work.Width; mmi.ptMaxSize.y = monitorArea.Work.Height; // To support minimum window size mmi.ptMinTrackSize.x = (int)window.MinWidth; mmi.ptMinTrackSize.y = (int)window.MinHeight; Marshal.StructureToPtr(mmi, lParam, true); handled = true; } } break; // To activate/deactivate border resize handles from window position case WM_WINDOWPOSCHANGED: { WINDOWPOS windowPos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS)); // When window is snapped and position changes if ((windowPos.flags & SWP_NOMOVE) != SWP_NOMOVE) { if (window.IsSnapped) { MonitorArea monitorArea = GetMonitorArea(hWnd); if (monitorArea != null) { UpdateResizeBorder(window, monitorArea, windowPos.x, windowPos.x + windowPos.cx); } } } } break; } return(IntPtr.Zero); }
/// <summary> /// Handles the given window position and size change. /// </summary> /// <param name="hwnd">The handle to the window.</param> /// <param name="lParam">Additional information.</param> private static IntPtr WindowChangePos(IntPtr hwnd, IntPtr lParam) { // Get the current position var pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS)); // Check the position // 2 represents no move if ((pos.flags & 2) != 0) { return(IntPtr.Zero); } // Get the window var window = (System.Windows.Window)HwndSource.FromHwnd(hwnd)?.RootVisual; // Check the window if (window == null) { return(IntPtr.Zero); } // Get the DPI var presentationSource = PresentationSource.FromVisual(window); var dpiMatrix = presentationSource?.CompositionTarget?.TransformToDevice; // Initialize the flag var changedPosition = false; // Get the minimum values var minWidth = window.MinWidth * (dpiMatrix?.M11 ?? 1); var minHeight = window.MinHeight * (dpiMatrix?.M22 ?? 1); // Check for minimum width if (pos.cx < minWidth) { // Set the position pos.cx = (int)minWidth; // Set the x position pos.x = _prevPos.x; // Set the flag changedPosition = true; } if (pos.cy < minHeight) { // Set the position pos.cy = (int)minHeight; // Set the y position pos.y = _prevPos.y; // Set the flag changedPosition = true; } // Keep the position _prevPos = pos; // Check the flag if (!changedPosition) { return(IntPtr.Zero); } // Set the new position Marshal.StructureToPtr(pos, lParam, true); return((IntPtr)1); }
protected override void WndProc(ref Message m) { try { if (m.Msg == uCallBackMsg) { switch (m.WParam.ToInt32()) { case (int)ABNotify.ABN_FULLSCREENAPP: { if ((int)m.LParam == 1) { this.RunningFullScreenApp = true; this.TopMost = false; } else { this.RunningFullScreenApp = false; this.TopMost = true; } break; } default: break; } } switch (m.Msg) { case 0X46: //WM_WINDOWPOSCHANGING WINDOWPOS windowPos = (WINDOWPOS)m.GetLParam(typeof(WINDOWPOS)); if (windowPos.x + windowPos.cx > rectangle_Screen.Right) { windowPos.x = rectangle_Screen.Right - windowPos.cx; } if (windowPos.y + windowPos.cy > rectangle_Screen.Bottom) { windowPos.y = rectangle_Screen.Bottom - windowPos.cy; } if (windowPos.x < rectangle_Screen.Top) { windowPos.x = rectangle_Screen.Top; } if (windowPos.y < rectangle_Screen.Left) { windowPos.y = 0; } Marshal.StructureToPtr(windowPos, m.LParam, false); base.WndProc(ref m); break; default: base.WndProc(ref m); break; } } catch (Exception ex) { log.WriteLog(ex.ToString()); } }
protected override void WndProc(ref Message m) { switch ((Msg)m.Msg) { case Msg.WM_SHOWWINDOW: mInitializated = true; InitControls(); break; case Msg.WM_SIZING: RECT currentSize = new RECT(); NativeMethods.GetClientRect(_hFileDialogHandle, ref currentSize); switch (_CustomControl.FileDlgStartLocation) { case AddonWindowLocation.Right: if (currentSize.Height != _CustomControl.Height) { NativeMethods.SetWindowPos(_CustomControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, 0, 0, (int)_CustomControl.Width, (int)currentSize.Height, UFLAGSSIZEEX); } break; case AddonWindowLocation.Bottom: if (currentSize.Height != _CustomControl.Height) { NativeMethods.SetWindowPos(_CustomControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, 0, 0, (int)currentSize.Width, (int)_CustomControl.Height, UFLAGSSIZEEX); } break; case AddonWindowLocation.BottomRight: if (currentSize.Width != _CustomControl.Width || currentSize.Height != _CustomControl.Height) { NativeMethods.SetWindowPos(_CustomControl.Handle, (IntPtr)ZOrderPos.HWND_BOTTOM, (int)currentSize.Width, (int)currentSize.Height, (int)currentSize.Width, (int)currentSize.Height, UFLAGSSIZEEX); } break; } break; case Msg.WM_WINDOWPOSCHANGING: if (!mIsClosing) { if (!mInitializated) { // Resize OpenDialog to make fit our extra form WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { switch (_CustomControl.FileDlgStartLocation) { case AddonWindowLocation.Right: mOriginalSize = new Size(pos.cx, pos.cy); pos.cx += _CustomControl.Width; Marshal.StructureToPtr(pos, m.LParam, true); currentSize = new RECT(); NativeMethods.GetClientRect(_hFileDialogHandle, ref currentSize); _CustomControl.Height = (int)currentSize.Height; break; case AddonWindowLocation.Bottom: mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += _CustomControl.Height; Marshal.StructureToPtr(pos, m.LParam, true); currentSize = new RECT(); NativeMethods.GetClientRect(_hFileDialogHandle, ref currentSize); _CustomControl.Width = (int)currentSize.Width; break; case AddonWindowLocation.BottomRight: mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += _CustomControl.Height; pos.cx += _CustomControl.Width; Marshal.StructureToPtr(pos, m.LParam, true); break; } } } } break; case Msg.WM_IME_NOTIFY: if (m.WParam == (IntPtr)ImeNotify.IMN_CLOSESTATUSWINDOW) { mIsClosing = true; NativeMethods.SetWindowPos(_hFileDialogHandle, IntPtr.Zero, 0, 0, 0, 0, UFLAGSHIDE); NativeMethods.GetWindowRect(_hFileDialogHandle, ref _DialogWindowRect); NativeMethods.SetWindowPos(_hFileDialogHandle, IntPtr.Zero, (int)(_DialogWindowRect.left), (int)(_DialogWindowRect.top), (int)(mOriginalSize.Width), (int)(mOriginalSize.Height), FileDialogControlBase.NativeFileDialogWrapper.UFLAGSSIZE); } break; case Msg.WM_PAINT: break; case Msg.WM_NCCREATE: break; case Msg.WM_CREATE: break; case Msg.WM_ACTIVATE: if (_WatchForActivate && !mIsClosing && m.Msg == (int)Msg.WM_ACTIVATE) //WM_NCACTIVATE works too { //Now the Open/Save Dialog is visible and about to enter the modal loop _WatchForActivate = false; //Now we save the real dialog window handle _hFileDialogHandle = m.LParam; ReleaseHandle(); //release the dummy window AssignHandle(_hFileDialogHandle); //assign the native open file handle to grab the messages #pragma warning disable 0197, 0414 NativeMethods.GetWindowRect(_hFileDialogHandle, ref _CustomControl._OpenDialogWindowRect); #pragma warning restore 0197, 0414 _CustomControl._hFileDialogHandle = _hFileDialogHandle; } break; case Msg.WM_COMMAND: switch (NativeMethods.GetDlgCtrlID(m.LParam)) { case (int)ControlsId.ButtonOk: //OK break; case (int)ControlsId.ButtonCancel: //Cancel #pragma warning disable 1690, 0414 NativeMethods.SetWindowPos(_CustomControl._hFileDialogHandle, IntPtr.Zero, (int)_CustomControl._OpenDialogWindowRect.left, (int)_CustomControl._OpenDialogWindowRect.top, (int)_CustomControl._OpenDialogWindowRect.Width, (int)_CustomControl._OpenDialogWindowRect.Height, FileDialogControlBase.NativeFileDialogWrapper.UFLAGSSIZE); #pragma warning restore 1690, 0414 break; case (int)ControlsId.ButtonHelp: //help break; case 0: break; default: break; }//switch(NativeMethods.GetDlgCtrlID(m.LParam)) ends break; default: break; }//switch ((Msg)m.Msg) ends base.WndProc(ref m); }
public IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam) { switch ((WM)msg) { case WM.COPYDATA: if (lParam == IntPtr.Zero) { CairoLogger.Instance.Debug("TrayService: CopyData is null"); break; } COPYDATASTRUCT copyData = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT)); switch ((int)copyData.dwData) { case 0: // AppBar message if (Marshal.SizeOf(typeof(APPBARMSGDATAV3)) == copyData.cbData) { APPBARMSGDATAV3 amd = (APPBARMSGDATAV3)Marshal.PtrToStructure(copyData.lpData, typeof(APPBARMSGDATAV3)); if (Marshal.SizeOf(typeof(APPBARDATAV2)) != amd.abd.cbSize) { CairoLogger.Instance.Debug("TrayService: Size incorrect for APPBARMSGDATAV3"); break; } if (AppBarMessageAction(amd)) { return((IntPtr)1); } } else { CairoLogger.Instance.Debug("TrayService: AppBar message received, but with unknown size"); } break; case 1: SHELLTRAYDATA trayData = (SHELLTRAYDATA)Marshal.PtrToStructure(copyData.lpData, typeof(SHELLTRAYDATA)); if (trayDelegate != null) { if (trayDelegate(trayData.dwMessage, new SafeNotifyIconData(trayData.nid))) { return((IntPtr)1); } CairoLogger.Instance.Debug("TrayService: Ignored notify icon message"); } else { CairoLogger.Instance.Info("TrayService: TrayDelegate is null"); } break; case 3: WINNOTIFYICONIDENTIFIER iconData = (WINNOTIFYICONIDENTIFIER)Marshal.PtrToStructure(copyData.lpData, typeof(WINNOTIFYICONIDENTIFIER)); if (iconDataDelegate != null) { return(iconDataDelegate(iconData.dwMessage, iconData.hWnd, iconData.uID, iconData.guidItem)); } CairoLogger.Instance.Info("TrayService: IconDataDelegate is null"); break; } break; case WM.WINDOWPOSCHANGED: WINDOWPOS wndPos = WINDOWPOS.FromMessage(lParam); if ((wndPos.flags & SetWindowPosFlags.SWP_SHOWWINDOW) != 0) { SetWindowLong(HwndTray, GWL_STYLE, GetWindowLong(HwndTray, GWL_STYLE) & ~(int)WindowStyles.WS_VISIBLE); CairoLogger.Instance.Debug($"TrayService: {TrayWndClass} became visible; hiding"); } break; } if (msg == (int)WM.COPYDATA || msg == (int)WM.ACTIVATEAPP) { return(ForwardMsg(msg, wParam, lParam)); } return(DefWindowProc(hWnd, msg, wParam, lParam)); }
protected override void WndProc(ref Message m) { switch (m.Msg) { case (int)Msg.WM_CLOSE: { if (mSourceControl != null) { CancelEventArgs e = new CancelEventArgs(); e.Cancel = false; mSourceControl.OnClosingDialog(e); //if (e.Cancel == true && mIsOpen == true) // return; } } break; case (int)Msg.WM_SHOWWINDOW: mInitializated = true; InitControls(); break; case (int)Msg.WM_WINDOWPOSCHANGING: if (!mIsClosing) { if (!mInitializated) { WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); if (mSourceControl.StartLocation == AddonWindowLocation.Right) { if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { mOriginalSize = new Size(pos.cx, pos.cy); pos.cx += mSourceControl.Width; Marshal.StructureToPtr(pos, m.LParam, true); } } if (mSourceControl.StartLocation == AddonWindowLocation.Bottom) { if (pos.flags != 0 && ((pos.flags & (int)SWP_Flags.SWP_NOSIZE) != (int)SWP_Flags.SWP_NOSIZE)) { mOriginalSize = new Size(pos.cx, pos.cy); pos.cy += mSourceControl.Height; Marshal.StructureToPtr(pos, m.LParam, true); } } } RECT currentSize; switch (mSourceControl.StartLocation) { case AddonWindowLocation.Right: currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); mSourceControl.Height = (int)currentSize.Height; break; case AddonWindowLocation.Bottom: currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); mSourceControl.Width = (int)currentSize.Width; break; case AddonWindowLocation.None: currentSize = new RECT(); Win32.GetClientRect(mOpenDialogHandle, ref currentSize); mSourceControl.Width = (int)currentSize.Width; mSourceControl.Height = (int)currentSize.Height; break; } } break; case (int)Msg.WM_IME_NOTIFY: if (m.WParam == (IntPtr)ImeNotify.IMN_CLOSESTATUSWINDOW) { mIsClosing = true; CancelEventArgs e = new CancelEventArgs(); e.Cancel = false; mSourceControl.OnClosingDialog(e); Win32.SetWindowPos(mOpenDialogHandle, IntPtr.Zero, 0, 0, 0, 0, UFLAGSHIDE); Win32.GetWindowRect(mOpenDialogHandle, ref mOpenDialogWindowRect); Win32.SetWindowPos(mOpenDialogHandle, IntPtr.Zero, (int)(mOpenDialogWindowRect.left), (int)(mOpenDialogWindowRect.top), (int)(mOriginalSize.Width), (int)(mOriginalSize.Height), UFLAGSSIZE); } break; case (int)Msg.WM_COMMAND: if (m.WParam.ToInt32() == 1) //´ò¿ª { mIsOpen = true; OFNOTIFY ofNotify = new OFNOTIFY(); ofNotify.fileNameShareViolation = IntPtr.Zero; StringBuilder strbuf = new StringBuilder(256); Win32.GetWindowText(mComboFileName, strbuf, 256); if (strbuf.Length > 0) { ofNotify.OPENFILENAME = Marshal.StringToBSTR(strbuf.ToString()); ofNotify.hdr.code = (uint)DialogChangeStatus.CDN_SELCHANGE; int rawsize = Marshal.SizeOf(ofNotify); byte[] rawdata = new byte[rawsize]; GCHandle handle = GCHandle.Alloc(rawdata, GCHandleType.Pinned); IntPtr wParam = handle.AddrOfPinnedObject(); Marshal.StructureToPtr(ofNotify, wParam, false); //Win32.SendMessage(mhwnd, (int)Msg.WM_NOTIFY, IntPtr.Zero, wParam); handle.Free(); CancelEventArgs e = new CancelEventArgs(); e.Cancel = false; mSourceControl.OnClosingDialog(e); if (e.Cancel == true) { if (File.Exists(strbuf.ToString())) { string strDir = Path.GetDirectoryName(strbuf.ToString()); if (strDir.Length <= 0) { return; } Win32.SetWindowText(mComboFileName, new StringBuilder(strDir)); } } } } else if (m.WParam.ToInt32() == 2) //È¡Ïû { mIsOpen = false; } break; } base.WndProc(ref m); }
public static void SnapToDesktopBorder(Form clientForm, IntPtr LParam, int widthAdjustment) { if (clientForm == null) { // Satisfies rule: Validate parameters throw new ArgumentNullException("clientForm"); } // Snap client to the top, left, bottom or right desktop border // as the form is moved near that border. try { // Marshal the LPARAM value which is a WINDOWPOS struct WINDOWPOS NewPosition = new WINDOWPOS(); NewPosition = (WINDOWPOS)System.Runtime.InteropServices.Marshal.PtrToStructure(LParam, typeof(WINDOWPOS)); if (NewPosition.y == 0 || NewPosition.x == 0) { return;// Nothing to do! } // Adjust the client size for borders and caption bar Rectangle ClientRect = clientForm.RectangleToScreen(clientForm.ClientRectangle); ClientRect.Width += SystemInformation.FrameBorderSize.Width - widthAdjustment - dpicalc(clientForm, 8); ClientRect.Height += (SystemInformation.FrameBorderSize.Height + SystemInformation.CaptionHeight - dpicalc(clientForm, 31)); // 임의로 포인트 변경 // Now get the screen working area (without taskbar) Rectangle WorkingRect = Screen.GetWorkingArea(clientForm.ClientRectangle); // Left border if (NewPosition.x >= WorkingRect.X - mSnapOffset && NewPosition.x <= WorkingRect.X + mSnapOffset) { NewPosition.x = WorkingRect.X; } // Get screen bounds and taskbar height // (when taskbar is horizontal) Rectangle ScreenRect = Screen.GetBounds(Screen.PrimaryScreen.Bounds); int TaskbarHeight = ScreenRect.Height - WorkingRect.Height; // Top border (check if taskbar is on top // or bottom via WorkingRect.Y) if (NewPosition.y >= -mSnapOffset && (WorkingRect.Y > 0 && NewPosition.y <= (TaskbarHeight + mSnapOffset)) || (WorkingRect.Y <= 0 && NewPosition.y <= (mSnapOffset))) { if (TaskbarHeight > 0) { NewPosition.y = WorkingRect.Y; // Horizontal Taskbar } else { NewPosition.y = 0;// Vertical Taskbar } } // Right border if (NewPosition.x + ClientRect.Width <= WorkingRect.Right + mSnapOffset && NewPosition.x + ClientRect.Width >= WorkingRect.Right - mSnapOffset) { NewPosition.x = WorkingRect.Right - (ClientRect.Width + SystemInformation.FrameBorderSize.Width); } // Bottom border if (NewPosition.y + ClientRect.Height <= WorkingRect.Bottom + mSnapOffset && NewPosition.y + ClientRect.Height >= WorkingRect.Bottom - mSnapOffset) { NewPosition.y = WorkingRect.Bottom - (ClientRect.Height + SystemInformation.FrameBorderSize.Height); } // Marshal it back System.Runtime.InteropServices.Marshal.StructureToPtr(NewPosition, LParam, true); } catch (ArgumentException ex) { } }
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handeled) { switch (msg) { case NativeFlags.WM_GETMINMAXINFO: WmGetMinMaxInfo(hwnd, lParam); handeled = true; break; case NativeFlags.WM_WINDOWPOSCHANGING: WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS)); if ((pos.flags & 0x0002) != 0) { return(IntPtr.Zero); } Window wnd = (Window)HwndSource.FromHwnd(hwnd).RootVisual; if (wnd == null) { return(IntPtr.Zero); } bool changedPos = false; PresentationSource source = PresentationSource.FromVisual(this); double wpfDpi = 96; double dpiX = wpfDpi; double dpiY = wpfDpi; if (source != null) { dpiX = wpfDpi * source.CompositionTarget.TransformToDevice.M11; dpiY = wpfDpi * source.CompositionTarget.TransformToDevice.M22; } int minWidth = (int)Math.Round(MinWidth / 96 * dpiX, 0); if (pos.cx < minWidth) { pos.cx = minWidth; changedPos = true; } int minHeight = (int)Math.Round(MinHeight / 96 * dpiY, 0); if (pos.cy < minHeight) { pos.cy = minHeight; changedPos = true; } if (!changedPos) { return(IntPtr.Zero); } Marshal.StructureToPtr(pos, lParam, true); handeled = true; break; } return((System.IntPtr) 0); }
public static IntPtr MonitorFromWindowPosOrWindow(WINDOWPOS windowpos, IntPtr hwnd) { var windowRect = windowpos.ToRECT(); return(MonitorFromRectOrWindow(windowRect, hwnd)); }
protected override IntPtr WindowProcedureHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { var result = base.WindowProcedureHook(hwnd, msg, wParam, lParam, ref handled); if (handled) { return(result); } switch (msg) { case 0x0024: WmGetMinMaxInfo(hwnd, lParam); break; case 0x0046: { WINDOWPOS pos = (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS)); if ((pos.flags & (int)SWP.NOMOVE) != 0) { return(IntPtr.Zero); } Window wnd = (Window)HwndSource.FromHwnd(hwnd).RootVisual; if (wnd == null) { return(IntPtr.Zero); } bool changedPos = false; // *********************** // Here you check the values inside the pos structure // if you want to override them just change the pos // structure and set changedPos to true // *********************** // this is a simplified version that doesn't work in high-dpi settings // pos.cx and pos.cy are in "device pixels" and MinWidth and MinHeight // are in "WPF pixels" (WPF pixels are always 1/96 of an inch - if your // system is configured correctly). if (pos.cx < wnd.MinWidth) { pos.cx = (int)wnd.MinWidth; changedPos = true; } if (pos.cy < wnd.MinHeight) { pos.cy = (int)wnd.MinHeight; changedPos = true; } // *********************** // end of "logic" // *********************** if (!changedPos) { return(IntPtr.Zero); } Marshal.StructureToPtr(pos, lParam, true); handled = true; } break; } return(IntPtr.Zero); }
private void _SetRoundingRegion(WINDOWPOS? wp) { const int MONITOR_DEFAULTTONEAREST = 0x00000002; // We're early - WPF hasn't necessarily updated the state of the window. // Need to query it ourselves. WINDOWPLACEMENT wpl = NativeMethods.GetWindowPlacement(_hwnd); if (wpl.showCmd == SW.SHOWMAXIMIZED) { int left; int top; if (wp.HasValue) { left = wp.Value.x; top = wp.Value.y; } else { Rect r = _GetWindowRect(); left = (int)r.Left; top = (int)r.Top; } IntPtr hMon = NativeMethods.MonitorFromWindow(_hwnd, MONITOR_DEFAULTTONEAREST); MONITORINFO mi = NativeMethods.GetMonitorInfo(hMon); RECT rcMax = mi.rcWork; // The location of maximized window takes into account the border that Windows was // going to remove, so we also need to consider it. rcMax.Offset(-left, -top); IntPtr hrgn = IntPtr.Zero; try { hrgn = NativeMethods.CreateRectRgnIndirect(rcMax); NativeMethods.SetWindowRgn(_hwnd, hrgn, NativeMethods.IsWindowVisible(_hwnd)); hrgn = IntPtr.Zero; } finally { Utility.SafeDeleteObject(ref hrgn); } } else { Size windowSize; // Use the size if it's specified. if (null != wp && !Utility.IsFlagSet(wp.Value.flags, (int)SWP.NOSIZE)) { windowSize = new Size((double)wp.Value.cx, (double)wp.Value.cy); } else if (null != wp && (_lastRoundingState == _window.WindowState)) { return; } else { windowSize = _GetWindowRect().Size; } _lastRoundingState = _window.WindowState; IntPtr hrgn = IntPtr.Zero; try { double shortestDimension = Math.Min(windowSize.Width, windowSize.Height); double topLeftRadius = DpiHelper.LogicalPixelsToDevice(new Point(_chromeInfo.CornerRadius.TopLeft, 0)).X; topLeftRadius = Math.Min(topLeftRadius, shortestDimension / 2); if (_IsUniform(_chromeInfo.CornerRadius)) { // RoundedRect HRGNs require an additional pixel of padding. hrgn = _CreateRoundRectRgn(new Rect(windowSize), topLeftRadius); } else { // We need to combine HRGNs for each of the corners. // Create one for each quadrant, but let it overlap into the two adjacent ones // by the radius amount to ensure that there aren't corners etched into the middle // of the window. hrgn = _CreateRoundRectRgn(new Rect(0, 0, windowSize.Width / 2 + topLeftRadius, windowSize.Height / 2 + topLeftRadius), topLeftRadius); double topRightRadius = DpiHelper.LogicalPixelsToDevice(new Point(_chromeInfo.CornerRadius.TopRight, 0)).X; topRightRadius = Math.Min(topRightRadius, shortestDimension / 2); Rect topRightRegionRect = new Rect(0, 0, windowSize.Width / 2 + topRightRadius, windowSize.Height / 2 + topRightRadius); topRightRegionRect.Offset(windowSize.Width / 2 - topRightRadius, 0); Assert.AreEqual(topRightRegionRect.Right, windowSize.Width); _CreateAndCombineRoundRectRgn(hrgn, topRightRegionRect, topRightRadius); double bottomLeftRadius = DpiHelper.LogicalPixelsToDevice(new Point(_chromeInfo.CornerRadius.BottomLeft, 0)).X; bottomLeftRadius = Math.Min(bottomLeftRadius, shortestDimension / 2); Rect bottomLeftRegionRect = new Rect(0, 0, windowSize.Width / 2 + bottomLeftRadius, windowSize.Height / 2 + bottomLeftRadius); bottomLeftRegionRect.Offset(0, windowSize.Height / 2 - bottomLeftRadius); Assert.AreEqual(bottomLeftRegionRect.Bottom, windowSize.Height); _CreateAndCombineRoundRectRgn(hrgn, bottomLeftRegionRect, bottomLeftRadius); double bottomRightRadius = DpiHelper.LogicalPixelsToDevice(new Point(_chromeInfo.CornerRadius.BottomRight, 0)).X; bottomRightRadius = Math.Min(bottomRightRadius, shortestDimension / 2); Rect bottomRightRegionRect = new Rect(0, 0, windowSize.Width / 2 + bottomRightRadius, windowSize.Height / 2 + bottomRightRadius); bottomRightRegionRect.Offset(windowSize.Width / 2 - bottomRightRadius, windowSize.Height / 2 - bottomRightRadius); Assert.AreEqual(bottomRightRegionRect.Right, windowSize.Width); Assert.AreEqual(bottomRightRegionRect.Bottom, windowSize.Height); _CreateAndCombineRoundRectRgn(hrgn, bottomRightRegionRect, bottomRightRadius); } NativeMethods.SetWindowRgn(_hwnd, hrgn, NativeMethods.IsWindowVisible(_hwnd)); hrgn = IntPtr.Zero; } finally { // Free the memory associated with the HRGN if it wasn't assigned to the HWND. Utility.SafeDeleteObject(ref hrgn); } } }
///// <summary> ///// 启用或禁用窗口大小调整。 ///// </summary> ///// <param name="enable">True/False</param> //public void EnableResize(bool enable) //{ // foreach (FormShadowElement sideShadow in shadows) // { // sideShadow.ExternalResizeEnable = enable; // } //} protected override void WndProc(ref Message m) { if (!isEnabled || IsDisposed) { base.WndProc(ref m); return; } var msg = (WindowsMessages)m.Msg; //System.Diagnostics.Debug.WriteLine(m); switch (msg) { case WindowsMessages.WM_WINDOWPOSCHANGED: lastLocation = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); WindowPosChanged(lastLocation); base.WndProc(ref m); break; case WindowsMessages.WM_ACTIVATEAPP: { var className = new StringBuilder(256); if (m.LParam != IntPtr.Zero && User32.GetClassName(m.LParam, className, className.Capacity) != 0) { var hWndShadow = m.LParam; var name = className.ToString(); if (name.StartsWith(CONSTS.CLASS_NAME) && isFocused && shadows.Exists(p => p.Handle == hWndShadow)) { return; } } if (m.WParam == Win32.FALSE) { isFocused = false; KillFocus(); } else { isFocused = true; SetFocus(); } } break; case WindowsMessages.WM_SIZE: Size(m.WParam, m.LParam); base.WndProc(ref m); break; default: base.WndProc(ref m); break; } }
protected override void WndProc(ref Message message) { switch (message.Msg) { case WM_COMMAND: { // need to dispatch the message for the context menu if (message.LParam == IntPtr.Zero) { _commandDispatch.Invoke(null, new object[] { message.WParam.ToInt32()& 0xFFFF }); } } break; case WM_NCHITTEST: { message.Result = (IntPtr)HitResult.Caption; if (HitTest != null) { Point p = new Point( Macros.GET_X_LPARAM(message.LParam) - _location.X, Macros.GET_Y_LPARAM(message.LParam) - _location.Y ); HitTestEventArgs e = new HitTestEventArgs(p, HitResult.Caption); HitTest(this, e); message.Result = (IntPtr)e.HitResult; } } break; case WM_NCLBUTTONDBLCLK: { MouseDoubleClick?.Invoke(this, new MouseEventArgs(MouseButtons.Left, 2, Macros.GET_X_LPARAM(message.LParam) - _location.X, Macros.GET_Y_LPARAM(message.LParam) - _location.Y, 0)); message.Result = IntPtr.Zero; } break; case WM_NCRBUTTONDOWN: { message.Result = IntPtr.Zero; } break; case WM_NCRBUTTONUP: { ContextMenuStrip?.Show(new Point(Macros.GET_X_LPARAM(message.LParam), Macros.GET_Y_LPARAM(message.LParam))); message.Result = IntPtr.Zero; } break; case WM_WINDOWPOSCHANGING: { WINDOWPOS wp = (WINDOWPOS)Marshal.PtrToStructure(message.LParam, typeof(WINDOWPOS)); if (!LockPositionAndSize) { // prevent the window from leaving the screen if ((wp.flags & SWP_NOMOVE) == 0) { Rectangle rect = Screen.GetWorkingArea(new Rectangle(wp.x, wp.y, wp.cx, wp.cy)); const int margin = 16; wp.x = Math.Max(wp.x, rect.Left - wp.cx + margin); wp.x = Math.Min(wp.x, rect.Right - margin); wp.y = Math.Max(wp.y, rect.Top - wp.cy + margin); wp.y = Math.Min(wp.y, rect.Bottom - margin); } // update location and fire event if ((wp.flags & SWP_NOMOVE) == 0) { if (_location.X != wp.x || _location.Y != wp.y) { _location = new Point(wp.x, wp.y); LocationChanged?.Invoke(this, EventArgs.Empty); } } // update size and fire event if ((wp.flags & SWP_NOSIZE) == 0) { if (_size.Width != wp.cx || _size.Height != wp.cy) { _size = new Size(wp.cx, wp.cy); SizeChanged?.Invoke(this, EventArgs.Empty); } } // update the size of the layered window if ((wp.flags & SWP_NOSIZE) == 0) { NativeMethods.UpdateLayeredWindow(Handle, IntPtr.Zero, IntPtr.Zero, ref _size, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, 0); } // update the position of the layered window if ((wp.flags & SWP_NOMOVE) == 0) { NativeMethods.SetWindowPos(Handle, IntPtr.Zero, _location.X, _location.Y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSENDCHANGING); } } // do not forward any move or size messages wp.flags |= SWP_NOSIZE | SWP_NOMOVE; // suppress any frame changed events wp.flags &= ~SWP_FRAMECHANGED; Marshal.StructureToPtr(wp, message.LParam, false); message.Result = IntPtr.Zero; } break; default: { base.WndProc(ref message); } break; } }
protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_WINDOWPOSCHANGED: WINDOWPOS wp = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS)); //Console.WriteLine($"{ wp.cx}"); wp.cx = 500; wp.cy = 500; wp.cx = 1000; wp.cy = 1000; Marshal.StructureToPtr(wp, m.LParam, false); break; case WM_MOVING: RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); //rc.Left = 0; //rc.Top = 100; Console.WriteLine($"Moving: {rc.Left}, {rc.Top}"); Marshal.StructureToPtr(rc, m.LParam, true); break; case WM_SIZING: RECT rc2 = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT)); if (casetype == 0) { if (prevRight != 0 && Math.Abs(prevRight - rc2.Right) > 100 && bOver == false) { //int d = (rc2.Right - prevRight) / 3; //rc2.Right = (prevRight += d); bOver = true; Console.WriteLine($"OVer ON1 :: {cursorRight}"); prevRight = 0; } if (bOver == true) { if (cursorRight != int.MinValue) { Console.WriteLine($"Size: {rc2.Right} {cursorRight} right! / {Cursor.Position.X}pos"); if (cursorRight > 0) { rc2.Right -= cursorRight; } else { rc2.Right += Math.Abs(cursorRight); } } } else { width = rc2.Right - rc2.Left; } } if (casetype == 1) { rc2.Right = rc2.Left + width; prevRight = rc2.Right; cursorRight = Cursor.Position.X - rc2.Right; } Marshal.StructureToPtr(rc2, m.LParam, true); Console.WriteLine($"Sizing: {rc2.Right}, {rc2.Bottom} :: {prevRight} : cur-{Cursor.Position.X}"); break; case WM_GETMINMAXINFO: MINMAXINFO mmi2 = (MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO)); mmi2.ptMinTrackSize.X = 400; mmi2.ptMinTrackSize.Y = 270; mmi2.ptMaxTrackSize.X = 1920; mmi2.ptMaxTrackSize.Y = 1920; //if (casetype == 0) //{ // mmi2.ptMinTrackSize.X = 400; // mmi2.ptMinTrackSize.Y = 270; // mmi2.ptMaxTrackSize.X = 1920; // mmi2.ptMaxTrackSize.Y = 1920; //} //else if (casetype == 1 && isResizing) // width moving //{ // mmi2.ptMinTrackSize.X = 400; // mmi2.ptMinTrackSize.Y = height; // mmi2.ptMaxTrackSize.X = 1920; // mmi2.ptMaxTrackSize.Y = height; //} //else if (casetype == 2 && isResizing) // height moving //{ // mmi2.ptMinTrackSize.X = width; // mmi2.ptMinTrackSize.Y = 270; // mmi2.ptMaxTrackSize.X = width; // mmi2.ptMaxTrackSize.Y = 1920; // //} Marshal.StructureToPtr(mmi2, m.LParam, true); break; } base.WndProc(ref m); }
private void ProcessWindowPosChangingSizing(ref WINDOWPOS r) { Rectangle ScreenBounds = Screen.FromHandle(this.Handle).Bounds; if (r.x == 0 && r.y == 0 && r.cx == 0 && r.cy == 0) { return; } foreach (IntPtr k in Program.snappingManager.bounds.Keys) { Rectangle v = Program.snappingManager.bounds[k]; if (k != this.Handle) { if (Math.Abs(r.x + r.cx - v.Right) < SNAPTHRESHOLD) { r.cx = v.Right - r.x; } if (Math.Abs(r.x + r.cx - v.Left) < SNAPTHRESHOLD) { r.cx = v.Left - r.x; } if (Math.Abs(r.x - v.Left) < SNAPTHRESHOLD) { int diff = r.x - v.Left; r.x = v.Left; r.cx += diff; } if (Math.Abs(r.x - v.Right) < SNAPTHRESHOLD) { r.x = v.Right; } if (Math.Abs(r.y - v.Top) < SNAPTHRESHOLD) { int diff = r.y - v.Top; r.y = v.Top; r.cy += diff; } if (Math.Abs(r.y - v.Bottom) < SNAPTHRESHOLD) { int diff = r.y - v.Bottom; r.y = v.Bottom; r.cy += diff; } if (Math.Abs(r.y + r.cy - v.Top) < SNAPTHRESHOLD) { r.cy = v.Top - r.y; } if (Math.Abs(r.y + r.cy - v.Bottom) < SNAPTHRESHOLD) { r.cy = v.Bottom - r.y; } } } }