From b01ea9159abeee71908a465372e5bcebd2690936 Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 25 Mar 2026 20:19:00 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9D=E3=83=83=E3=83=97=E3=82=A2=E3=83=83?= =?UTF-8?q?=E3=83=97=E3=82=A6=E3=82=A3=E3=83=B3=E3=83=89=E3=82=A6=E3=81=B8?= =?UTF-8?q?=E3=81=AEDWM=E3=83=86=E3=83=BC=E3=83=9E=E8=87=AA=E5=8B=95?= =?UTF-8?q?=E9=81=A9=E7=94=A8=E6=A9=9F=E8=83=BD=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IWindowThemeServiceインターフェースを新規作成し、Bind(Window)メソッドを定義しました。 WindowThemeServiceクラスを実装し、SourceInitializedおよびLoadedイベントにフックしてウィンドウ生成時に自動でテーマを適用するようにしました。 テーマ適用はWindows11以降のみを対象とし、DWM APIのDwmSetWindowAttributeを用いてキャプションバー・ボーダー・テキストの色をWPFのシステムカラーリソースから取得して設定しました。 WindowThemeRegistry静的クラスを導入し、Null Objectパターンにより未登録時でも安全に動作するNullWindowThemeServiceをデフォルト実装として保持するようにしました。 RegisterメソッドはInterlocked.Exchangeでスレッドセーフな差し替えを保証するようにしました。 PluginHostの初期化処理にてWindowThemeServiceをWindowThemeRegistryへ登録し、GenerationPopupView生成直後にWindowThemeRegistry.Bindを呼び出してポップアップウィンドウをテーマサービスに紐付けるようにしました。 --- ProxyForge/Interfaces/IWindowThemeService.cs | 8 ++++ ProxyForge/Plugin/PluginHost.cs | 3 ++ ProxyForge/Services/WindowThemeRegistry.cs | 22 +++++++++ ProxyForge/Services/WindowThemeService.cs | 47 ++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 ProxyForge/Interfaces/IWindowThemeService.cs create mode 100644 ProxyForge/Services/WindowThemeRegistry.cs create mode 100644 ProxyForge/Services/WindowThemeService.cs diff --git a/ProxyForge/Interfaces/IWindowThemeService.cs b/ProxyForge/Interfaces/IWindowThemeService.cs new file mode 100644 index 0000000..35b706f --- /dev/null +++ b/ProxyForge/Interfaces/IWindowThemeService.cs @@ -0,0 +1,8 @@ +using System.Windows; + +namespace ProxyForge.Interfaces; + +internal interface IWindowThemeService +{ + void Bind(Window window); +} \ No newline at end of file diff --git a/ProxyForge/Plugin/PluginHost.cs b/ProxyForge/Plugin/PluginHost.cs index 06ac661..180be30 100644 --- a/ProxyForge/Plugin/PluginHost.cs +++ b/ProxyForge/Plugin/PluginHost.cs @@ -3,6 +3,7 @@ using ProxyForge.Core; using ProxyForge.Detection; using ProxyForge.Interfaces; using ProxyForge.Memory; +using ProxyForge.Services; using ProxyForge.Settings; using ProxyForge.Streaming; using ProxyForge.ViewModels; @@ -60,6 +61,7 @@ internal sealed class PluginHost : IDisposable var chunkAllocator = new ChunkAllocator(65536); MfSession.AddRef(); + WindowThemeRegistry.Register(new WindowThemeService()); IProxyEncoderFactory encoderFactory = new MfProxyEncoderFactory( budget, @@ -124,6 +126,7 @@ internal sealed class PluginHost : IDisposable { var vm = new GenerationPopupViewModel(CacheManager.ActiveGenerations); _popupView = new GenerationPopupView(vm); + WindowThemeRegistry.Bind(_popupView); var mainWindow = Application.Current?.MainWindow; if (mainWindow is { IsLoaded: true }) diff --git a/ProxyForge/Services/WindowThemeRegistry.cs b/ProxyForge/Services/WindowThemeRegistry.cs new file mode 100644 index 0000000..33085a8 --- /dev/null +++ b/ProxyForge/Services/WindowThemeRegistry.cs @@ -0,0 +1,22 @@ +using ProxyForge.Interfaces; +using System.Windows; + +namespace ProxyForge.Services; + +internal static class WindowThemeRegistry +{ + private static IWindowThemeService _service = NullWindowThemeService.Instance; + + internal static void Register(IWindowThemeService service) + => Interlocked.Exchange(ref _service, service ?? NullWindowThemeService.Instance); + + internal static void Bind(Window window) + => _service.Bind(window); + + private sealed class NullWindowThemeService : IWindowThemeService + { + internal static readonly NullWindowThemeService Instance = new(); + private NullWindowThemeService() { } + public void Bind(Window window) { } + } +} \ No newline at end of file diff --git a/ProxyForge/Services/WindowThemeService.cs b/ProxyForge/Services/WindowThemeService.cs new file mode 100644 index 0000000..731611b --- /dev/null +++ b/ProxyForge/Services/WindowThemeService.cs @@ -0,0 +1,47 @@ +using ProxyForge.Interfaces; +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; +using System.Windows.Media; + +namespace ProxyForge.Services; + +internal sealed class WindowThemeService : IWindowThemeService +{ + public void Bind(Window window) + { + if (window is null) return; + window.SourceInitialized += (_, _) => ApplyCurrentTheme(window); + window.Loaded += (_, _) => ApplyCurrentTheme(window); + } + + private static void ApplyCurrentTheme(Window window) + { + if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000)) return; + var hwnd = new WindowInteropHelper(window).Handle; + if (hwnd == nint.Zero) return; + + var captionBrush = (window.TryFindResource(SystemColors.ControlBrushKey) as SolidColorBrush) ?? Brushes.White; + var textBrush = (window.TryFindResource(SystemColors.WindowTextBrushKey) as SolidColorBrush) ?? Brushes.Black; + + SetDwmColor(hwnd, NativeMethods.DwmwaCaption, captionBrush.Color); + SetDwmColor(hwnd, NativeMethods.DwmwaBorder, captionBrush.Color); + SetDwmColor(hwnd, NativeMethods.DwmwaText, textBrush.Color); + } + + private static void SetDwmColor(nint hwnd, uint attribute, Color color) + { + uint colorRef = color.R | ((uint)color.G << 8) | ((uint)color.B << 16); + NativeMethods.DwmSetWindowAttribute(hwnd, attribute, ref colorRef, sizeof(uint)); + } + + private static class NativeMethods + { + internal const uint DwmwaCaption = 35; + internal const uint DwmwaBorder = 34; + internal const uint DwmwaText = 36; + + [DllImport("dwmapi.dll")] + internal static extern int DwmSetWindowAttribute(nint hwnd, uint dwAttribute, ref uint pvAttribute, uint cbAttribute); + } +} \ No newline at end of file