private void WndProc2(IntPtr hWinEventHook, uint eventType, IntPtr handle, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { if (this.handle != handle) { return; } if (eventType == (uint)Win32.EventContants.EVENT_SYSTEM_MOVESIZESTART) { //Log("Starting to move or resize window '{0}' ('{1}', '{2}', '{3}', '{4}').", handle, idObject, idChild, dwEventThread, dwmsEventTime); initialPosition = GetWindowPositionOrDefault(handle); } else if (eventType == (uint)Win32.EventContants.EVENT_SYSTEM_MOVESIZEEND) { //Log("Resized or moved window '{0}' (from '{5}') ('{1}', '{2}', '{3}', '{4}').", handle, idObject, idChild, dwEventThread, dwmsEventTime, handle); Position currentPosition = GetWindowPositionOrDefault(handle); if (currentPosition == null) { return; } Win32.RECT offset = GetWindowFrameOffset(handle, currentPosition); ResizeDirection resize = ResizeDirection.Empty; MoveDirection move = MoveDirection.Empty; if (initialPosition != null) { if (initialPosition.Left < currentPosition.Left) { move |= MoveDirection.LeftToRight; } else if (initialPosition.Left > currentPosition.Left) { move |= MoveDirection.RightToLeft; } if (initialPosition.Top < currentPosition.Top) { move |= MoveDirection.TopToBottom; } else if (initialPosition.Top > currentPosition.Top) { move |= MoveDirection.BottomToTop; } if (initialPosition.Width != currentPosition.Width) { resize |= ResizeDirection.Width; } if (initialPosition.Height != currentPosition.Height) { resize |= ResizeDirection.Height; } } //Log("User state: {0}x{1} at {2}x{3}.", currentPosition.Left, currentPosition.Top, currentPosition.Width, currentPosition.Height); StickContext left; if (resize.HasFlag(ResizeDirection.Width)) { left = new StickContext(currentPosition.Left, currentPosition.Width); } else { left = new StickContext(currentPosition.Left); } StickContext top; if (resize.HasFlag(ResizeDirection.Height)) { top = new StickContext(currentPosition.Top, currentPosition.Height); } else { top = new StickContext(currentPosition.Top); } int leftPriority = Int32.MaxValue; int topPriority = Int32.MaxValue; if (move.HasFlag(MoveDirection.RightToLeft) || move.HasFlag(MoveDirection.LeftToRight)) { TryStickLeft(left, ref leftPriority); TryStickRight(left, currentPosition, ref leftPriority); } if (move.HasFlag(MoveDirection.BottomToTop) || move.HasFlag(MoveDirection.TopToBottom)) { TryStickTop(top, ref topPriority); TryStickBottom(top, currentPosition, ref topPriority); } if (move == MoveDirection.Empty && resize.HasFlag(ResizeDirection.Width)) { TryStickRight(left, currentPosition, ref leftPriority); } if (move == MoveDirection.Empty && resize.HasFlag(ResizeDirection.Height)) { TryStickBottom(top, currentPosition, ref topPriority); } Position targetPosition = new Position( left.NewPosition - offset.Left, top.NewPosition - offset.Top, (left.IsResize ? left.NewSize : currentPosition.Width) - offset.Right + offset.Left, (top.IsResize ? top.NewSize : currentPosition.Height) - offset.Bottom + offset.Top ); //Log("Stick state: {0}x{1} at {2}x{3}.", targetPosition.Left, targetPosition.Top, targetPosition.Width, targetPosition.Height); Win32.SetWindowPos( handle, IntPtr.Zero, targetPosition.Left, targetPosition.Top, targetPosition.Width, targetPosition.Height, 0 ); } }