From 20e4e8a671e6d46dab87ad41fbb405a9b60c8f7a Mon Sep 17 00:00:00 2001 From: agent Date: Sun, 29 Nov 2026 22:22:30 +0100 Subject: [PATCH 1/4] Feature: Appearance - transparency of the windows New settings Appearance_UseTransparency (default off) and Appearance_Transparency (1 - 30 %, default 10 %) in Settings > Appearance: toggle with help icon and a slider in 1 % steps, applied live. The main window, the windows of dragged-out tabs, the status window and the notifications get a uniform alpha via SetLayeredWindowAttributes (WindowTransparencyHelper) instead of AllowsTransparency, so the embedded windows (Remote Desktop, PuTTY, PowerShell, TigerVNC, WebView2) stay visible. WPF (HwndTarget) removes WS_EX_LAYERED in WM_STYLECHANGING from windows without AllowsTransparency; a window hook (called before the one of WPF) keeps it while the window should be transparent. Popups (context menus, drop-downs, tooltips) stay opaque. Opaque in screenshot mode. Co-Authored-By: Claude Opus 5.5 --- .../Resources/Strings.Designer.cs | 27 ++++ .../Resources/Strings.resx | 9 ++ .../AppearanceManager.cs | 13 ++ .../GlobalStaticConfiguration.cs | 6 + .../NETworkManager.Settings/SettingsInfo.cs | 26 +++ .../WindowTransparencyHelperTests.cs | 26 +++ .../WindowTransparencyHelper.cs | 149 ++++++++++++++++++ .../Controls/DragablzTabHostWindow.xaml.cs | 3 + Source/NETworkManager/MainWindow.xaml.cs | 13 ++ .../NETworkManager/NotificationWindow.xaml.cs | 3 + Source/NETworkManager/StatusWindow.xaml.cs | 8 + .../ViewModels/SettingsAppearanceViewModel.cs | 41 +++++ .../Views/SettingsAppearanceView.xaml | 18 +++ Website/docs/changelog/next-release.md | 4 + Website/docs/settings/appearance.md | 25 ++- 15 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 Source/NETworkManager.Tests/Utilities/WindowTransparencyHelperTests.cs create mode 100644 Source/NETworkManager.Utilities.WPF/WindowTransparencyHelper.cs diff --git a/Source/NETworkManager.Localization/Resources/Strings.Designer.cs b/Source/NETworkManager.Localization/Resources/Strings.Designer.cs index f7ad28673a..e8eb7b7a81 100644 --- a/Source/NETworkManager.Localization/Resources/Strings.Designer.cs +++ b/Source/NETworkManager.Localization/Resources/Strings.Designer.cs @@ -4720,6 +4720,15 @@ public static string EnableRulesX { } } + /// + /// Looks up a localized string similar to Enable transparency. + /// + public static string EnableTransparency { + get { + return ResourceManager.GetString("EnableTransparency", resourceCulture); + } + } + /// /// Looks up a localized string similar to Encryption. /// @@ -6936,6 +6945,15 @@ public static string HelpMessage_Tags { } } + /// + /// Looks up a localized string similar to Makes the main window, the windows of dragged-out tabs, the status window and the notifications transparent, so you can see what is behind them. Embedded tools (Remote Desktop, PuTTY, PowerShell, TigerVNC, Web Console) become transparent too.. + /// + public static string HelpMessage_Transparency { + get { + return ResourceManager.GetString("HelpMessage_Transparency", resourceCulture); + } + } + /// /// Looks up a localized string similar to Use custom themes to personalize the appearance of the application. You can edit or add theme in the "Program Folder > Themes" directory. For more details, refer to the documentation.. /// @@ -16097,6 +16115,15 @@ public static string Traceroute { } } + /// + /// Looks up a localized string similar to Transparency. + /// + public static string Transparency { + get { + return ResourceManager.GetString("Transparency", resourceCulture); + } + } + /// /// Looks up a localized string similar to Tray. /// diff --git a/Source/NETworkManager.Localization/Resources/Strings.resx b/Source/NETworkManager.Localization/Resources/Strings.resx index 4d1848e5e7..ab4154b509 100644 --- a/Source/NETworkManager.Localization/Resources/Strings.resx +++ b/Source/NETworkManager.Localization/Resources/Strings.resx @@ -5966,4 +5966,13 @@ Do you want to close all hosts? Actions + + Transparency + + + Enable transparency + + + Makes the main window, the windows of dragged-out tabs, the status window and the notifications transparent, so you can see what is behind them. Embedded tools (Remote Desktop, PuTTY, PowerShell, TigerVNC, Web Console) become transparent too. + diff --git a/Source/NETworkManager.Settings/AppearanceManager.cs b/Source/NETworkManager.Settings/AppearanceManager.cs index 7a6bc13006..5471c6bac3 100644 --- a/Source/NETworkManager.Settings/AppearanceManager.cs +++ b/Source/NETworkManager.Settings/AppearanceManager.cs @@ -195,6 +195,19 @@ public static class AppearanceManager AnimationMode.Off => false, _ => SystemParameters.ClientAreaAnimation }; + + /// + /// Transparency of the main window, the windows of dragged-out tabs, the status window and the notifications in + /// percent (0 = opaque): the setting + /// (, limited to the range of the slider) if + /// is enabled. Opaque in screenshot mode. + /// + public static int WindowTransparency => + !CommandLineManager.Current.Screenshot && SettingsManager.Current?.Appearance_UseTransparency == true + ? Math.Clamp(SettingsManager.Current.Appearance_Transparency, + GlobalStaticConfiguration.Appearance_TransparencyMin, + GlobalStaticConfiguration.Appearance_TransparencyMax) + : 0; #endregion #region Constructor diff --git a/Source/NETworkManager.Settings/GlobalStaticConfiguration.cs b/Source/NETworkManager.Settings/GlobalStaticConfiguration.cs index 93d174fdee..2e9ded7260 100644 --- a/Source/NETworkManager.Settings/GlobalStaticConfiguration.cs +++ b/Source/NETworkManager.Settings/GlobalStaticConfiguration.cs @@ -103,6 +103,12 @@ public static class GlobalStaticConfiguration public static AnimationMode Appearance_Animations => AnimationMode.System; // Medium for new installations (subtle, static); saved in the settings public static AccentGlowIntensity Appearance_AccentGlowIntensity => AccentGlowIntensity.Medium; + // Transparency of the main window, the windows of dragged-out tabs, the status window and the notifications + // (percent, 0 = opaque); more than 30 % makes the texts hard to read + public static bool Appearance_UseTransparency => false; + public static int Appearance_Transparency => 10; // % + public static int Appearance_TransparencyMin => 1; // % + public static int Appearance_TransparencyMax => 30; // % // Network public static bool Network_AddDNSSuffix => true; diff --git a/Source/NETworkManager.Settings/SettingsInfo.cs b/Source/NETworkManager.Settings/SettingsInfo.cs index fc06a6a013..7f1d7f663d 100644 --- a/Source/NETworkManager.Settings/SettingsInfo.cs +++ b/Source/NETworkManager.Settings/SettingsInfo.cs @@ -373,6 +373,32 @@ public AccentGlowIntensity Appearance_AccentGlowIntensity } } = GlobalStaticConfiguration.Appearance_AccentGlowIntensity; + public bool Appearance_UseTransparency + { + get; + set + { + if (value == field) + return; + + field = value; + OnPropertyChanged(); + } + } = GlobalStaticConfiguration.Appearance_UseTransparency; + + public int Appearance_Transparency + { + get; + set + { + if (value == field) + return; + + field = value; + OnPropertyChanged(); + } + } = GlobalStaticConfiguration.Appearance_Transparency; + public AnimationMode Appearance_Animations { get; diff --git a/Source/NETworkManager.Tests/Utilities/WindowTransparencyHelperTests.cs b/Source/NETworkManager.Tests/Utilities/WindowTransparencyHelperTests.cs new file mode 100644 index 0000000000..91ec74bdaf --- /dev/null +++ b/Source/NETworkManager.Tests/Utilities/WindowTransparencyHelperTests.cs @@ -0,0 +1,26 @@ +using NETworkManager.Utilities.WPF; +using Xunit; + +namespace NETworkManager.Tests.Utilities; + +public class WindowTransparencyHelperTests +{ + [Theory] + [InlineData(0, 255)] + [InlineData(5, 242)] + [InlineData(15, 217)] + [InlineData(30, 179)] + [InlineData(100, 0)] + public void ConvertsTransparencyToAlpha(int transparency, byte expectedAlpha) + { + Assert.Equal(expectedAlpha, WindowTransparencyHelper.GetAlpha(transparency)); + } + + [Theory] + [InlineData(-10, 255)] + [InlineData(150, 0)] + public void ClampsTransparency(int transparency, byte expectedAlpha) + { + Assert.Equal(expectedAlpha, WindowTransparencyHelper.GetAlpha(transparency)); + } +} diff --git a/Source/NETworkManager.Utilities.WPF/WindowTransparencyHelper.cs b/Source/NETworkManager.Utilities.WPF/WindowTransparencyHelper.cs new file mode 100644 index 0000000000..7e8a49baff --- /dev/null +++ b/Source/NETworkManager.Utilities.WPF/WindowTransparencyHelper.cs @@ -0,0 +1,149 @@ +using System; +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; + +namespace NETworkManager.Utilities.WPF; + +/// +/// Makes a whole window transparent (uniform alpha of a system layered window, applied by Windows / DWM). Unlike +/// (per-pixel transparency, rendered by WPF) it can be changed while the window +/// is shown and keeps the embedded windows (Remote Desktop, PuTTY, PowerShell, TigerVNC, WebView2) visible - they become +/// transparent too. +/// +/// +/// WPF removes WS_EX_LAYERED from a top-level window without in +/// WM_STYLECHANGING (HwndTarget: "prevents an external program from making us system-layered"), also when +/// it updates its render settings later. A hook of the window (called before the one of WPF) keeps the style while the +/// window should be transparent. +/// +public static class WindowTransparencyHelper +{ + // https://learn.microsoft.com/windows/win32/winmsg/extended-window-styles + private const int GWL_EXSTYLE = -20; + private const int WS_EX_LAYERED = 0x00080000; + + // https://learn.microsoft.com/windows/win32/winmsg/wm-stylechanging + private const int WM_STYLECHANGING = 0x007C; + + // https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-setlayeredwindowattributes + private const int LWA_ALPHA = 0x00000002; + + // https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-redrawwindow + private const int RDW_INVALIDATE = 0x0001; + private const int RDW_ERASE = 0x0004; + private const int RDW_ALLCHILDREN = 0x0080; + private const int RDW_FRAME = 0x0400; + + [DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")] + private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex); + + [DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")] + private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, uint flags); + + /// + /// Transparency of the window in percent that the hook keeps (0 = WPF may remove the layered style again). + /// + private static readonly DependencyProperty TransparencyProperty = + DependencyProperty.RegisterAttached("Transparency", typeof(int), typeof(WindowTransparencyHelper), + new PropertyMetadata(0)); + + /// + /// Hook of the window, added once. + /// + private static readonly DependencyProperty HookProperty = + DependencyProperty.RegisterAttached("Hook", typeof(HwndSourceHook), typeof(WindowTransparencyHelper), + new PropertyMetadata(null)); + + /// + /// Converts the transparency in percent (0 = opaque) to the alpha of the layered window (255 = opaque). + /// + /// Transparency in percent, clamped to 0 - 100. + /// Alpha value (0 - 255). + public static byte GetAlpha(int transparency) + { + var value = Math.Clamp(transparency, 0, 100); + + // Rounded half up (integer math, Math.Round would round 178.5 to 178) + return (byte)((255 * (100 - value) + 50) / 100); + } + + /// + /// Makes the window transparent ( in percent) or opaque again (0). Does nothing + /// before the window has a handle (call it in / after SourceInitialized). + /// + /// Window to change. + /// Transparency in percent (0 = opaque, removes the layered style again). + public static void Apply(Window window, int transparency) + { + var handle = new WindowInteropHelper(window).Handle; + + if (handle == IntPtr.Zero) + return; + + transparency = Math.Clamp(transparency, 0, 100); + + // The hook keeps the layered style as long as this value is > 0, so set it before the style is changed + window.SetValue(TransparencyProperty, transparency); + + if (transparency > 0 && window.GetValue(HookProperty) == null) + { + var source = HwndSource.FromHwnd(handle); + + if (source == null) + return; + + HwndSourceHook hook = (IntPtr _, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) => + WndProc(window, msg, wParam, lParam, ref handled); + + source.AddHook(hook); + window.SetValue(HookProperty, hook); + } + + var exStyle = GetWindowLongPtr(handle, GWL_EXSTYLE).ToInt64(); + var isLayered = (exStyle & WS_EX_LAYERED) != 0; + + if (transparency == 0) + { + // Opaque: remove the layered style again, so a disabled option has no side effects (WPF only allows it + // with the hook, so it was set here) + if (!isLayered) + return; + + SetWindowLongPtr(handle, GWL_EXSTYLE, new IntPtr(exStyle & ~WS_EX_LAYERED)); + + // Windows doesn't repaint the window after the layered style is removed + RedrawWindow(handle, IntPtr.Zero, IntPtr.Zero, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); + + return; + } + + if (!isLayered) + SetWindowLongPtr(handle, GWL_EXSTYLE, new IntPtr(exStyle | WS_EX_LAYERED)); + + SetLayeredWindowAttributes(handle, 0, GetAlpha(transparency), LWA_ALPHA); + } + + private static IntPtr WndProc(Window window, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + // Keep the layered style while the window should be transparent. Handled, so WPF (HwndTarget) doesn't remove + // it again. STYLESTRUCT: styleOld (offset 0), styleNew (offset 4). + if (msg != WM_STYLECHANGING || wParam.ToInt32() != GWL_EXSTYLE || (int)window.GetValue(TransparencyProperty) == 0) + return IntPtr.Zero; + + var styleNew = Marshal.ReadInt32(lParam, 4); + Marshal.WriteInt32(lParam, 4, styleNew | WS_EX_LAYERED); + + handled = true; + + return IntPtr.Zero; + } +} diff --git a/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs b/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs index e8223a1ebd..a6049c6f59 100644 --- a/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs +++ b/Source/NETworkManager/Controls/DragablzTabHostWindow.xaml.cs @@ -5,6 +5,7 @@ using NETworkManager.Models.RemoteDesktop; using NETworkManager.Settings; using NETworkManager.Utilities; +using NETworkManager.Utilities.WPF; using System; using System.Collections.Generic; using System.ComponentModel; @@ -425,6 +426,8 @@ protected override void OnSourceInitialized(EventArgs e) _hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); _hwndSource?.AddHook(HwndHook); + + WindowTransparencyHelper.Apply(this, AppearanceManager.WindowTransparency); } [DebuggerStepThrough] diff --git a/Source/NETworkManager/MainWindow.xaml.cs b/Source/NETworkManager/MainWindow.xaml.cs index 01d0f11454..c517b4b55c 100644 --- a/Source/NETworkManager/MainWindow.xaml.cs +++ b/Source/NETworkManager/MainWindow.xaml.cs @@ -10,6 +10,7 @@ using NETworkManager.Settings; using NETworkManager.Update; using NETworkManager.Utilities; +using NETworkManager.Utilities.WPF; using NETworkManager.ViewModels; using NETworkManager.Views; using System; @@ -51,6 +52,16 @@ private void SettingsManager_PropertyChanged(object sender, PropertyChangedEvent break; + // Change the transparency of the main window, the windows of dragged-out tabs, the status window and the + // notifications + case nameof(SettingsInfo.Appearance_UseTransparency): + case nameof(SettingsInfo.Appearance_Transparency): + foreach (var window in Application.Current.Windows.OfType() + .Where(x => x is MainWindow or DragablzTabHostWindow or StatusWindow or NotificationWindow)) + WindowTransparencyHelper.Apply(window, AppearanceManager.WindowTransparency); + + break; + // Update TrayIcon if changed in the settings case nameof(SettingsInfo.TrayIcon_AlwaysShowIcon): if (SettingsManager.Current.TrayIcon_AlwaysShowIcon && _notifyIcon == null) @@ -1440,6 +1451,8 @@ protected override void OnSourceInitialized(EventArgs e) _hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); _hwndSource?.AddHook(HwndHook); + WindowTransparencyHelper.Apply(this, AppearanceManager.WindowTransparency); + RegisterHotKeys(); } diff --git a/Source/NETworkManager/NotificationWindow.xaml.cs b/Source/NETworkManager/NotificationWindow.xaml.cs index 8c348d7851..70c13dd007 100644 --- a/Source/NETworkManager/NotificationWindow.xaml.cs +++ b/Source/NETworkManager/NotificationWindow.xaml.cs @@ -3,6 +3,7 @@ using NETworkManager.Models; using NETworkManager.Settings; using NETworkManager.Utilities; +using NETworkManager.Utilities.WPF; using System; using System.Collections.Generic; using System.ComponentModel; @@ -265,6 +266,8 @@ protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); + WindowTransparencyHelper.Apply(this, AppearanceManager.WindowTransparency); + ApplyPosition(); SlideIn(); diff --git a/Source/NETworkManager/StatusWindow.xaml.cs b/Source/NETworkManager/StatusWindow.xaml.cs index fcbc68a959..b937fc119a 100644 --- a/Source/NETworkManager/StatusWindow.xaml.cs +++ b/Source/NETworkManager/StatusWindow.xaml.cs @@ -1,5 +1,6 @@ using NETworkManager.Settings; using NETworkManager.Utilities; +using NETworkManager.Utilities.WPF; using NETworkManager.Views; using System; using System.ComponentModel; @@ -36,6 +37,13 @@ public StatusWindow(MainWindow mainWindow) SizeChanged += (_, _) => UpdatePosition(); } + protected override void OnSourceInitialized(EventArgs e) + { + base.OnSourceInitialized(e); + + WindowTransparencyHelper.Apply(this, AppearanceManager.WindowTransparency); + } + #endregion #region PropertyChangedEventHandler diff --git a/Source/NETworkManager/ViewModels/SettingsAppearanceViewModel.cs b/Source/NETworkManager/ViewModels/SettingsAppearanceViewModel.cs index 77847607f4..e0e3d7636f 100644 --- a/Source/NETworkManager/ViewModels/SettingsAppearanceViewModel.cs +++ b/Source/NETworkManager/ViewModels/SettingsAppearanceViewModel.cs @@ -118,6 +118,45 @@ public AccentGlowIntensity AccentGlowIntensity } } + public bool UseTransparency + { + get; + set + { + if (value == field) + return; + + if (!_isLoading) + SettingsManager.Current.Appearance_UseTransparency = value; + + field = value; + OnPropertyChanged(); + } + } + + public int TransparencyMin => GlobalStaticConfiguration.Appearance_TransparencyMin; + + public int TransparencyMax => GlobalStaticConfiguration.Appearance_TransparencyMax; + + /// + /// Transparency in percent, applied immediately while the slider is moved. + /// + public int Transparency + { + get; + set + { + if (value == field) + return; + + if (!_isLoading) + SettingsManager.Current.Appearance_Transparency = value; + + field = value; + OnPropertyChanged(); + } + } + public IEnumerable AnimationModes => Enum.GetValues(); public AnimationMode AnimationMode @@ -186,6 +225,8 @@ private void LoadSettings() .FirstOrDefault(x => x.Name == SettingsManager.Current.Appearance_CustomThemeName) ?? CustomThemes.Cast().FirstOrDefault(); AccentGlowIntensity = SettingsManager.Current.Appearance_AccentGlowIntensity; + UseTransparency = SettingsManager.Current.Appearance_UseTransparency; + Transparency = Math.Clamp(SettingsManager.Current.Appearance_Transparency, TransparencyMin, TransparencyMax); // If policy is set, show the policy value (on / off); otherwise show the user's setting AnimationMode = PolicyManager.Current?.Appearance_Animations is { } animationsPolicy ? animationsPolicy ? AnimationMode.On : AnimationMode.Off diff --git a/Source/NETworkManager/Views/SettingsAppearanceView.xaml b/Source/NETworkManager/Views/SettingsAppearanceView.xaml index e04df8e4ed..7be272f8e8 100644 --- a/Source/NETworkManager/Views/SettingsAppearanceView.xaml +++ b/Source/NETworkManager/Views/SettingsAppearanceView.xaml @@ -86,6 +86,24 @@ + + + + + +