基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-05 4df6165fa93c7caeea86385312415e312de4d11f
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Layout;
 
namespace Moli.Desktop.Interop;
 
/// <summary>Attempts to attach desktop surfaces through Explorer's WorkerW or Progman desktop host.</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 TryAttachDesktopWindow(IntPtr hwnd, RectD bounds, out string? error)
    {
        error = null;
        var desktopHost = FindDesktopHostWindow(out var hostClass);
        if (desktopHost == IntPtr.Zero)
        {
            error = "Explorer desktop host window was not found.";
            _diagnostics.Record(
                DiagnosticSeverity.Warning,
                DiagnosticCodes.DesktopLayerAttachFailed,
                "Explorer desktop host window was not found.",
                new Dictionary<string, string> { ["strategy"] = Name });
            return false;
        }
 
        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;
            }
        }
 
        if (!NativeMethods.SetWindowPos(
            hwnd,
            IntPtr.Zero,
            (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;
        }
 
        _diagnostics.Record(
            DiagnosticSeverity.Info,
            DiagnosticCodes.DesktopLayerStrategySelected,
            "Desktop layer strategy selected.",
            new Dictionary<string, string> { ["strategy"] = Name, ["host"] = hostClass });
        return true;
    }
 
    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,
                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);
 
        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)));
    }
}