- 主界面移除整行「● 本机服务 运行中 · http://…」,顶部只留「已登录:xxx / 退出登录」 - UpdateServerState 只刷新菜单「服务 → 服务状态」,不再碰界面控件 - 托盘菜单的状态项改成固定的「本机服务」(原来是「服务:<地址>」), UpdateStatus 只传「运行中 / 未启动」;TrayIconService 构造不再需要 serverUrl - 运行状态统一在菜单里看,界面不再重复展示内部实现细节
121 lines
3.7 KiB
C#
121 lines
3.7 KiB
C#
using System;
|
||
using System.Drawing;
|
||
using WinForms = System.Windows.Forms;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>
|
||
/// 托盘图标(WPF 没有内置托盘,用 WinForms 的 <see cref="WinForms.NotifyIcon"/>)
|
||
///
|
||
/// 关窗口 = 缩到托盘,程序继续跑(本机服务不能因为关窗就断)。
|
||
/// 退出只能从托盘菜单「退出」。
|
||
/// </summary>
|
||
public sealed class TrayIconService : IDisposable
|
||
{
|
||
private readonly WinForms.NotifyIcon _icon;
|
||
private readonly WinForms.ToolStripMenuItem _statusItem;
|
||
private bool _disposed;
|
||
|
||
/// <summary>要显示主界面(双击托盘 / 菜单「打开主界面」)</summary>
|
||
public event Action? ShowRequested;
|
||
|
||
/// <summary>要从托盘菜单重启程序</summary>
|
||
public event Action? RestartRequested;
|
||
|
||
/// <summary>要退出程序</summary>
|
||
public event Action? ExitRequested;
|
||
|
||
public TrayIconService(string version)
|
||
{
|
||
// 服务地址属于内部实现,不在界面/托盘上展示
|
||
_statusItem = new WinForms.ToolStripMenuItem("本机服务") { Enabled = false };
|
||
|
||
var menu = new WinForms.ContextMenuStrip();
|
||
menu.Items.Add(new WinForms.ToolStripMenuItem("打开主界面", null, (_, _) => ShowRequested?.Invoke()));
|
||
menu.Items.Add(new WinForms.ToolStripMenuItem("重启软件", null, (_, _) => RestartRequested?.Invoke()));
|
||
menu.Items.Add(_statusItem);
|
||
menu.Items.Add(new WinForms.ToolStripMenuItem($"版本 {version}") { Enabled = false });
|
||
menu.Items.Add(new WinForms.ToolStripSeparator());
|
||
menu.Items.Add(new WinForms.ToolStripMenuItem("退出", null, (_, _) => ExitRequested?.Invoke()));
|
||
|
||
_icon = new WinForms.NotifyIcon
|
||
{
|
||
Icon = LoadIcon(),
|
||
Text = "云仓视频溯源软件(本机服务)",
|
||
Visible = true,
|
||
ContextMenuStrip = menu,
|
||
};
|
||
|
||
// 双击托盘 → 打开主界面
|
||
_icon.DoubleClick += (_, _) => ShowRequested?.Invoke();
|
||
}
|
||
|
||
/// <summary>更新托盘提示(服务地址 / 运行状态)</summary>
|
||
public void UpdateStatus(string text)
|
||
{
|
||
if (_disposed) return;
|
||
|
||
_statusItem.Text = text;
|
||
|
||
// NotifyIcon.Text 最多 63 个字符,超了会抛异常
|
||
var tip = $"云仓视频溯源软件\n{text}";
|
||
_icon.Text = tip.Length <= 63 ? tip : tip[..63];
|
||
}
|
||
|
||
/// <summary>冒一个气泡提示(第一次缩到托盘时用)</summary>
|
||
public void ShowBalloon(string title, string text)
|
||
{
|
||
if (_disposed) return;
|
||
|
||
try
|
||
{
|
||
_icon.BalloonTipTitle = title;
|
||
_icon.BalloonTipText = text;
|
||
_icon.ShowBalloonTip(3000);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"托盘气泡提示失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取 exe 自带的图标(csproj 里 ApplicationIcon),取不到就用系统默认
|
||
/// </summary>
|
||
private static Icon LoadIcon()
|
||
{
|
||
try
|
||
{
|
||
var path = Environment.ProcessPath;
|
||
if (!string.IsNullOrEmpty(path))
|
||
{
|
||
var icon = Icon.ExtractAssociatedIcon(path);
|
||
if (icon is not null) return icon;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
AppLog.Write($"读取程序图标失败:{ex.Message}");
|
||
}
|
||
|
||
return SystemIcons.Application;
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
if (_disposed) return;
|
||
|
||
_disposed = true;
|
||
try
|
||
{
|
||
_icon.Visible = false;
|
||
_icon.ContextMenuStrip?.Dispose();
|
||
_icon.Dispose();
|
||
}
|
||
catch
|
||
{
|
||
// 退出路径上忽略
|
||
}
|
||
}
|
||
}
|