diff --git a/HotkeyHandler.cs b/HotkeyHandler.cs index 9a5d3b7..2ce7dc1 100644 --- a/HotkeyHandler.cs +++ b/HotkeyHandler.cs @@ -67,12 +67,21 @@ private void MouseHook_MouseMove(MouseHook.MSLLHOOKSTRUCT mouseStruct) { currentWindowPosition.X = mousePosition.X - moveWindowMouseOffset.X; currentWindowPosition.Y = mousePosition.Y - moveWindowMouseOffset.Y; - if (clampToScreen) { Screen currentScreen = Screen.FromHandle(currentWindowHandle); - currentWindowPosition.X = Math.Clamp(currentWindowPosition.X, currentScreen.WorkingArea.X, currentScreen.WorkingArea.X + currentScreen.WorkingArea.Width - currentWindowWidth); - currentWindowPosition.Y = Math.Clamp(currentWindowPosition.Y, currentScreen.WorkingArea.Y, currentScreen.WorkingArea.Y + currentScreen.WorkingArea.Height - currentWindowHeight); + int remainingAreaX = currentScreen.WorkingArea.X + currentScreen.WorkingArea.Width - currentWindowWidth; + if (remainingAreaX < currentScreen.WorkingArea.X) + { + remainingAreaX = currentScreen.WorkingArea.X; + } + int remainingAreaY = currentScreen.WorkingArea.Y + currentScreen.WorkingArea.Height - currentWindowHeight; + if (remainingAreaY < currentScreen.WorkingArea.Y) + { + remainingAreaY = currentScreen.WorkingArea.Y; + } + currentWindowPosition.X = Math.Clamp(currentWindowPosition.X, currentScreen.WorkingArea.X, remainingAreaX); + currentWindowPosition.Y = Math.Clamp(currentWindowPosition.Y, currentScreen.WorkingArea.Y, remainingAreaY); } Window.SetWindowPosition(currentWindowHandle, currentWindowPosition.X, currentWindowPosition.Y); @@ -137,6 +146,7 @@ private void StartMovingWindow() { isMovingWindow = true; isResizingWindow = false; + Window.HandleMaximizedWindow(currentWindowHandle); SetStartingOffsets(); } @@ -149,6 +159,7 @@ private void StartResizingWindow() { isMovingWindow = false; isResizingWindow = true; + Window.HandleMaximizedWindow(currentWindowHandle); SetStartingOffsets(); } @@ -167,7 +178,6 @@ private void SetStartingOffsets() resizeWindowsMouseOffset.X = mousePostion.X; resizeWindowsMouseOffset.Y = mousePostion.Y; - resizeStartWidth = currentWindowRectangle.Width - currentWindowRectangle.X; resizeStartHeight = currentWindowRectangle.Height - currentWindowRectangle.Y; diff --git a/Window.cs b/Window.cs index b879bb1..0498e43 100644 --- a/Window.cs +++ b/Window.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Drawing; using System.Runtime.InteropServices; using System.Text; @@ -8,14 +9,16 @@ namespace WindowTool { internal static class Window { - const short SWP_NOMOVE = 0X2; - const short SWP_NOSIZE = 1; - const short SWP_NOZORDER = 0X4; + const int SWP_NOSIZE = 0x0001; + const int SWP_NOMOVE = 0x0002; + const int SWP_NOZORDER = 0x0004; const int SWP_SHOWWINDOW = 0x0040; + + const int SW_RESTORE = 9; - private const int GA_PARENT = 1; // Retrieves the parent window.This does not include the owner, as it does with the GetParent function. - private const int GA_ROOT = 2; // Retrieves the root window by walking the chain of parent windows. - private const int GA_ROOTOWNER = 3; // Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent. + const int GA_PARENT = 1; // Retrieves the parent window.This does not include the owner, as it does with the GetParent function. + const int GA_ROOT = 2; // Retrieves the root window by walking the chain of parent windows. + const int GA_ROOTOWNER = 3; // Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent. [DllImport("user32.dll")] private static extern IntPtr WindowFromPoint(Point point); @@ -32,12 +35,27 @@ internal static class Window [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); + [DllImport("user32.dll")] + static extern bool ShowWindow(IntPtr hWnd, int wFlags); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl); + + private struct WINDOWPLACEMENT + { + public int length; + public int flags; + public int showCmd; + public Point ptMinPosition; + public Point ptMaxPosition; + public Rectangle rcNormalPosition; + } public static IntPtr GetWindow(Point point) { IntPtr intPtr = WindowFromPoint(point); intPtr = GetAncestor(intPtr, GA_ROOT); - return intPtr; } @@ -59,6 +77,17 @@ public static Rectangle GetWindowPosition(IntPtr hWnd) return rect; } - + public static void HandleMaximizedWindow(IntPtr hWnd) + { + WINDOWPLACEMENT windowPlacement = new(); + GetWindowPlacement(hWnd, ref windowPlacement); + if (windowPlacement.showCmd == 3) + { + Rectangle windowDimentions = GetWindowPosition(hWnd); + ShowWindow(hWnd, SW_RESTORE); + SetForegroundWindow(hWnd); + SetWindowPos(hWnd, 0, windowDimentions.Top, windowDimentions.Right, windowDimentions.Width, windowDimentions.Height, SWP_SHOWWINDOW); + } + } } } diff --git a/WindowTool.csproj b/WindowTool.csproj index 5687e9c..9176271 100644 --- a/WindowTool.csproj +++ b/WindowTool.csproj @@ -2,7 +2,7 @@ WinExe - netcoreapp3.1 + net6.0-windows10.0.17763.0 true WindowTool 128x128.ico