基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-07-29 cf40cded1708bb303be4a404b7aceddd196e1d46
src/Moli.Desktop/Tray/TrayShell.cs
@@ -44,7 +44,7 @@
    {
        _notifyIcon = new Forms.NotifyIcon
        {
            Icon = Drawing.SystemIcons.Application,
            Icon = LoadTrayIcon(),
            Text = "Moli Desktop",
            Visible = true,
            ContextMenuStrip = BuildMenu()
@@ -157,4 +157,35 @@
    }
    private static string TrimForTray(string text) => text.Length <= 63 ? text : text[..63];
    private static Drawing.Icon LoadTrayIcon()
    {
        try
        {
            var iconPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Moli.Desktop.ico");
            if (File.Exists(iconPath))
            {
                return new Drawing.Icon(iconPath);
            }
        }
        catch (Exception)
        {
            // Fall back to the embedded executable icon below.
        }
        try
        {
            var executablePath = Environment.ProcessPath;
            if (!string.IsNullOrWhiteSpace(executablePath) && File.Exists(executablePath))
            {
                return Drawing.Icon.ExtractAssociatedIcon(executablePath) ?? Drawing.SystemIcons.Application;
            }
        }
        catch (Exception)
        {
            // Last resort keeps the tray available even if icon loading fails.
        }
        return Drawing.SystemIcons.Application;
    }
}