Skip to content

Commit

Permalink
Fixed issue with maximised windows.
Browse files Browse the repository at this point in the history
If maximised windows were snapped there was a chance they would "forget" where
the top of the screen was e.g. when maximised, subsequent to being snapped to
the bottom of the scrren, the window would grow to the correct size to fill the
screen but would continue to place itself halfway downt he screen. Restoring
the window prior to snapping fixes this issue.
  • Loading branch information
JMAlego committed May 2, 2021
1 parent 74cf809 commit 220fc9f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ private void SnapTop()
IntPtr focussedWindow = Native.GetForegroundWindow();
Screen windowScreen = Screen.FromHandle(focussedWindow);

// Windows gets confused if you move a maximised window, interestingly breaking the maximise button.
// To avoid any such issues we restore the window before moving it.
Native.ShowWindow(focussedWindow, Native.SW_RESTORE);

// Get window x border.
int windowBorderX = GetWindowXBorder(focussedWindow);

Expand Down Expand Up @@ -222,6 +226,10 @@ private void SnapBottom()
IntPtr focussedWindow = Native.GetForegroundWindow();
Screen windowScreen = Screen.FromHandle(focussedWindow);

// Windows gets confused if you move a maximised window, interestingly breaking the maximise button.
// To avoid any such issues we restore the window before moving it.
Native.ShowWindow(focussedWindow, Native.SW_RESTORE);

// Get window x border.
int windowBorderX = GetWindowXBorder(focussedWindow);

Expand Down
7 changes: 7 additions & 0 deletions Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public override string ToString()
public const uint SWP_ASYNCWINDOWPOS = 0x4000;
public const uint SWP_NOCOPYBITS = 0x0100;

// Same for SW_* values as above.
public const int SW_RESTORE = 9;

// Same for WM_* values as above.
public const int WM_HOTKEY = 0x0312;

Expand Down Expand Up @@ -168,5 +171,9 @@ public override string ToString()
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Snapper")]
[assembly: AssemblyCopyright("Copyright © Jacob Allen 2020")]
[assembly: AssemblyCopyright("Copyright © Jacob Allen 2020-2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.*")]
[assembly: AssemblyVersion("1.4.*")]
[assembly: NeutralResourcesLanguage("en-GB")]

0 comments on commit 220fc9f

Please sign in to comment.