From 4df6165fa93c7caeea86385312415e312de4d11f Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期二, 05 五月 2026 01:10:39 +0800
Subject: [PATCH] 暂存

---
 src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs |   66 +++++++++++++++++++++++++--------
 1 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs b/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs
index 21de1f7..bbda7f6 100644
--- a/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs
+++ b/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs
@@ -1,9 +1,9 @@
-using Moli.Desktop.Configuration;
 using Moli.Desktop.Diagnostics;
+using Moli.Desktop.Layout;
 
 namespace Moli.Desktop.Interop;
 
-/// <summary>Attempts to attach wallpaper windows behind desktop icons through Explorer's WorkerW layer.</summary>
+/// <summary>Attempts to attach desktop surfaces through Explorer's WorkerW or Progman desktop host.</summary>
 public sealed class WorkerWDesktopLayerStrategy : IDesktopLayerStrategy
 {
     private readonly IDiagnosticsService _diagnostics;
@@ -17,27 +17,30 @@
 
     public bool IsSupported() => NativeMethods.FindWindow("Progman", null) != IntPtr.Zero;
 
-    public bool TryAttachWallpaperWindow(IntPtr hwnd, MonitorInfo monitor, out string? error)
+    public bool TryAttachDesktopWindow(IntPtr hwnd, RectD bounds, out string? error)
     {
         error = null;
-        var workerW = FindWorkerW();
-        if (workerW == IntPtr.Zero)
+        var desktopHost = FindDesktopHostWindow(out var hostClass);
+        if (desktopHost == IntPtr.Zero)
         {
-            error = "WorkerW window was not found.";
+            error = "Explorer desktop host window was not found.";
             _diagnostics.Record(
                 DiagnosticSeverity.Warning,
                 DiagnosticCodes.DesktopLayerAttachFailed,
-                "WorkerW desktop layer was not found.",
+                "Explorer desktop host window was not found.",
                 new Dictionary<string, string> { ["strategy"] = Name });
             return false;
         }
 
-        if (NativeMethods.SetParent(hwnd, workerW) == IntPtr.Zero)
+        SetDesktopChildStyle(hwnd);
+
+        if (NativeMethods.SetParent(hwnd, desktopHost) == IntPtr.Zero)
         {
             var lastError = NativeMethods.GetLastError();
             if (lastError != 0)
             {
                 error = $"SetParent failed with {lastError}";
+                RestoreTopLevelStyle(hwnd);
                 return false;
             }
         }
@@ -45,13 +48,15 @@
         if (!NativeMethods.SetWindowPos(
             hwnd,
             IntPtr.Zero,
-            (int)monitor.Bounds.X,
-            (int)monitor.Bounds.Y,
-            (int)monitor.Bounds.Width,
-            (int)monitor.Bounds.Height,
-            NativeMethods.SwpNoZOrder | NativeMethods.SwpNoActivate | NativeMethods.SwpShowWindow))
+            (int)bounds.X,
+            (int)bounds.Y,
+            (int)bounds.Width,
+            (int)bounds.Height,
+            NativeMethods.SwpNoZOrder | NativeMethods.SwpNoActivate | NativeMethods.SwpFrameChanged | NativeMethods.SwpShowWindow))
         {
             error = $"SetWindowPos failed with {NativeMethods.GetLastError()}";
+            NativeMethods.SetParent(hwnd, IntPtr.Zero);
+            RestoreTopLevelStyle(hwnd);
             return false;
         }
 
@@ -59,15 +64,24 @@
             DiagnosticSeverity.Info,
             DiagnosticCodes.DesktopLayerStrategySelected,
             "Desktop layer strategy selected.",
-            new Dictionary<string, string> { ["strategy"] = Name });
+            new Dictionary<string, string> { ["strategy"] = Name, ["host"] = hostClass });
         return true;
     }
 
-    private static IntPtr FindWorkerW()
+    private static IntPtr FindDesktopHostWindow(out string hostClass)
     {
+        hostClass = "WorkerW";
         var progman = NativeMethods.FindWindow("Progman", null);
         if (progman != IntPtr.Zero)
         {
+            NativeMethods.SendMessageTimeout(
+                progman,
+                0x052C,
+                new IntPtr(0xD),
+                IntPtr.Zero,
+                NativeMethods.SmtoNormal,
+                1000,
+                out _);
             NativeMethods.SendMessageTimeout(
                 progman,
                 0x052C,
@@ -91,6 +105,26 @@
             return workerW == IntPtr.Zero;
         }, IntPtr.Zero);
 
-        return workerW;
+        if (workerW != IntPtr.Zero)
+        {
+            return workerW;
+        }
+
+        hostClass = "Progman";
+        return progman;
+    }
+
+    private static void SetDesktopChildStyle(IntPtr hwnd)
+    {
+        var current = unchecked((uint)NativeMethods.GetWindowLongPtr(hwnd, NativeMethods.GwlStyle).ToInt64());
+        var updated = (current | NativeMethods.WsChild | NativeMethods.WsVisible) & ~NativeMethods.WsPopup;
+        NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GwlStyle, new IntPtr(unchecked((int)updated)));
+    }
+
+    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