From 97df0fb5ed49e37d1c8e0e62057bb72ba5c9eb6a Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期三, 12 八月 2026 17:24:49 +0800
Subject: [PATCH] 优化透明样式
---
src/Moli.Desktop/Interop/Win32InteropService.cs | 54 +++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 37 insertions(+), 17 deletions(-)
diff --git a/src/Moli.Desktop/Interop/Win32InteropService.cs b/src/Moli.Desktop/Interop/Win32InteropService.cs
index 6bed968..1b56207 100644
--- a/src/Moli.Desktop/Interop/Win32InteropService.cs
+++ b/src/Moli.Desktop/Interop/Win32InteropService.cs
@@ -22,7 +22,9 @@
public void SetClickThrough(IntPtr hwnd, bool enabled)
{
- SetStyleFlag(hwnd, NativeMethods.WsExLayered | NativeMethods.WsExTransparent, enabled, "SetClickThrough");
+ // Only toggle WS_EX_TRANSPARENT. WS_EX_LAYERED is owned by SetLayered / WPF
+ // AllowsTransparency; clearing it here would break per-pixel transparency.
+ SetStyleFlag(hwnd, NativeMethods.WsExTransparent, enabled, "SetClickThrough");
}
public void SetToolWindow(IntPtr hwnd, bool enabled)
@@ -35,19 +37,16 @@
SetStyleFlag(hwnd, NativeMethods.WsExNoActivate, enabled, "SetNoActivate");
}
- public void SetLayered(IntPtr hwnd, bool enabled, byte opacity)
+ public void SetLayered(IntPtr hwnd, bool enabled)
{
+ // Only manage the WS_EX_LAYERED style bit. Never call SetLayeredWindowAttributes here:
+ // it switches the window from WPF AllowsTransparency per-pixel alpha (UpdateLayeredWindow)
+ // to constant-alpha mode, in which transparent pixels composite as opaque black.
+ // Global opacity is handled by Window.Opacity instead.
SetStyleFlag(hwnd, NativeMethods.WsExLayered, enabled, "SetLayered");
- if (enabled && !NativeMethods.SetLayeredWindowAttributes(hwnd, 0, opacity, NativeMethods.LwaAlpha))
- {
- RecordWin32Failure("SetLayeredWindowAttributes", hwnd);
- }
}
- public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, MonitorInfo monitor)
- => MoveToDesktopLayer(hwnd, mode, monitor.Bounds);
-
- public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, RectD bounds)
+ public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, MonitorInfo monitor, RectD bounds)
{
var candidates = mode == DesktopLayerMode.WorkerW
? _strategies.OrderBy(strategy => strategy is WorkerWDesktopLayerStrategy ? 0 : 1)
@@ -60,7 +59,7 @@
continue;
}
- if (strategy.TryAttachDesktopWindow(hwnd, bounds, out var error))
+ if (strategy.TryAttachDesktopWindow(hwnd, monitor, bounds, out var error))
{
return;
}
@@ -139,16 +138,36 @@
return true;
}
- var id = string.IsNullOrWhiteSpace(native.DeviceName)
+ var scale = 1.0;
+ var hr = NativeMethods.GetDpiForMonitor(hMonitor, NativeMethods.MdtEffectiveDpi, out var dpiX, out _);
+ if (hr == 0 && dpiX > 0)
+ {
+ scale = dpiX / 96.0;
+ }
+
+ var physicalBounds = ToRectD(native.Monitor);
+ var physicalWorkArea = ToRectD(native.WorkArea);
+ var dipBounds = new RectD(
+ physicalBounds.X / scale,
+ physicalBounds.Y / scale,
+ physicalBounds.Width / scale,
+ physicalBounds.Height / scale);
+ var dipWorkArea = new RectD(
+ physicalWorkArea.X / scale,
+ physicalWorkArea.Y / scale,
+ physicalWorkArea.Width / scale,
+ physicalWorkArea.Height / scale);
+
+ var deviceName = string.IsNullOrWhiteSpace(native.DeviceName)
? $"monitor-{monitors.Count + 1}"
: native.DeviceName;
monitors.Add(new MonitorInfo
{
- Id = new MonitorId(native.Flags ? "primary" : id),
- Bounds = ToRectD(native.Monitor),
- WorkArea = ToRectD(native.WorkArea),
+ Id = new MonitorId(deviceName),
+ Bounds = dipBounds,
+ WorkArea = dipWorkArea,
IsPrimary = native.Flags,
- Scale = 1
+ Scale = scale
});
return true;
}, IntPtr.Zero);
@@ -160,7 +179,8 @@
Id = new MonitorId("primary"),
Bounds = new RectD(0, 0, SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight),
WorkArea = new RectD(0, 0, SystemParameters.WorkArea.Width, SystemParameters.WorkArea.Height),
- IsPrimary = true
+ IsPrimary = true,
+ Scale = 1
}];
}
--
Gitblit v1.9.1