需求:服务跟随软件运行,不需要单独的启停;唤醒地址由插件生成,界面不必提供复制。 - MainWindow.xaml:删除「停止服务/启动服务」与「复制唤醒地址」按钮; 第③行只保留协议状态文字;标题栏的圆点 + 「运行中 · 地址」继续显示服务状态。 - MainWindow.xaml.cs: - 删除 StartStop_Click(不再允许手动停服务:停了网页看到的现象和"未安装"一模一样); - 删除 CopyLaunchUrl_Click; - UpdateServerState 只刷新状态点/文字;移除 ServiceStateChanged 事件; - 顺手修复「在浏览器打开」指向了已删除的 /api/health → 改为根路径说明页。 - TrayIconService:菜单新增「重启软件」(打开主界面 / 重启软件 / 服务地址 / 版本 / 退出)。 - App:订阅 RestartRequested → RestartApplication(): 用 `cmd /c timeout /t 2 & start "" "<exe>"` 延迟拉起自己,再走正常退出流程 (必须先退出,否则新进程会撞单实例检测、把参数转发给正在退出的旧进程后就没了)。 - README:行为表补「托盘右键重启软件」,并说明主界面不提供启停/复制唤醒地址的原因。 验证:打包 1.0.5 安装后,主界面控件只剩「退出登录 / 复制地址 / 在浏览器打开 / 查询」等, 无启停与复制唤醒地址;协议状态文字正常;托盘菜单代码含「重启软件」。
120 lines
3.7 KiB
C#
120 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 serverUrl, string version)
|
||
{
|
||
_statusItem = new WinForms.ToolStripMenuItem($"服务:{serverUrl}") { 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
|
||
{
|
||
// 退出路径上忽略
|
||
}
|
||
}
|
||
}
|