基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 da97410f48a1e94d7e1fe019c69a7c847fe67ddf
src/Moli.Desktop/Windows/WidgetWindow.cs
@@ -13,6 +13,8 @@
using Moli.Desktop.Runtime;
using Moli.Desktop.WebView;
using Brushes = System.Windows.Media.Brushes;
using Color = System.Windows.Media.Color;
using SolidColorBrush = System.Windows.Media.SolidColorBrush;
using Cursors = System.Windows.Input.Cursors;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
@@ -24,6 +26,13 @@
public sealed class WidgetWindow : Window
{
    private const double ResizeGripSize = 18;
    /// <summary>
    /// WPF AllowsTransparency uses UpdateLayeredWindow hit-testing against the WPF alpha
    /// bitmap. Fully transparent pixels are skipped even when WS_EX_TRANSPARENT is off, so a
    /// Transparent background over WebView2 (HwndHost) makes the widget look like a sticker.
    /// Alpha=1 keeps hits while remaining visually invisible.
    /// </summary>
    private static readonly SolidColorBrush HitTestBackground = CreateHitTestBackground();
    private readonly PluginDescriptor _plugin;
    private readonly IWebViewHost _webView;
    private readonly IWin32InteropService _interop;
@@ -41,6 +50,7 @@
    private RectD _startBounds;
    private bool _resizing;
    private DisplayLayer? _lastDisplayLayer;
    private bool _desktopLayerAttached;
    public WidgetWindow(
        PluginDescriptor plugin,
@@ -63,12 +73,12 @@
        WindowStyle = WindowStyle.None;
        ResizeMode = ResizeMode.NoResize;
        AllowsTransparency = true;
        Background = Brushes.Transparent;
        Background = HitTestBackground;
        ShowInTaskbar = false;
        Topmost = false;
        SizeToContent = SizeToContent.Manual;
        var root = new Grid { Background = Brushes.Transparent };
        var root = new Grid { Background = HitTestBackground };
        root.Children.Add(webView.View);
        _editBorder = new Border
        {
@@ -298,6 +308,13 @@
        return overlapX * overlapY;
    }
    private static SolidColorBrush CreateHitTestBackground()
    {
        var brush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
        brush.Freeze();
        return brush;
    }
    private void ApplyWin32Styles()
    {
        var hwnd = _interop.GetWindowHandle(this);
@@ -313,7 +330,14 @@
            _interop.SetClickThrough(hwnd, !_editMode && !_safeInputMode && _layout.ClickThrough);
            var currentLayer = _layout.DisplayLayer;
            if (currentLayer == _lastDisplayLayer)
            var layerChanged = currentLayer != _lastDisplayLayer;
            var needsDesktopAttach = currentLayer == DisplayLayer.Desktop
                && _monitorInfo is not null
                && (!_desktopLayerAttached || layerChanged);
            var needsTopLevelRestore = currentLayer != DisplayLayer.Desktop
                && (layerChanged || _desktopLayerAttached);
            if (!layerChanged && !needsDesktopAttach && !needsTopLevelRestore)
            {
                return;
            }
@@ -323,24 +347,35 @@
            {
                case DisplayLayer.Desktop:
                    Topmost = false;
                    if (_monitorInfo is not null)
                    if (needsDesktopAttach)
                    {
                        _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitorInfo, _layout.Bounds);
                        _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitorInfo!, _layout.Bounds);
                        _desktopLayerAttached = true;
                        parentChanged = true;
                    }
                    break;
                case DisplayLayer.GlobalTop:
                    _interop.RestoreTopLevelWindow(hwnd);
                    if (needsTopLevelRestore)
                    {
                        _interop.RestoreTopLevelWindow(hwnd);
                        _desktopLayerAttached = false;
                        parentChanged = true;
                    }
                    Topmost = true;
                    parentChanged = true;
                    break;
                case DisplayLayer.AppTop:
                case DisplayLayer.Normal:
                default:
                    _interop.RestoreTopLevelWindow(hwnd);
                    if (needsTopLevelRestore)
                    {
                        _interop.RestoreTopLevelWindow(hwnd);
                        _desktopLayerAttached = false;
                        parentChanged = true;
                    }
                    Topmost = false;
                    parentChanged = true;
                    break;
            }