基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 97df0fb5ed49e37d1c8e0e62057bb72ba5c9eb6a
src/Moli.Desktop/Windows/WallpaperWindow.cs
@@ -9,6 +9,7 @@
using Moli.Desktop.Plugins;
using Moli.Desktop.Runtime;
using Moli.Desktop.WebView;
using Brushes = System.Windows.Media.Brushes;
namespace Moli.Desktop.Windows;
@@ -22,7 +23,6 @@
    private readonly DesktopPaths _paths;
    private readonly IDiagnosticsService _diagnostics;
    private bool _clickThrough = true;
    private double _opacity = 1;
    public WallpaperWindow(
        PluginDescriptor plugin,
@@ -58,7 +58,7 @@
    public async Task LoadAsync(WallpaperRuntimeState state, CancellationToken cancellationToken = default)
    {
        _clickThrough = state.ClickThrough;
        _opacity = state.Opacity;
        Opacity = Math.Clamp(state.Opacity, 0.1, 1.0);
        await _webView.InitializeAsync(new WebViewHostOptions
        {
            UserDataFolder = Path.Combine(_paths.WebViewDataDirectory, "wallpaper", _plugin.Id.Value),
@@ -76,7 +76,6 @@
    public void ApplyState(WallpaperRuntimeState state, bool safeInputMode)
    {
        _clickThrough = !safeInputMode && state.ClickThrough;
        _opacity = state.Opacity;
        Opacity = Math.Clamp(state.Opacity, 0.1, 1.0);
        ApplyWin32Styles();
    }
@@ -123,9 +122,9 @@
        {
            _interop.SetToolWindow(hwnd, true);
            _interop.SetNoActivate(hwnd, true);
            _interop.SetLayered(hwnd, true, ToAlpha(_opacity));
            _interop.SetLayered(hwnd, true);
            _interop.SetClickThrough(hwnd, _clickThrough);
            _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitor);
            _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitor, _monitor.Bounds);
        }
        catch (Exception ex)
        {
@@ -136,6 +135,4 @@
                new Dictionary<string, string> { ["pluginId"] = _plugin.Id.Value, ["exception"] = ex.GetType().Name });
        }
    }
    private static byte ToAlpha(double opacity) => (byte)Math.Round(Math.Clamp(opacity, 0.1, 1.0) * 255);
}