Stuff for tunesync

This commit is contained in:
Dan Clark
2024-11-16 19:46:25 +00:00
parent 0865e7da89
commit b5bf17821c
27 changed files with 562 additions and 58 deletions
+6
View File
@@ -11,6 +11,8 @@
</RuntimeIdentifiers>
<UseWinUI>true</UseWinUI>
<WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
@@ -18,4 +20,8 @@
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />
<PackageReference Include="WinUIEx" Version="2.4.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Toolkit.Windows\Toolkit.Windows.csproj" />
</ItemGroup>
</Project>
+84 -13
View File
@@ -1,25 +1,96 @@
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using WinUIEx;
using Windows.Win32;
using Windows.Win32.Foundation;
using WinRT.Interop;
using Windows.Win32.Graphics.Gdi;
using System.Drawing;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.WindowsAndMessaging;
namespace Toolkit.UI.WinUI;
public static partial class WindowExtensions
{
public static void SetWindowStyle(this Window window,
WindowStyle style)
{
WinUIEx.WindowStyle windowStyle = window.GetWindowStyle();
private static SUBCLASSPROC? SubClassDelegate;
switch (style)
public static void SetBorderless(this Window window,
bool value)
{
if (window.AppWindow is AppWindow appWindow &&
appWindow.Presenter is OverlappedPresenter presenter)
{
case WindowStyle.None:
windowStyle &= ~(WinUIEx.WindowStyle.Caption |
WinUIEx.WindowStyle.ThickFrame |
WinUIEx.WindowStyle.Border |
WinUIEx.WindowStyle.SysMenu);
break;
presenter.IsMaximizable = !value;
presenter.IsMinimizable = !value;
presenter.IsResizable = !value;
presenter.SetBorderAndTitleBar(!value, !value);
}
}
public static void SetTransparency(this Window window,
bool value)
{
nint handle = WindowNative.GetWindowHandle(window);
if (handle == 0) return;
HWND hWnd = new(handle);
if (value)
{
EnableTransparency(hWnd);
}
else
{
DisableTransparency(hWnd);
}
}
private static unsafe void EnableTransparency(HWND hWnd)
{
SubClassDelegate = new SUBCLASSPROC(WindowSubClass);
_ = PInvoke.SetWindowSubclass(hWnd, SubClassDelegate, 0, 0);
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));
COLORREF blackColor = new COLORREF((uint)ToWin32(Color.Black));
_ = PInvoke.SetLayeredWindowAttributes(hWnd, blackColor, 0, LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_COLORKEY |
LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_ALPHA);
}
private static unsafe void DisableTransparency(HWND hWnd)
{
if (SubClassDelegate != null)
{
_ = PInvoke.RemoveWindowSubclass(hWnd, SubClassDelegate, 0);
SubClassDelegate = null;
}
window.SetWindowStyle(windowStyle);
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 int ToWin32(Color c) => c.B << 16 | c.G << 8 | c.R;
private static unsafe LRESULT WindowSubClass(HWND hWnd,
uint uMsg, WPARAM wParam, LPARAM lParam, nuint uIdSubclass, nuint dwRefData)
{
switch (uMsg)
{
case PInvoke.WM_ERASEBKGND:
{
RECT rect;
PInvoke.GetClientRect(hWnd, &rect);
HBRUSH hBrush = PInvoke.CreateSolidBrush(new COLORREF((uint)ToWin32(Color.Black)));
_ = PInvoke.FillRect(new HDC((nint)wParam.Value),
&rect, hBrush);
_ = PInvoke.DeleteObject(new HGDIOBJ(hBrush));
return new LRESULT(1);
}
}
return PInvoke.DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
}
-9
View File
@@ -1,9 +0,0 @@
namespace Toolkit.UI.WinUI;
public enum WindowStyle
{
None,
SingleBorderWindow,
ThreeDBorderWindow,
ToolWindow
}