基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 da97410f48a1e94d7e1fe019c69a7c847fe67ddf
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
using Moli.Desktop.Configuration;
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Layout;
 
namespace Moli.Desktop.Interop;
 
/// <summary>Fallback strategy that keeps the wallpaper window at the bottom of normal Z-order.</summary>
public sealed class BottomDesktopLayerStrategy : IDesktopLayerStrategy
{
    private readonly IDiagnosticsService _diagnostics;
 
    public BottomDesktopLayerStrategy(IDiagnosticsService diagnostics)
    {
        _diagnostics = diagnostics;
    }
 
    public string Name => "BottomDesktopLayer";
 
    public bool IsSupported() => true;
 
    public bool TryAttachDesktopWindow(IntPtr hwnd, MonitorInfo monitor, RectD bounds, out string? error)
    {
        error = null;
        var scale = monitor.Scale;
        var x = (int)(bounds.X * scale);
        var y = (int)(bounds.Y * scale);
        var w = (int)(bounds.Width * scale);
        var h = (int)(bounds.Height * scale);
 
        if (!NativeMethods.SetWindowPos(
            hwnd,
            NativeMethods.HwndBottom,
            x,
            y,
            w,
            h,
            NativeMethods.SwpNoActivate | NativeMethods.SwpShowWindow))
        {
            error = $"SetWindowPos failed with {NativeMethods.GetLastError()}";
            _diagnostics.Record(
                DiagnosticSeverity.Warning,
                DiagnosticCodes.DesktopLayerAttachFailed,
                "Failed to place window at bottom desktop layer.",
                new Dictionary<string, string> { ["reason"] = error });
            return false;
        }
 
        _diagnostics.Record(
            DiagnosticSeverity.Info,
            DiagnosticCodes.DesktopLayerStrategySelected,
            "Desktop layer strategy selected.",
            new Dictionary<string, string> { ["strategy"] = Name });
        return true;
    }
}