From df6a437518d89eebfa1a583e09ec99c462e64e47 Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期三, 12 八月 2026 20:42:12 +0800
Subject: [PATCH] 移除发布文件的跟踪

---
 src/Moli.Desktop/Interop/Win32InteropService.cs |   74 ++++++++++++++++++++++++++++--------
 1 files changed, 57 insertions(+), 17 deletions(-)

diff --git a/src/Moli.Desktop/Interop/Win32InteropService.cs b/src/Moli.Desktop/Interop/Win32InteropService.cs
index bf066f9..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;
             }
@@ -79,6 +78,7 @@
 
     public void RestoreTopLevelWindow(IntPtr hwnd)
     {
+        RestoreTopLevelStyle(hwnd);
         NativeMethods.SetParent(hwnd, IntPtr.Zero);
         var error = NativeMethods.GetLastError();
         if (error != 0)
@@ -88,6 +88,18 @@
                 DiagnosticCodes.Win32InteropFailed,
                 "RestoreTopLevelWindow failed.",
                 new Dictionary<string, string> { ["hwnd"] = hwnd.ToString("X"), ["error"] = error.ToString() });
+        }
+
+        if (!NativeMethods.SetWindowPos(
+            hwnd,
+            IntPtr.Zero,
+            0,
+            0,
+            0,
+            0,
+            NativeMethods.SwpNoMove | NativeMethods.SwpNoSize | NativeMethods.SwpNoZOrder | NativeMethods.SwpNoActivate | NativeMethods.SwpFrameChanged))
+        {
+            RecordWin32Failure("RestoreTopLevelWindow.SetWindowPos", hwnd);
         }
     }
 
@@ -126,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);
@@ -147,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
             }];
     }
 
@@ -186,4 +219,11 @@
     {
         return new RectD(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
     }
+
+    private static void RestoreTopLevelStyle(IntPtr hwnd)
+    {
+        var current = unchecked((uint)NativeMethods.GetWindowLongPtr(hwnd, NativeMethods.GwlStyle).ToInt64());
+        var updated = (current | NativeMethods.WsPopup | NativeMethods.WsVisible) & ~NativeMethods.WsChild;
+        NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GwlStyle, new IntPtr(unchecked((int)updated)));
+    }
 }

--
Gitblit v1.9.1