基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-02 690cf73d6100e93d6ae7ca95dededbad4e40eeed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Moli.Desktop.Configuration;
using Moli.Desktop.Diagnostics;
 
namespace Moli.Desktop.Interop;
 
/// <summary>Attempts to attach wallpaper windows behind desktop icons through Explorer's WorkerW layer.</summary>
public sealed class WorkerWDesktopLayerStrategy : IDesktopLayerStrategy
{
    private readonly IDiagnosticsService _diagnostics;
 
    public WorkerWDesktopLayerStrategy(IDiagnosticsService diagnostics)
    {
        _diagnostics = diagnostics;
    }
 
    public string Name => "WorkerW";
 
    public bool IsSupported() => NativeMethods.FindWindow("Progman", null) != IntPtr.Zero;
 
    public bool TryAttachWallpaperWindow(IntPtr hwnd, MonitorInfo monitor, out string? error)
    {
        error = null;
        var workerW = FindWorkerW();
        if (workerW == IntPtr.Zero)
        {
            error = "WorkerW window was not found.";
            _diagnostics.Record(
                DiagnosticSeverity.Warning,
                DiagnosticCodes.DesktopLayerAttachFailed,
                "WorkerW desktop layer was not found.",
                new Dictionary<string, string> { ["strategy"] = Name });
            return false;
        }
 
        if (NativeMethods.SetParent(hwnd, workerW) == IntPtr.Zero)
        {
            var lastError = NativeMethods.GetLastError();
            if (lastError != 0)
            {
                error = $"SetParent failed with {lastError}";
                return false;
            }
        }
 
        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))
        {
            error = $"SetWindowPos failed with {NativeMethods.GetLastError()}";
            return false;
        }
 
        _diagnostics.Record(
            DiagnosticSeverity.Info,
            DiagnosticCodes.DesktopLayerStrategySelected,
            "Desktop layer strategy selected.",
            new Dictionary<string, string> { ["strategy"] = Name });
        return true;
    }
 
    private static IntPtr FindWorkerW()
    {
        var progman = NativeMethods.FindWindow("Progman", null);
        if (progman != IntPtr.Zero)
        {
            NativeMethods.SendMessageTimeout(
                progman,
                0x052C,
                IntPtr.Zero,
                IntPtr.Zero,
                NativeMethods.SmtoNormal,
                1000,
                out _);
        }
 
        IntPtr workerW = IntPtr.Zero;
        NativeMethods.EnumWindows((topHandle, _) =>
        {
            var shellView = NativeMethods.FindWindowEx(topHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
            if (shellView == IntPtr.Zero)
            {
                return true;
            }
 
            workerW = NativeMethods.FindWindowEx(IntPtr.Zero, topHandle, "WorkerW", null);
            return workerW == IntPtr.Zero;
        }, IntPtr.Zero);
 
        return workerW;
    }
}