Skip to content

Commit 802d7f0

Browse files
Merge pull request #4 from MichaelEllnebrand/development
v1.0.1
2 parents 55a5ac2 + ed00b25 commit 802d7f0

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

HotkeyHandler.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,21 @@ private void MouseHook_MouseMove(MouseHook.MSLLHOOKSTRUCT mouseStruct)
6767
{
6868
currentWindowPosition.X = mousePosition.X - moveWindowMouseOffset.X;
6969
currentWindowPosition.Y = mousePosition.Y - moveWindowMouseOffset.Y;
70-
7170
if (clampToScreen)
7271
{
7372
Screen currentScreen = Screen.FromHandle(currentWindowHandle);
74-
currentWindowPosition.X = Math.Clamp(currentWindowPosition.X, currentScreen.WorkingArea.X, currentScreen.WorkingArea.X + currentScreen.WorkingArea.Width - currentWindowWidth);
75-
currentWindowPosition.Y = Math.Clamp(currentWindowPosition.Y, currentScreen.WorkingArea.Y, currentScreen.WorkingArea.Y + currentScreen.WorkingArea.Height - currentWindowHeight);
73+
int remainingAreaX = currentScreen.WorkingArea.X + currentScreen.WorkingArea.Width - currentWindowWidth;
74+
if (remainingAreaX < currentScreen.WorkingArea.X)
75+
{
76+
remainingAreaX = currentScreen.WorkingArea.X;
77+
}
78+
int remainingAreaY = currentScreen.WorkingArea.Y + currentScreen.WorkingArea.Height - currentWindowHeight;
79+
if (remainingAreaY < currentScreen.WorkingArea.Y)
80+
{
81+
remainingAreaY = currentScreen.WorkingArea.Y;
82+
}
83+
currentWindowPosition.X = Math.Clamp(currentWindowPosition.X, currentScreen.WorkingArea.X, remainingAreaX);
84+
currentWindowPosition.Y = Math.Clamp(currentWindowPosition.Y, currentScreen.WorkingArea.Y, remainingAreaY);
7685
}
7786

7887
Window.SetWindowPosition(currentWindowHandle, currentWindowPosition.X, currentWindowPosition.Y);
@@ -137,6 +146,7 @@ private void StartMovingWindow()
137146
{
138147
isMovingWindow = true;
139148
isResizingWindow = false;
149+
Window.HandleMaximizedWindow(currentWindowHandle);
140150
SetStartingOffsets();
141151
}
142152

@@ -149,6 +159,7 @@ private void StartResizingWindow()
149159
{
150160
isMovingWindow = false;
151161
isResizingWindow = true;
162+
Window.HandleMaximizedWindow(currentWindowHandle);
152163
SetStartingOffsets();
153164
}
154165

@@ -167,7 +178,6 @@ private void SetStartingOffsets()
167178
resizeWindowsMouseOffset.X = mousePostion.X;
168179
resizeWindowsMouseOffset.Y = mousePostion.Y;
169180

170-
171181
resizeStartWidth = currentWindowRectangle.Width - currentWindowRectangle.X;
172182
resizeStartHeight = currentWindowRectangle.Height - currentWindowRectangle.Y;
173183

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
}

WindowTool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
66
<UseWindowsForms>true</UseWindowsForms>
77
<ApplicationIcon>WindowTool 128x128.ico</ApplicationIcon>
88
</PropertyGroup>

0 commit comments

Comments
 (0)