Skip to content

Commit ed00b25

Browse files
Handle maximized windows before moving or resizing
1 parent ce8cc6e commit ed00b25

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

HotkeyHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ private void StartMovingWindow()
146146
{
147147
isMovingWindow = true;
148148
isResizingWindow = false;
149+
Window.HandleMaximizedWindow(currentWindowHandle);
149150
SetStartingOffsets();
150151
}
151152

@@ -158,6 +159,7 @@ private void StartResizingWindow()
158159
{
159160
isMovingWindow = false;
160161
isResizingWindow = true;
162+
Window.HandleMaximizedWindow(currentWindowHandle);
161163
SetStartingOffsets();
162164
}
163165

@@ -176,7 +178,6 @@ private void SetStartingOffsets()
176178
resizeWindowsMouseOffset.X = mousePostion.X;
177179
resizeWindowsMouseOffset.Y = mousePostion.Y;
178180

179-
180181
resizeStartWidth = currentWindowRectangle.Width - currentWindowRectangle.X;
181182
resizeStartHeight = currentWindowRectangle.Height - currentWindowRectangle.Y;
182183

Window.cs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Drawing;
45
using System.Runtime.InteropServices;
56
using System.Text;
@@ -8,14 +9,16 @@ namespace WindowTool
89
{
910
internal static class Window
1011
{
11-
const short SWP_NOMOVE = 0X2;
12-
const short SWP_NOSIZE = 1;
13-
const short SWP_NOZORDER = 0X4;
12+
const int SWP_NOSIZE = 0x0001;
13+
const int SWP_NOMOVE = 0x0002;
14+
const int SWP_NOZORDER = 0x0004;
1415
const int SWP_SHOWWINDOW = 0x0040;
16+
17+
const int SW_RESTORE = 9;
1518

16-
private const int GA_PARENT = 1; // Retrieves the parent window.This does not include the owner, as it does with the GetParent function.
17-
private const int GA_ROOT = 2; // Retrieves the root window by walking the chain of parent windows.
18-
private const int GA_ROOTOWNER = 3; // Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent.
19+
const int GA_PARENT = 1; // Retrieves the parent window.This does not include the owner, as it does with the GetParent function.
20+
const int GA_ROOT = 2; // Retrieves the root window by walking the chain of parent windows.
21+
const int GA_ROOTOWNER = 3; // Retrieves the owned root window by walking the chain of parent and owner windows returned by GetParent.
1922

2023
[DllImport("user32.dll")]
2124
private static extern IntPtr WindowFromPoint(Point point);
@@ -32,12 +35,27 @@ internal static class Window
3235
[DllImport("user32.dll")]
3336
private static extern bool SetForegroundWindow(IntPtr hWnd);
3437

38+
[DllImport("user32.dll")]
39+
static extern bool ShowWindow(IntPtr hWnd, int wFlags);
40+
41+
[DllImport("user32.dll")]
42+
[return: MarshalAs(UnmanagedType.Bool)]
43+
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
44+
45+
private struct WINDOWPLACEMENT
46+
{
47+
public int length;
48+
public int flags;
49+
public int showCmd;
50+
public Point ptMinPosition;
51+
public Point ptMaxPosition;
52+
public Rectangle rcNormalPosition;
53+
}
3554

3655
public static IntPtr GetWindow(Point point)
3756
{
3857
IntPtr intPtr = WindowFromPoint(point);
3958
intPtr = GetAncestor(intPtr, GA_ROOT);
40-
4159
return intPtr;
4260
}
4361

@@ -59,6 +77,17 @@ public static Rectangle GetWindowPosition(IntPtr hWnd)
5977
return rect;
6078
}
6179

62-
80+
public static void HandleMaximizedWindow(IntPtr hWnd)
81+
{
82+
WINDOWPLACEMENT windowPlacement = new();
83+
GetWindowPlacement(hWnd, ref windowPlacement);
84+
if (windowPlacement.showCmd == 3)
85+
{
86+
Rectangle windowDimentions = GetWindowPosition(hWnd);
87+
ShowWindow(hWnd, SW_RESTORE);
88+
SetForegroundWindow(hWnd);
89+
SetWindowPos(hWnd, 0, windowDimentions.Top, windowDimentions.Right, windowDimentions.Width, windowDimentions.Height, SWP_SHOWWINDOW);
90+
}
91+
}
6392
}
6493
}

0 commit comments

Comments
 (0)