| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- 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;
- }
- /// <summary>
- /// 加载内嵌的应用程序图标(app.ico,同时作为 exe 图标,桌面快捷方式即显示它)。
- /// 若资源缺失则回退到运行时绘制,保证托盘始终有图标。
- /// </summary>
- 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;
- }
- /// <summary>
- /// 打开 HTML 调试页。走服务自身的 /debug(与接口同源),而不是 Process.Start 打开
- /// 本地 html 文件 —— 后者是 file:// 源,访问 127.0.0.1 时会撞上浏览器的
- /// Private Network Access 策略,被直接拒绝(服务端加 CORS 头也救不回来)。
- /// </summary>
- 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);
- }
- }
- }
|