Skip to content

Commit

Permalink
Merge pull request #4 from MichaelEllnebrand/development
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
MichaelEllnebrand authored Jul 18, 2023
2 parents 55a5ac2 + ed00b25 commit 802d7f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
18 changes: 14 additions & 4 deletions HotkeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -137,6 +146,7 @@ private void StartMovingWindow()
{
isMovingWindow = true;
isResizingWindow = false;
Window.HandleMaximizedWindow(currentWindowHandle);
SetStartingOffsets();
}

Expand All @@ -149,6 +159,7 @@ private void StartResizingWindow()
{
isMovingWindow = false;
isResizingWindow = true;
Window.HandleMaximizedWindow(currentWindowHandle);
SetStartingOffsets();
}

Expand All @@ -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;

Expand Down
45 changes: 37 additions & 8 deletions Window.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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);
}
}
}
}
2 changes: 1 addition & 1 deletion WindowTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>WindowTool 128x128.ico</ApplicationIcon>
</PropertyGroup>
Expand Down

0 comments on commit 802d7f0

Please sign in to comment.