Skip to content

Commit

Permalink
v3.0.9: 🛠️ 实现 #8 - 1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
WangHaonie committed Jan 12, 2025
1 parent f5e3f28 commit 78c42b9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
54 changes: 1 addition & 53 deletions CEETimerCSharpWinForms/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 54 additions & 5 deletions CEETimerCSharpWinForms/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public partial class MainForm : TrackableForm
private List<TupleEx<Color, Color>> CountdownColors;
private List<TupleEx<Color, Color>> DefaultColors;
private List<TupleEx<int, TimeSpan, TupleEx<Color, Color, string>>> CustomRules;
private string WindowTitle = "高考倒计时";
private string ExamName;
private string[] CustomText;

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions CEETimerCSharpWinForms/Modules/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 78c42b9

Please sign in to comment.