diff --git a/CEETimerCSharpWinForms/Forms/MainForm.Designer.cs b/CEETimerCSharpWinForms/Forms/MainForm.Designer.cs index b50f692..44b64b4 100644 --- a/CEETimerCSharpWinForms/Forms/MainForm.Designer.cs +++ b/CEETimerCSharpWinForms/Forms/MainForm.Designer.cs @@ -30,19 +30,12 @@ private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.LabelCountdown = new System.Windows.Forms.Label(); - this.ContextMenuMain = new System.Windows.Forms.ContextMenuStrip(this.components); - this.ContextSettings = new System.Windows.Forms.ToolStripMenuItem(); - this.ContextAbout = new System.Windows.Forms.ToolStripMenuItem(); - this.ToolStripSeparatorMain = new System.Windows.Forms.ToolStripSeparator(); - this.ContextInstallDir = new System.Windows.Forms.ToolStripMenuItem(); - this.ContextMenuMain.SuspendLayout(); this.SuspendLayout(); // // LabelCountdown // this.LabelCountdown.AutoSize = true; this.LabelCountdown.BackColor = System.Drawing.Color.Transparent; - this.LabelCountdown.ContextMenuStrip = this.ContextMenuMain; this.LabelCountdown.Dock = System.Windows.Forms.DockStyle.Fill; this.LabelCountdown.ForeColor = System.Drawing.Color.Black; this.LabelCountdown.Location = new System.Drawing.Point(0, 0); @@ -51,44 +44,6 @@ private void InitializeComponent() this.LabelCountdown.TabIndex = 0; this.LabelCountdown.Text = "正在加载中..."; // - // ContextMenuMain - // - this.ContextMenuMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.ContextSettings, - this.ContextAbout, - this.ToolStripSeparatorMain, - this.ContextInstallDir}); - this.ContextMenuMain.Name = "Context"; - this.ContextMenuMain.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional; - this.ContextMenuMain.Size = new System.Drawing.Size(183, 76); - // - // ContextSettings - // - this.ContextSettings.Name = "ContextSettings"; - this.ContextSettings.Size = new System.Drawing.Size(182, 22); - this.ContextSettings.Text = "设置(&S)"; - this.ContextSettings.Click += new System.EventHandler(this.ContextSettings_Click); - // - // ContextAbout - // - this.ContextAbout.Name = "ContextAbout"; - this.ContextAbout.ShowShortcutKeys = false; - this.ContextAbout.Size = new System.Drawing.Size(182, 22); - this.ContextAbout.Text = "关于(&A)"; - this.ContextAbout.Click += new System.EventHandler(this.ContextAbout_Click); - // - // ToolStripSeparatorMain - // - this.ToolStripSeparatorMain.Name = "ToolStripSeparatorMain"; - this.ToolStripSeparatorMain.Size = new System.Drawing.Size(179, 6); - // - // ContextInstallDir - // - this.ContextInstallDir.Name = "ContextInstallDir"; - this.ContextInstallDir.Size = new System.Drawing.Size(182, 22); - this.ContextInstallDir.Text = "安装目录(&D)"; - this.ContextInstallDir.Click += new System.EventHandler(this.ContextInstallDir_Click); - // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); @@ -96,7 +51,6 @@ private void InitializeComponent() this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.ClientSize = new System.Drawing.Size(162, 23); - this.ContextMenuStrip = this.ContextMenuMain; this.ControlBox = false; this.Controls.Add(this.LabelCountdown); this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); @@ -107,9 +61,8 @@ private void InitializeComponent() this.ShowIcon = false; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; - this.Text = "高考倒计时"; + this.Text = WindowTitle; this.TopMost = true; - this.ContextMenuMain.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -118,11 +71,6 @@ private void InitializeComponent() #endregion private System.Windows.Forms.Label LabelCountdown; - private System.Windows.Forms.ContextMenuStrip ContextMenuMain; - private System.Windows.Forms.ToolStripMenuItem ContextAbout; - private System.Windows.Forms.ToolStripMenuItem ContextSettings; - private System.Windows.Forms.ToolStripSeparator ToolStripSeparatorMain; - private System.Windows.Forms.ToolStripMenuItem ContextInstallDir; } } diff --git a/CEETimerCSharpWinForms/Forms/MainForm.cs b/CEETimerCSharpWinForms/Forms/MainForm.cs index 94518bf..f0eec77 100644 --- a/CEETimerCSharpWinForms/Forms/MainForm.cs +++ b/CEETimerCSharpWinForms/Forms/MainForm.cs @@ -35,6 +35,7 @@ public partial class MainForm : TrackableForm private List> CountdownColors; private List> DefaultColors; private List>> CustomRules; + private string WindowTitle = "高考倒计时"; private string ExamName; private string[] CustomText; @@ -52,6 +53,7 @@ public partial class MainForm : TrackableForm private Rectangle SelectedScreen; private SettingsForm FormSettings; private AboutForm FormAbout; + private NotifyIcon TrayIcon; private readonly ConfigManager Config = new(); private readonly FontConverter fontConverter = new(); @@ -64,6 +66,7 @@ protected override void OnTrackableFormLoad() { SizeChanged += MainForm_SizeChanged; DefaultColors = [new(Color.Red, Color.White), new(Color.Green, Color.White), new(Color.Black, Color.White), new(Color.Black, Color.White)]; + InitializeContextMenu(); SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged; RefreshSettings(); @@ -86,6 +89,40 @@ private void MainForm_SizeChanged(object sender, EventArgs e) } } + private void InitializeContextMenu() + { + #region 来自网络 + /* + + 克隆 (重用) 现有 ContextMenuStrip 实例 参考: + + .net - C# - Duplicate ContextMenuStrip Items into another - Stack Overflow + https://stackoverflow.com/questions/37884815/c-sharp-duplicate-contextmenustrip-items-into-another + + */ + ContextMenuStrip BaseContextMenu() => new ContextMenuStrip() + .AddContextMenuItem("设置(&S)", ContextSettings_Click) + .AddContextMenuItem("关于(&A)", ContextAbout_Click) + .AddContextSeparator() + .AddContextMenuItem("安装目录(&D)", (sender, e) => AppLauncher.OpenInstallDir()); + #endregion + + var ContextMenuMain = BaseContextMenu(); + ContextMenuStrip = ContextMenuMain; + LabelCountdown.ContextMenuStrip = ContextMenuMain; + + TrayIcon = new() + { + Visible = true, + Text = Text, + Icon = Icon.FromHandle(Properties.Resources.AppIcon100px.GetHicon()), + ContextMenuStrip = BaseContextMenu() + .AddContextSeparator() + .AddContextMenuItem("显示(&S)", (sender, e) => this.ReActivate()) + .AddContextMenuItem("退出(&E)", (sender, e) => AppLauncher.Shutdown()) + }; + } + private async void RefreshSettings() { Config.MountConfig(true); @@ -310,11 +347,6 @@ private void ContextAbout_Click(object sender, EventArgs e) FormAbout.ReActivate(); } - private void ContextInstallDir_Click(object sender, EventArgs e) - { - AppLauncher.OpenInstallDir(); - } - protected override void OnTrackableFormClosing(FormClosingEventArgs e) { e.Cancel = e.CloseReason != CloseReason.WindowsShutDown; @@ -362,6 +394,7 @@ private async Task StartCountdown() IsCountdownRunning = false; UpdateCountdown("欢迎使用高考倒计时", CountdownColors[3].Item1, CountdownColors[3].Item2); + UpdateTrayIconText(WindowTitle); } private void ApplyColorRule(int Phase, TimeSpan Span, string Name, string Hint) @@ -450,9 +483,25 @@ private void UpdateCountdown(string CountdownText, Color Fore, Color Back) LabelCountdown.Text = CountdownText; LabelCountdown.ForeColor = Fore; BackColor = Back; + UpdateTrayIconText(CountdownText, false); }); } + private void UpdateTrayIconText(string cText, bool cInvokeRequired = true) + { + if (cInvokeRequired) + { + BeginInvoke(() => + { + TrayIcon.Text = cText; + }); + } + else + { + TrayIcon.Text = cText; + } + } + private void CompatibleWithPPTService() { if (IsPPTService) diff --git a/CEETimerCSharpWinForms/Modules/Extensions.cs b/CEETimerCSharpWinForms/Modules/Extensions.cs index 3cd6d69..a85a70e 100644 --- a/CEETimerCSharpWinForms/Modules/Extensions.cs +++ b/CEETimerCSharpWinForms/Modules/Extensions.cs @@ -30,6 +30,18 @@ public static string RemoveIllegalChars(this string s) => new(s.Trim().Replace(" ", "").Where(c => char.IsLetterOrDigit(c) || (c >= ' ' && c <= byte.MaxValue)).Where(x => !ConfigPolicy.CharsNotAllowed.Contains(x)).ToArray()); #endregion + public static ContextMenuStrip AddContextMenuItem(this ContextMenuStrip ContextMenu, string Text, EventHandler OnClickHandler) + { + ContextMenu.Items.Add(Text, null, OnClickHandler); + return ContextMenu; + } + + public static ContextMenuStrip AddContextSeparator(this ContextMenuStrip ContextMenu) + { + ContextMenu.Items.Add(new ToolStripSeparator()); + return ContextMenu; + } + public static int WithDpi(this int px, Control control) { Graphics g = null;