packServer/desktop/Services/TrayIconService.cs
liufumingren 51abccbaaf desktop:加登录页、托盘图标、防多开(单实例 + 参数转发)
- LoginWindow:服务器地址 / 账号 / 密码 / 记住账号 → POST /api/web/auth/login 拿 user JWT;
  本地已有令牌时先用 GET /api/web/me 验证,有效就跳过登录(窗口一闪而过),无效清掉令牌留下重登。
- TrayIconService(WinForms NotifyIcon,csproj 开 UseWindowsForms):
  关窗口 = 隐藏到托盘(本机服务继续跑),双击托盘恢复,右键菜单「打开主界面 / 服务地址 / 版本 / 退出」;
  第一次缩到托盘冒一次气泡提示。退出只走托盘「退出」(App 改 ShutdownMode=OnExplicitShutdown)。
- 防多开:SingleInstance(Local\ 命名互斥体 + 命名管道)。第二个实例不抢 17311 端口,
  把 yuncang://open?code=… 转给已运行实例(在托盘里也会被弹出来并按 code 查询)后自己退出;
  还在登录界面时先把 URL 存起来,登录完成后再查。
- 生命周期调整:本机 HTTP 服务与托盘在 App 里创建(登录前就可用),主窗口只管展示与操作;
  日志改成全局 AppLog(窗口没开时的日志也会在窗口打开后补上)。
- 新增 AppSettings / SessionStore:%LOCALAPPDATA%\YuncangTrace\settings.json、session.json
  (服务器地址默认 http://127.0.0.1:7300;换服务器不会复用旧令牌)。
- 新增 ApiClient(登录 / 校验令牌)、MainWindow「退出登录」(清令牌回登录页)。
- 程序/托盘图标:Assets/app.ico + tools/make-icon.ps1(品牌蓝 + 白色播放三角)。
- csproj 关闭 ImplicitUsings:WPF 与 WinForms 同时开启时隐式命名空间会让
  MessageBox / Application / Clipboard 等产生歧义。

⚠️ 本机没有 .NET SDK,无法编译验证;已做 XAML/XML 良构与括号平衡检查。
2026-10-17 10:23:50 +08:00

116 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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? 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(_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
{
// 退出路径上忽略
}
}
}