基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 574a6a769115034f37bdecf3284545d74843816a
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
using System.Windows;
using System.Windows.Interop;
 
namespace Moli.Desktop.Interop;
 
public sealed class DisplayChangeMonitor : IDisposable
{
    private HwndSource? _source;
    private readonly SemaphoreSlim _singleFlight = new(1, 1);
    private readonly int _debounceMs;
    private CancellationTokenSource? _debounceCts;
    private bool _disposed;
 
    public event Func<Task>? DisplayChanged;
 
    public DisplayChangeMonitor(int debounceMs = 500)
    {
        _debounceMs = debounceMs;
    }
 
    public void Start()
    {
        if (_source is not null)
        {
            return;
        }
 
        try
        {
            var parameters = new HwndSourceParameters("MoliDisplayChangeMonitor")
            {
                WindowStyle = 0,
                ExtendedWindowStyle = 0,
                ParentWindow = NativeMethods.HwndMessage,
            };
 
            _source = new HwndSource(parameters);
            _source.AddHook(WndProc);
        }
        catch (InvalidOperationException)
        {
            _source = null;
        }
    }
 
    public void Stop()
    {
        if (_source is null)
        {
            return;
        }
 
        _source.RemoveHook(WndProc);
        _source.Dispose();
        _source = null;
    }
 
    public void Dispose()
    {
        if (_disposed)
        {
            return;
        }
 
        _disposed = true;
        Stop();
        _debounceCts?.Cancel();
        _debounceCts?.Dispose();
        _singleFlight.Dispose();
    }
 
    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == NativeMethods.WmDisplayChange
            || msg == NativeMethods.WmSettingChange
            || msg == NativeMethods.WmDeviceChange)
        {
            OnDisplayChanged();
        }
 
        return IntPtr.Zero;
    }
 
    private async void OnDisplayChanged()
    {
        _debounceCts?.Cancel();
        _debounceCts?.Dispose();
        _debounceCts = new CancellationTokenSource();
        var token = _debounceCts.Token;
 
        try
        {
            await Task.Delay(_debounceMs, token).ConfigureAwait(false);
        }
        catch (OperationCanceledException)
        {
            return;
        }
 
        await _singleFlight.WaitAsync(CancellationToken.None).ConfigureAwait(false);
        try
        {
            if (DisplayChanged is not null)
            {
                await DisplayChanged.Invoke().ConfigureAwait(false);
            }
        }
        finally
        {
            _singleFlight.Release();
        }
    }
}