Rename projects to better structure it. The aim is to try and keep it not dependant on the type of UI framework it uses thus allowing us to switch to another UI framework if we need later...

This commit is contained in:
TheXamlGuy
2024-01-06 08:49:12 +00:00
parent b380f06fbf
commit 3e88950669
67 changed files with 211 additions and 196 deletions
@@ -0,0 +1,91 @@
using System;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
using System.Runtime.InteropServices;
using Windows.Win32.UI.WindowsAndMessaging;
namespace Hyperbar.Windows.Win32;
public static class HwndExtensions
{
[Flags]
private enum WindowStyles
{
WS_EX_LAYERED = 0x80000
}
public static void SetWindowOpacity(this IntPtr hWnd,
byte value)
{
HWND hWND = new(hWnd);
WindowStyles windowLong = (WindowStyles)PInvoke.GetWindowLong(hWND, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
if (!windowLong.HasFlag(WindowStyles.WS_EX_LAYERED))
{
PInvoke.SetWindowLong(hWND, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)(windowLong | WindowStyles.WS_EX_LAYERED));
}
if (!PInvoke.SetLayeredWindowAttributes(hWND, 0u, value, LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_ALPHA))
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
public static void SetWindowStyle(this IntPtr hwnd,
WindowStyle newStyle)
{
int windowLong = PInvoke.GetWindowLong(new HWND(hwnd), WINDOW_LONG_PTR_INDEX.GWL_STYLE);
if (PInvoke.SetWindowLong(new HWND(hwnd), WINDOW_LONG_PTR_INDEX.GWL_STYLE, (int)newStyle) != windowLong)
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
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,
WindowPlacement 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 WindowPlacement.Left:
left = 0;
top = (info.rcWork.bottom + info.rcWork.top) / 2 - actualHeight / 2;
break;
case WindowPlacement.Top:
left = (info.rcWork.left + info.rcWork.right) / 2 - actualWidth / 2;
top = 0;
break;
case WindowPlacement.Right:
left = info.rcWork.left + info.rcWork.right - actualWidth;
top = (info.rcWork.bottom + info.rcWork.top) / 2 - actualHeight / 2;
break;
case WindowPlacement.Bottom:
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);
}
}
@@ -0,0 +1,37 @@
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using Windows.UI.Popups;
using WinRT.Interop;
namespace Hyperbar.Windows.Win32;
public static class WindowExtensions
{
public static IntPtr GetHandle(this Window window) =>
window is not null ? WindowNative.GetWindowHandle(window) : default;
public static void SetIsShownInSwitchers(this Window window,
bool value) => window.AppWindow.IsShownInSwitchers = value;
public static void SetOpacity(this Window window,
byte value) => window.GetHandle().SetWindowOpacity(value);
public static void SetStyle(this Window window,
WindowStyle style) => window.GetHandle().SetWindowStyle(style);
public static void SetTopMost(this Window window,
bool value)
{
AppWindow appWindow = window.AppWindow;
if (appWindow.Presenter is OverlappedPresenter presenter)
{
presenter.IsAlwaysOnTop = value;
}
}
public static void Snap(this Window window,
WindowPlacement placement,
double? width = null,
double? height = null) => window.GetHandle().SnapWindow(placement, width, height);
}
@@ -0,0 +1,9 @@
namespace Hyperbar.Windows.Win32;
public enum WindowPlacement
{
Left,
Top,
Right,
Bottom
}
@@ -0,0 +1,33 @@
using System;
namespace Hyperbar.Windows.Win32;
[Flags]
public enum WindowStyle
{
Border = 0x800000,
Caption = 0xC00000,
Child = 0x40000000,
ChildWindow = 0x40000000,
ChildChildren = 0x2000000,
ClipSiblings = 0x4000000,
Disabled = 0x8000000,
DlgFrame = 0x400000,
Group = 0x20000,
HScroll = 0x100000,
Iconic = 0x20000000,
Maximize = 0x1000000,
MaximizeBox = 0x10000,
Minimize = 0x20000000,
MinimizeBox = 0x20000,
Overlapped = 0,
OverlappedWindow = 0xCF0000,
SizeBox = 0x40000,
SysMenu = 0x80000,
TabStop = 0x10000,
ThickFrame = 0x40000,
Tiled = 0,
TiledWindow = 0xCF0000,
Visible = 0x10000000,
VScroll = 0x200000
}