WIP
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
namespace Hyperbar.Windows.Interop;
|
||||
|
||||
public enum AppBarWindowPlacement
|
||||
{
|
||||
Left,
|
||||
Top,
|
||||
Right,
|
||||
Bottom
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.Graphics.Gdi;
|
||||
@@ -30,8 +29,10 @@ public static class HwndExtensions
|
||||
SET_WINDOW_POS_FLAGS.SWP_NOZORDER);
|
||||
}
|
||||
|
||||
public static uint GetDpiForWindow(IntPtr hwnd) => PInvoke.GetDpiForWindow(new HWND(hwnd));
|
||||
|
||||
public static void SetWindowOpacity(this IntPtr hWnd,
|
||||
byte value)
|
||||
byte value)
|
||||
{
|
||||
HWND hWND = new(hWnd);
|
||||
WindowStyles windowLong = (WindowStyles)PInvoke.GetWindowLong(hWND, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
|
||||
@@ -45,6 +46,7 @@ public static class HwndExtensions
|
||||
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetWindowStyle(this IntPtr hwnd,
|
||||
WindowStyle newStyle)
|
||||
{
|
||||
@@ -56,53 +58,4 @@ public static class HwndExtensions
|
||||
|
||||
PInvoke.SetWindowPos(new HWND(hwnd), new HWND(0), 0, 0, 0, 0, SET_WINDOW_POS_FLAGS.SWP_DRAWFRAME | SET_WINDOW_POS_FLAGS.SWP_NOMOVE | SET_WINDOW_POS_FLAGS.SWP_NOOWNERZORDER | SET_WINDOW_POS_FLAGS.SWP_NOSIZE | SET_WINDOW_POS_FLAGS.SWP_NOZORDER);
|
||||
}
|
||||
|
||||
public static void SnapWindow(this IntPtr hwnd,
|
||||
int placement,
|
||||
double? width = null,
|
||||
double? height = null)
|
||||
{
|
||||
HMONITOR hwndDesktop = PInvoke.MonitorFromWindow(new(hwnd), MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST);
|
||||
MONITORINFO info = new()
|
||||
{
|
||||
cbSize = 40
|
||||
};
|
||||
|
||||
PInvoke.GetMonitorInfo(hwndDesktop, ref info);
|
||||
|
||||
uint dpi = PInvoke.GetDpiForWindow(new HWND(hwnd));
|
||||
PInvoke.GetWindowRect(new HWND(hwnd), out RECT windowRect);
|
||||
|
||||
double scalingFactor = dpi / 96d;
|
||||
int actualWidth = width.HasValue ? (int)(width * scalingFactor) : windowRect.right - windowRect.left;
|
||||
int actualHeight = height.HasValue ? (int)(height * scalingFactor) : windowRect.bottom - windowRect.top;
|
||||
|
||||
int left = 0;
|
||||
int top = 0;
|
||||
|
||||
switch (placement)
|
||||
{
|
||||
case 0:
|
||||
left = 0;
|
||||
top = (info.rcWork.bottom + info.rcWork.top) / 2 - actualHeight / 2;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
left = (info.rcWork.left + info.rcWork.right) / 2 - actualWidth / 2;
|
||||
top = 0;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
left = info.rcWork.left + info.rcWork.right - actualWidth;
|
||||
top = (info.rcWork.bottom + info.rcWork.top) / 2 - actualHeight / 2;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
left = (info.rcWork.left + info.rcWork.right) / 2 - actualWidth / 2;
|
||||
top = info.rcWork.bottom + info.rcWork.top - actualHeight;
|
||||
break;
|
||||
}
|
||||
|
||||
PInvoke.SetWindowPos(new HWND(hwnd), new HWND(), left, top, actualWidth, actualHeight, 0);
|
||||
}
|
||||
}
|
||||
@@ -56,4 +56,5 @@ CreateSolidBrush
|
||||
FillRect
|
||||
GetDC
|
||||
SendInput
|
||||
MapVirtualKey
|
||||
MapVirtualKey
|
||||
MoveWindow
|
||||
@@ -0,0 +1,111 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Foundation;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.UI.WindowsAndMessaging;
|
||||
using Windows.Win32.Graphics.Gdi;
|
||||
|
||||
namespace Hyperbar.Windows.Interop;
|
||||
|
||||
public class Screen
|
||||
{
|
||||
private const int CCHDEVICENAME = 32;
|
||||
private const int PRIMARY_MONITOR = unchecked((int)0xBAADF00D);
|
||||
private static readonly bool multiMonitorSupport;
|
||||
|
||||
private readonly IntPtr monitorHandle;
|
||||
|
||||
static Screen()
|
||||
{
|
||||
multiMonitorSupport = PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CMONITORS) != 0;
|
||||
}
|
||||
|
||||
private Screen(IntPtr monitorHandle)
|
||||
{
|
||||
if (!multiMonitorSupport || monitorHandle == PRIMARY_MONITOR)
|
||||
{
|
||||
Bounds = SystemInformationHelper.VirtualScreen;
|
||||
Primary = true;
|
||||
DeviceName = "DISPLAY";
|
||||
}
|
||||
else
|
||||
{
|
||||
MonitorData monitorData = GetMonitorData(monitorHandle);
|
||||
|
||||
Bounds = new Rect(monitorData.MonitorRect.left, monitorData.MonitorRect.top, monitorData.MonitorRect.right - monitorData.MonitorRect.left, monitorData.MonitorRect.bottom - monitorData.MonitorRect.top);
|
||||
Primary = (monitorData.Flags & (int)MonitorFlag.MONITOR_DEFAULTTOPRIMARY) != 0;
|
||||
DeviceName = monitorData.DeviceName;
|
||||
}
|
||||
|
||||
this.monitorHandle = monitorHandle;
|
||||
}
|
||||
|
||||
private enum MonitorFlag : uint
|
||||
{
|
||||
MONITOR_DEFAULTTONULL = 0,
|
||||
MONITOR_DEFAULTTOPRIMARY = 1,
|
||||
MONITOR_DEFAULTTONEAREST = 2
|
||||
}
|
||||
|
||||
public Rect Bounds { get; }
|
||||
|
||||
public string DeviceName { get; }
|
||||
|
||||
public bool Primary { get; }
|
||||
|
||||
public Rect WorkingArea => GetWorkingArea();
|
||||
|
||||
public static Screen FromHandle(IntPtr handle)
|
||||
{
|
||||
return multiMonitorSupport ? new Screen(PInvoke.MonitorFromWindow((HWND)handle, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST)) : new Screen((IntPtr)PRIMARY_MONITOR);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is not Screen monitor) return false;
|
||||
return monitorHandle == monitor.monitorHandle;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return (int)monitorHandle;
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", EntryPoint = "GetMonitorInfo", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
private static extern bool GetMonitorInfoEx(IntPtr hMonitor, ref MonitorData lpmi);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
||||
private static extern bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate);
|
||||
|
||||
private MonitorData GetMonitorData(IntPtr monitorHandle)
|
||||
{
|
||||
MonitorData monitorData = new();
|
||||
monitorData.Size = Marshal.SizeOf(monitorData);
|
||||
GetMonitorInfoEx(monitorHandle, ref monitorData);
|
||||
|
||||
return monitorData;
|
||||
}
|
||||
|
||||
private Rect GetWorkingArea()
|
||||
{
|
||||
if (!multiMonitorSupport || monitorHandle == PRIMARY_MONITOR)
|
||||
{
|
||||
return SystemInformationHelper.WorkingArea;
|
||||
}
|
||||
|
||||
MonitorData monitorData = GetMonitorData(monitorHandle);
|
||||
return new Rect(monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.top, monitorData.WorkAreaRect.right - monitorData.WorkAreaRect.left, monitorData.WorkAreaRect.bottom - monitorData.WorkAreaRect.top);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
private struct MonitorData
|
||||
{
|
||||
public int Size;
|
||||
public RECT MonitorRect;
|
||||
public RECT WorkAreaRect;
|
||||
public uint Flags;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
|
||||
public string DeviceName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Foundation;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.UI.WindowsAndMessaging;
|
||||
|
||||
namespace Hyperbar.Windows.Interop;
|
||||
|
||||
internal static class SystemInformationHelper
|
||||
{
|
||||
private const int SPI_GETWORKAREA = 48;
|
||||
|
||||
public static Rect VirtualScreen => GetVirtualScreen();
|
||||
|
||||
public static Rect WorkingArea => GetWorkingArea();
|
||||
|
||||
private static Rect GetVirtualScreen()
|
||||
{
|
||||
Size size = new(PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSCREEN),
|
||||
PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CYSCREEN));
|
||||
return new Rect(0, 0, size.Width, size.Height);
|
||||
}
|
||||
|
||||
private static Rect GetWorkingArea()
|
||||
{
|
||||
RECT rect = new();
|
||||
|
||||
SystemParametersInfo(SPI_GETWORKAREA, 0, ref rect, 0);
|
||||
return new Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto)]
|
||||
private static extern bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
using Windows.Win32.UI.Shell;
|
||||
|
||||
namespace Hyperbar.Windows.Interop;
|
||||
public class WindowMessageListener :
|
||||
IDisposable
|
||||
{
|
||||
private readonly nint hwnd;
|
||||
private SUBCLASSPROC? callback;
|
||||
|
||||
protected WindowMessageListener(IntPtr hwnd)
|
||||
{
|
||||
this.hwnd = hwnd;
|
||||
Set();
|
||||
}
|
||||
|
||||
~WindowMessageListener()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public static WindowMessageListener Create(IntPtr hwnd) => new(hwnd);
|
||||
|
||||
public void Dispose() => Remove();
|
||||
|
||||
private void Remove()
|
||||
{
|
||||
PInvoke.RemoveWindowSubclass(new HWND(hwnd), callback, 101);
|
||||
callback = null;
|
||||
}
|
||||
|
||||
private void Set()
|
||||
{
|
||||
callback = new SUBCLASSPROC(WindowProc);
|
||||
PInvoke.SetWindowSubclass(new HWND(hwnd), callback, 101, 0);
|
||||
}
|
||||
private LRESULT WindowProc(HWND hWnd, uint uMsg,
|
||||
WPARAM wParam,
|
||||
LPARAM lParam,
|
||||
nuint uIdSubclass,
|
||||
nuint dwRefData)
|
||||
{
|
||||
return PInvoke.DefSubclassProc(hWnd, uMsg, wParam, lParam);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Win32;
|
||||
using Windows.Win32.Foundation;
|
||||
|
||||
namespace Hyperbar.Windows.Interop;
|
||||
|
||||
public class WindowSnapping
|
||||
{
|
||||
private readonly nint hwnd;
|
||||
private uint callback;
|
||||
|
||||
public WindowSnapping(IntPtr hwnd)
|
||||
{
|
||||
this.hwnd = hwnd;
|
||||
InitializeAppBarWindow();
|
||||
}
|
||||
|
||||
private enum AppBarMsg : int
|
||||
{
|
||||
ABM_NEW = 0,
|
||||
ABM_REMOVE,
|
||||
ABM_QUERYPOS,
|
||||
ABM_SETPOS,
|
||||
ABM_GETSTATE,
|
||||
ABM_GETTASKBARPOS,
|
||||
ABM_ACTIVATE,
|
||||
ABM_GETAUTOHIDEBAR,
|
||||
ABM_SETAUTOHIDEBAR,
|
||||
ABM_WINDOWPOSCHANGED,
|
||||
ABM_SETSTATE
|
||||
}
|
||||
public static WindowSnapping Create(IntPtr hwnd)
|
||||
{
|
||||
return new WindowSnapping(hwnd);
|
||||
}
|
||||
|
||||
public void Snap(AppBarWindowPlacement placement, int size)
|
||||
{
|
||||
uint dpi = PInvoke.GetDpiForWindow(new HWND(hwnd));
|
||||
|
||||
double scalingFactor = dpi / 96d;
|
||||
int actualSize = (int)(size * scalingFactor);
|
||||
|
||||
Screen screen = Screen.FromHandle(hwnd);
|
||||
|
||||
APPBARDATA32 appBarData = new();
|
||||
appBarData.cbSize = (uint)Marshal.SizeOf(appBarData);
|
||||
appBarData.hWnd = new HWND(hwnd);
|
||||
appBarData.uEdge = (uint)placement;
|
||||
appBarData.rc = new RECT
|
||||
{
|
||||
left = (int)screen.Bounds.Left,
|
||||
top = (int)screen.Bounds.Top,
|
||||
right = (int)screen.Bounds.Right,
|
||||
bottom = (int)screen.Bounds.Bottom
|
||||
};
|
||||
|
||||
PInvoke.SHAppBarMessage((int)AppBarMsg.ABM_QUERYPOS, ref appBarData);
|
||||
|
||||
switch (placement)
|
||||
{
|
||||
case AppBarWindowPlacement.Top:
|
||||
appBarData.rc.bottom = appBarData.rc.top + actualSize;
|
||||
break;
|
||||
case AppBarWindowPlacement.Bottom:
|
||||
appBarData.rc.top = appBarData.rc.bottom - actualSize;
|
||||
break;
|
||||
case AppBarWindowPlacement.Left:
|
||||
appBarData.rc.right = appBarData.rc.left + actualSize;
|
||||
break;
|
||||
case AppBarWindowPlacement.Right:
|
||||
appBarData.rc.left = appBarData.rc.right - actualSize;
|
||||
break;
|
||||
default: throw new NotSupportedException();
|
||||
}
|
||||
|
||||
PInvoke.SHAppBarMessage((int)AppBarMsg.ABM_SETPOS, ref appBarData);
|
||||
|
||||
PInvoke.SetWindowPos(new HWND(hwnd), new HWND(),
|
||||
appBarData.rc.left,
|
||||
appBarData.rc.top,
|
||||
appBarData.rc.right - appBarData.rc.left,
|
||||
appBarData.rc.bottom - appBarData.rc.top, 0);
|
||||
}
|
||||
|
||||
private void InitializeAppBarWindow()
|
||||
{
|
||||
if (Environment.Is64BitProcess)
|
||||
{
|
||||
APPBARDATA64 appBarData = new();
|
||||
appBarData.cbSize = (uint)Marshal.SizeOf(appBarData);
|
||||
appBarData.hWnd = new HWND(hwnd);
|
||||
|
||||
callback = PInvoke.RegisterWindowMessage("AppBarMessage64");
|
||||
appBarData.uCallbackMessage = callback;
|
||||
_ = PInvoke.SHAppBarMessage(0, ref appBarData);
|
||||
}
|
||||
else
|
||||
{
|
||||
APPBARDATA32 appBarData = new();
|
||||
appBarData.cbSize = (uint)Marshal.SizeOf(appBarData);
|
||||
appBarData.hWnd = new HWND(hwnd);
|
||||
|
||||
callback = PInvoke.RegisterWindowMessage("AppBarMessage32");
|
||||
appBarData.uCallbackMessage = callback;
|
||||
|
||||
_ = PInvoke.SHAppBarMessage(0, ref appBarData);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user