using System; using System.Diagnostics; using System.Drawing; using System.IO; using System.Windows.Forms; using BeiJingComService.Core; using BeiJingComService.Infrastructure; using BeiJingComService.Protocols; namespace BeiJingComService.WinForms { public partial class TrayIconForm : Form { private readonly ITransportProtocol[] _protocols; private NotifyIcon _notifyIcon; private ContextMenuStrip _contextMenu; private DiagnosticForm _diagnosticForm; private bool _exitConfirmed; public TrayIconForm(ITransportProtocol[] protocols) { _protocols = protocols; InitializeComponent(); HideFormFromTaskbar(); GatewayHost.Instance.InitializeFailed += OnGatewayInitializeFailed; GatewayHost.Instance.Reinitialized += OnGatewayReinitialized; } private void HideFormFromTaskbar() { this.WindowState = FormWindowState.Minimized; this.ShowInTaskbar = false; this.Visible = false; } private void InitializeComponent() { _contextMenu = new ContextMenuStrip(); _contextMenu.Items.Add("打开面板", null, OnOpenPanel); _contextMenu.Items.Add("HTML 调试", null, OnOpenDebugPage); _contextMenu.Items.Add("查看日志", null, OnViewLog); _contextMenu.Items.Add("重启网关", null, OnRestartGateway); _contextMenu.Items.Add(new ToolStripSeparator()); _contextMenu.Items.Add("退出", null, OnExit); _notifyIcon = new NotifyIcon { Text = "医保COM组件封装服务", Visible = true, ContextMenuStrip = _contextMenu }; _notifyIcon.Icon = LoadAppIcon(); _notifyIcon.DoubleClick += OnOpenPanel; } /// /// 加载内嵌的应用程序图标(app.ico,同时作为 exe 图标,桌面快捷方式即显示它)。 /// 若资源缺失则回退到运行时绘制,保证托盘始终有图标。 /// private Icon LoadAppIcon() { try { var asm = typeof(TrayIconForm).Assembly; using (var stream = asm.GetManifestResourceStream("BeiJingComService.app.ico")) { if (stream != null) { return new Icon(stream, SystemInformation.SmallIconSize); } } } catch { // 图标资源异常不影响服务运行,回退绘制图标 } return CreateTrayIcon(); } private Icon CreateTrayIcon() { int size = 32; var bmp = new Bitmap(size, size); using (var g = Graphics.FromImage(bmp)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.Clear(Color.Transparent); using (var brush = new SolidBrush(Color.FromArgb(25, 118, 210))) using (var pen = new Pen(Color.FromArgb(13, 71, 161), 1.5f)) { var rect = new Rectangle(2, 6, 28, 20); using (var path = RoundedRect(rect, 5)) { g.FillPath(brush, path); g.DrawPath(pen, path); } } using (var pen = new Pen(Color.White, 2f)) { g.DrawLine(pen, 8, 13, 24, 13); g.DrawLine(pen, 19, 9, 24, 13); g.DrawLine(pen, 19, 17, 24, 13); g.DrawLine(pen, 8, 21, 24, 21); g.DrawLine(pen, 13, 17, 8, 21); g.DrawLine(pen, 13, 25, 8, 21); } } return Icon.FromHandle(bmp.GetHicon()); } private System.Drawing.Drawing2D.GraphicsPath RoundedRect(Rectangle bounds, int radius) { var path = new System.Drawing.Drawing2D.GraphicsPath(); int d = radius * 2; path.AddArc(bounds.X, bounds.Y, d, d, 180, 90); path.AddArc(bounds.Right - d, bounds.Y, d, d, 270, 90); path.AddArc(bounds.Right - d, bounds.Bottom - d, d, d, 0, 90); path.AddArc(bounds.X, bounds.Bottom - d, d, d, 90, 90); path.CloseFigure(); return path; } private void OnOpenPanel(object sender, EventArgs e) { if (_diagnosticForm == null || _diagnosticForm.IsDisposed) { _diagnosticForm = new DiagnosticForm(_protocols); } _diagnosticForm.Show(); _diagnosticForm.BringToFront(); _diagnosticForm.WindowState = FormWindowState.Normal; } /// /// 打开 HTML 调试页。走服务自身的 /debug(与接口同源),而不是 Process.Start 打开 /// 本地 html 文件 —— 后者是 file:// 源,访问 127.0.0.1 时会撞上浏览器的 /// Private Network Access 策略,被直接拒绝(服务端加 CORS 头也救不回来)。 /// private void OnOpenDebugPage(object sender, EventArgs e) { var http = _protocols == null ? null : Array.Find(_protocols, p => p.Name == "Http") as HttpProtocol; if (http != null && !http.IsListening) { MessageBox.Show("HTTP 通道未启动,无法打开调试页。" + Environment.NewLine + "请检查端口是否被占用,或重启网关后重试。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } string url = "http://127.0.0.1:" + AppConfig.HttpPort + "/debug"; try { Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }); } catch (Exception ex) { Logger.Error("打开 HTML 调试页失败", ex); MessageBox.Show("无法打开 HTML 调试页: " + ex.Message + Environment.NewLine + "可在浏览器中手动打开: " + url, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void OnViewLog(object sender, EventArgs e) { try { Directory.CreateDirectory(Logger.LogDirectoryPath); Process.Start(Logger.LogDirectoryPath); } catch (Exception ex) { Logger.Error("打开日志目录失败", ex); MessageBox.Show($"无法打开日志目录: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void OnRestartGateway(object sender, EventArgs e) { try { GatewayHost.Instance.Reinitialize(); MessageBox.Show("网关已重建", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { Logger.Error("重建网关失败", ex); MessageBox.Show($"重建网关失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void OnExit(object sender, EventArgs e) { if (!_exitConfirmed) { var result = MessageBox.Show("当前服务正在运行,确定要退出吗?", "确认退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result != DialogResult.Yes) return; _exitConfirmed = true; } _notifyIcon.Visible = false; this.Close(); } private void OnGatewayInitializeFailed(string message) { ShowBalloon("网关初始化失败", message, ToolTipIcon.Error); if (_diagnosticForm != null && !_diagnosticForm.IsDisposed) { _diagnosticForm.SetInitError(message); } } private void OnGatewayReinitialized() { ShowBalloon("网关已重建", "本地 COM 网关重新初始化完成", ToolTipIcon.Info); } private void ShowBalloon(string title, string message, ToolTipIcon icon) { try { _notifyIcon.ShowBalloonTip(3000, title, message, icon); } catch (Exception ex) { Logger.Error("显示托盘提示失败", ex); } } protected override void OnFormClosing(FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing && !_exitConfirmed) { var result = MessageBox.Show("当前服务正在运行,确定要退出吗?", "确认退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result != DialogResult.Yes) { e.Cancel = true; return; } _exitConfirmed = true; } base.OnFormClosing(e); } protected override void Dispose(bool disposing) { if (disposing) { GatewayHost.Instance.InitializeFailed -= OnGatewayInitializeFailed; GatewayHost.Instance.Reinitialized -= OnGatewayReinitialized; if (_diagnosticForm != null) _diagnosticForm.Dispose(); if (_notifyIcon != null) _notifyIcon.Dispose(); if (_contextMenu != null) _contextMenu.Dispose(); } base.Dispose(disposing); } } }