Add HotKeyListener

This commit is contained in:
Daniel Clark
2025-03-04 17:00:00 +00:00
parent dad7c54f0d
commit cee6557fb4
19 changed files with 424 additions and 297 deletions
+8
View File
@@ -0,0 +1,8 @@
using Rect = Windows.Foundation.Rect;
namespace Toolkit.UI.WinUI;
public static class RectExtensions
{
public static Rect ToWindowsRect(this Windows.Rect rect) => new(rect.X, rect.Y, rect.Width, rect.Height);
}
+42 -23
View File
@@ -8,9 +8,10 @@ using System.Drawing;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.WindowsAndMessaging;
using Toolkit.Windows;
using Rect = Windows.Foundation.Rect;
using WinUIEx;
using Windows.Graphics;
using Rect = Windows.Foundation.Rect;
using System;
namespace Toolkit.UI.WinUI;
@@ -18,6 +19,14 @@ public static partial class WindowExtensions
{
private static SUBCLASSPROC? SubClassDelegate;
public static uint GetDpi(this Window window)
{
nint handle = WindowNative.GetWindowHandle(window);
if (handle == 0) return 0;
return PInvoke.GetDpiForWindow(new HWND(handle));
}
public static void Hide(this Window window)
{
nint handle = WindowNative.GetWindowHandle(window);
@@ -26,7 +35,7 @@ public static partial class WindowExtensions
WindowHelper.HideWindow(new HWND(handle));
}
public static void IsShownInSwitchers(this Window window,
public static void SetIsShownInSwitchers(this Window window,
bool value)
{
if (window.AppWindow is AppWindow appWindow)
@@ -35,17 +44,6 @@ public static partial class WindowExtensions
}
}
public static void SetSize(this Window window,
int width,
int height)
{
nint handle = WindowNative.GetWindowHandle(window);
if (handle == 0) return;
float value = PInvoke.GetDpiForWindow(new HWND(handle)) / 96f;
window.AppWindow.Resize(new SizeInt32((int)(width * (double)value), (int)(height * (double)value)));
}
public static void MoveAndResize(this Window window,
Rect rect)
{
@@ -87,19 +85,32 @@ public static partial class WindowExtensions
nint handle = WindowNative.GetWindowHandle(window);
if (handle == 0) return;
WindowHelper.SetForegroundWindow(new HWND(handle));
PInvoke.SetForegroundWindow(new HWND(handle));
}
public static void SetTopMost(this Window window,
bool value)
public static void SetIsMaximizable(this Window window, bool value) =>
window.UpdateOverlappedPresenter(presenter => presenter.IsMaximizable = value);
public static void SetIsMinimizable(this Window window, bool value) =>
window.UpdateOverlappedPresenter(presenter => presenter.IsMinimizable = value);
public static void SetIsResizable(this Window window, bool value) =>
window.UpdateOverlappedPresenter(presenter => presenter.IsResizable = value);
public static void SetSize(this Window window,
int width,
int height)
{
if (window.AppWindow is AppWindow appWindow &&
appWindow.Presenter is OverlappedPresenter presenter)
{
presenter.IsAlwaysOnTop = value;
}
nint handle = WindowNative.GetWindowHandle(window);
if (handle == 0) return;
float value = PInvoke.GetDpiForWindow(new HWND(handle)) / 96f;
window.AppWindow.Resize(new SizeInt32((int)(width * (double)value), (int)(height * (double)value)));
}
public static void SetIsTopMost(this Window window, bool value) =>
window.UpdateOverlappedPresenter(presenter => presenter.IsAlwaysOnTop = value);
public static void SetTransparency(this Window window,
bool value)
{
@@ -136,7 +147,6 @@ public static partial class WindowExtensions
WINDOW_EX_STYLE exStyle = (WINDOW_EX_STYLE)PInvoke.GetWindowLong(hWnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
_ = PInvoke.SetWindowLong(hWnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)(exStyle & ~WINDOW_EX_STYLE.WS_EX_LAYERED));
}
private static unsafe void EnableTransparency(HWND hWnd)
{
SubClassDelegate = new SUBCLASSPROC(WindowSubClass);
@@ -147,12 +157,21 @@ public static partial class WindowExtensions
(int)(exStyle | WINDOW_EX_STYLE.WS_EX_LAYERED));
COLORREF blackColor = new COLORREF((uint)ToWin32(Color.Black));
_ = PInvoke.SetLayeredWindowAttributes(hWnd, blackColor, 0, LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_COLORKEY |
_ = PInvoke.SetLayeredWindowAttributes(hWnd, blackColor, 0, LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_COLORKEY |
LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_ALPHA);
}
private static int ToWin32(Color c) => c.B << 16 | c.G << 8 | c.R;
private static void UpdateOverlappedPresenter(this Window window,
Action<OverlappedPresenter> action)
{
if (window.AppWindow.Presenter is OverlappedPresenter presenter)
{
action(presenter);
}
}
private static unsafe LRESULT WindowSubClass(HWND hWnd,
uint uMsg, WPARAM wParam, LPARAM lParam, nuint uIdSubclass, nuint dwRefData)
{