fix: correct maximised size

This commit is contained in:
Anna 2021-02-26 01:59:44 -05:00
parent 945e3a4f6f
commit fda68a8f23
2 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,98 @@
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace XIVChat_Desktop {
public static class MaximiseHelper {
public static void FixMaximise(object? sender, EventArgs e) {
if (!(sender is Window window)) {
return;
}
var handle = new WindowInteropHelper(window).Handle;
HwndSource.FromHwnd(handle)?.AddHook(WindowProc);
}
private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
switch (msg) {
case 0x0024:
WmGetMinMaxInfo(hwnd, lParam);
break;
}
return IntPtr.Zero;
}
private static void WmGetMinMaxInfo(IntPtr hwnd, IntPtr lParam) {
GetCursorPos(out var mousePos);
var currentScreenPtr = MonitorFromPoint(mousePos, MonitorOptions.MONITOR_DEFAULTTONEAREST);
var lMmi = (MINMAXINFO) Marshal.PtrToStructure(lParam, typeof(MINMAXINFO))!;
var currentScreen = new MONITORINFO();
if (!GetMonitorInfo(currentScreenPtr, currentScreen)) {
return;
}
// x,y coords when maximised
lMmi.ptMaxPosition.X = currentScreen.rcWork.Left - currentScreen.rcMonitor.Left;
lMmi.ptMaxPosition.Y = currentScreen.rcWork.Top - currentScreen.rcMonitor.Top;
// max width and height allowed
lMmi.ptMaxSize.X = currentScreen.rcWork.Right - currentScreen.rcWork.Left;
lMmi.ptMaxSize.Y = currentScreen.rcWork.Bottom - currentScreen.rcWork.Top;
Marshal.StructureToPtr(lMmi, lParam, true);
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(out POINT lpPoint);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr MonitorFromPoint(POINT pt, MonitorOptions dwFlags);
private enum MonitorOptions : uint {
MONITOR_DEFAULTTONULL = 0x00000000,
MONITOR_DEFAULTTOPRIMARY = 0x00000001,
MONITOR_DEFAULTTONEAREST = 0x00000002,
}
[DllImport("user32.dll")]
private static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
[StructLayout(LayoutKind.Sequential)]
public struct POINT {
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public struct MINMAXINFO {
public POINT ptReserved;
public POINT ptMaxSize;
public POINT ptMaxPosition;
public POINT ptMinTrackSize;
public POINT ptMaxTrackSize;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MONITORINFO {
public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
public RECT rcMonitor = new();
public RECT rcWork = new();
public int dwFlags = 0;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left, Top, Right, Bottom;
}
}
}

View File

@ -14,6 +14,7 @@ namespace XIVChat_Desktop {
this.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Fixed);
this.ContentRendered += this.FixRendering;
this.Loaded += MaximiseHelper.FixMaximise;
}
private void FixRendering(object? sender, EventArgs eventArgs) {