Skip to content

Commit

Permalink
v3.0.9: 🛠️ 优化了弹出消息框的方法调用
Browse files Browse the repository at this point in the history
  • Loading branch information
WangHaonie committed Jan 11, 2025
1 parent 7a44b69 commit f5e3f28
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CEETimerCSharpWinForms/Controls/AppForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected void Adjust(Action Adjustment)
/// <param name="IgnoreChanges">执行 忽略更改 的代码</param>
protected void ShowUnsavedWarning(string WarningMsg, FormClosingEventArgs e, Action SaveChanges, Action IgnoreChanges)
{
switch (MessageX.Popup(WarningMsg, MessageLevel.Warning, Buttons: MessageBoxExButtons.YesNo))
switch (MessageX.Warn(WarningMsg, Buttons: MessageBoxExButtons.YesNo))
{
case DialogResult.Yes:
e.Cancel = true;
Expand Down
4 changes: 2 additions & 2 deletions CEETimerCSharpWinForms/Dialogs/CustomTextDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ protected override void OnButtonAClicked()
P3TextRaw = RemoveInvalid(TextBoxP3.Text);
string[] tmp = [P1TextRaw, P2TextRaw, P3TextRaw];

if (!(bool)CustomRuleHelper.CheckCustomText(tmp, out string Error) && !string.IsNullOrWhiteSpace(Error))
if (!(bool)CustomRuleHelper.CheckCustomText(tmp, out string ErrorMsg) && !string.IsNullOrWhiteSpace(ErrorMsg))
{
MessageX.Popup(Error, MessageLevel.Error);
MessageX.Error(ErrorMsg);
return;
}

Expand Down
8 changes: 4 additions & 4 deletions CEETimerCSharpWinForms/Dialogs/RuleDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,19 @@ protected override void OnButtonAClicked()

if (!ColorHelper.IsNiceContrast(_Fore, _Back))
{
MessageX.Popup("选择的颜色相似或对比度较低,将无法看清文字。\n\n请尝试更换其它背景颜色或文字颜色!", MessageLevel.Error);
MessageX.Error("选择的颜色相似或对比度较低,将无法看清文字。\n\n请尝试更换其它背景颜色或文字颜色!");
return;
}

if (NudDays.Value == 0 && NudHours.Value == 0 && NudMinutes.Value == 0 && NudSeconds.Value == 0)
{
MessageX.Popup("时刻不能为0,请重新设置!", MessageLevel.Error);
MessageX.Error("时刻不能为0,请重新设置!");
return;
}

if (!(bool)CustomRuleHelper.CheckCustomText([TextBoxCustomText.Text], out string Error, ToBoolean: true) && !string.IsNullOrWhiteSpace(Error))
if (!(bool)CustomRuleHelper.CheckCustomText([TextBoxCustomText.Text], out string ErrorMsg, ToBoolean: true) && !string.IsNullOrWhiteSpace(ErrorMsg))
{
MessageX.Popup(Error, MessageLevel.Error);
MessageX.Error(ErrorMsg);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions CEETimerCSharpWinForms/Dialogs/RulesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void ListViewMain_KeyDown(object sender, KeyEventArgs e)

private void ContextDelete_Click(object sender, EventArgs e)
{
if (MessageX.Popup("确认删除所选规则吗?此操作将不可撤销!", MessageLevel.Warning, Buttons: MessageBoxExButtons.YesNo) == DialogResult.Yes)
if (MessageX.Warn("确认删除所选规则吗?此操作将不可撤销!", Buttons: MessageBoxExButtons.YesNo) == DialogResult.Yes)
{
foreach (ListViewItem Item in ListViewMain.SelectedItems)
{
Expand Down Expand Up @@ -150,7 +150,7 @@ private void AddItem(int RuleTypeIndex, string ExamTick, Color Fore, Color Back,
{
Execute(() =>
{
if (MessageX.Popup("检测到即将添加的规则与现有的重复,是否覆盖?", MessageLevel.Warning, Buttons: MessageBoxExButtons.YesNo) == DialogResult.Yes)
if (MessageX.Warn("检测到即将添加的规则与现有的重复,是否覆盖?", Buttons: MessageBoxExButtons.YesNo) == DialogResult.Yes)
{
ModifyOrOverrideItem(Duplicate);
return;
Expand Down
4 changes: 2 additions & 2 deletions CEETimerCSharpWinForms/Forms/DownloaderForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private async Task DownloadUpdate()

if (ex is not TaskCanceledException)
{
MessageX.Popup($"无法下载更新文件!{ex.ToMessage()}", MessageLevel.Error);
MessageX.Error($"无法下载更新文件!{ex.ToMessage()}");
LabelDownloading.Text = "下载失败,你可以点击 重试 来重新启动下载。";
LabelSize.Text = "已下载/总共: N/A";
LabelSpeed.Text = "下载速度: N/A";
Expand Down Expand Up @@ -141,7 +141,7 @@ private void ButtonCancel_Click(object sender, EventArgs e)
cts?.Cancel();
LabelDownloading.Text = "用户已取消下载。";
IsCancelled = true;
MessageX.Popup("你已取消下载!\n\n稍后可以在 关于 窗口点击图标来再次检查更新。", MessageLevel.Warning);
MessageX.Warn("你已取消下载!\n\n稍后可以在 关于 窗口点击图标来再次检查更新。");
}

Close();
Expand Down
2 changes: 1 addition & 1 deletion CEETimerCSharpWinForms/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private void ContextSettings_Click(object sender, EventArgs e)
UserCustomRules = CustomRules
};

FormSettings.ConfigChanged += (sender,e) => RefreshSettings();
FormSettings.ConfigChanged += (sender, e) => RefreshSettings();
}

FormSettings.ReActivate();
Expand Down
18 changes: 9 additions & 9 deletions CEETimerCSharpWinForms/Forms/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void CheckBoxShowEnd_CheckedChanged(object sender, EventArgs e)
{
if (CheckBoxShowEnd.Checked)
{
MessageX.Popup("由于已开启显示考试结束倒计时,请设置考试结束日期和时间。", MessageLevel.Info);
MessageX.Info("由于已开启显示考试结束倒计时,请设置考试结束日期和时间。");
TabControlMain.SelectedTab = TabPageGeneral;
DtpExamEnd.Focus();
}
Expand Down Expand Up @@ -515,15 +515,15 @@ private bool IsSettingsFormatValid()

if (string.IsNullOrWhiteSpace(ExamName) || (ExamName.Length is < ConfigPolicy.MinExamNameLength or > ConfigPolicy.MaxExamNameLength))
{
MessageX.Popup("输入的考试名称有误!\n\n请检查输入的考试名称是否太长或太短!", MessageLevel.Error, this, TabControlMain, TabPageGeneral);
MessageX.Error("输入的考试名称有误!\n\n请检查输入的考试名称是否太长或太短!", this, TabControlMain, TabPageGeneral);
return false;
}

if (DtpExamEnd.Enabled)
{
if (DtpExamEnd.Value <= DtpExamStart.Value)
{
MessageX.Popup("考试结束时间必须在开始时间之后!", MessageLevel.Error, this, TabControlMain, TabPageGeneral);
MessageX.Error("考试结束时间必须在开始时间之后!", this, TabControlMain, TabPageGeneral);
return false;
}
else if (ExamTimeSpan.TotalDays > 4)
Expand All @@ -546,7 +546,7 @@ private bool IsSettingsFormatValid()

if (!string.IsNullOrEmpty(UniMsg))
{
var _DialogResult = MessageX.Popup(UniMsg, MessageLevel.Warning, this, TabControlMain, TabPageGeneral, Buttons: MessageBoxExButtons.YesNo);
var _DialogResult = MessageX.Warn(UniMsg, this, TabControlMain, TabPageGeneral, Buttons: MessageBoxExButtons.YesNo);

if (_DialogResult is DialogResult.No or DialogResult.None)
{
Expand All @@ -573,7 +573,7 @@ private bool IsSettingsFormatValid()

if (ColorCheckMsg != 0)
{
MessageX.Popup($"第{ColorCheckMsg}组的颜色相似或对比度较低,将无法看清文字。\n\n请尝试更换其它背景颜色或文字颜色!", MessageLevel.Error, this, TabControlMain, TabPageAppearance);
MessageX.Error($"第{ColorCheckMsg}组的颜色相似或对比度较低,将无法看清文字。\n\n请尝试更换其它背景颜色或文字颜色!", this, TabControlMain, TabPageAppearance);
return false;
}

Expand All @@ -584,13 +584,13 @@ private void StartSyncTime()
{
try
{
if (!AppLauncher.IsAdmin) MessageX.Popup("检测到当前用户不具有管理员权限,运行该操作会发生错误。\n\n程序将在此消息框关闭后尝试弹出 UAC 提示框,前提要把系统的 UAC 设置为 \"仅当应用尝试更改我的计算机时通知我\" 或及以上,否则将无法进行授权。\n\n稍后若没有看见提示框,请更改 UAC 设置: 开始菜单搜索 uac", MessageLevel.Warning, this);
if (!AppLauncher.IsAdmin) MessageX.Warn("检测到当前用户不具有管理员权限,运行该操作会发生错误。\n\n程序将在此消息框关闭后尝试弹出 UAC 提示框,前提要把系统的 UAC 设置为 \"仅当应用尝试更改我的计算机时通知我\" 或及以上,否则将无法进行授权。\n\n稍后若没有看见提示框,请更改 UAC 设置: 开始菜单搜索 uac", this);

Process SyncTimeProcess = ProcessHelper.RunProcess("cmd.exe", "/c net stop w32time & sc config w32time start= auto & net start w32time && w32tm /config /manualpeerlist:ntp1.aliyun.com /syncfromflags:manual /reliable:YES /update && w32tm /resync && w32tm /resync", AdminRequired: true);

SyncTimeProcess.WaitForExit();
var ExitCode = SyncTimeProcess.ExitCode;
MessageX.Popup($"命令执行完成!\n\n返回值为 {ExitCode} (0x{ExitCode:X})\n(0 代表成功,其他值为失败)", MessageLevel.Info, this, TabControlMain, TabPageTools);
MessageX.Info($"命令执行完成!\n\n返回值为 {ExitCode} (0x{ExitCode:X})\n(0 代表成功,其他值为失败)", this, TabControlMain, TabPageTools);
}
#region 来自网络
/*
Expand All @@ -603,12 +603,12 @@ private void StartSyncTime()
*/
catch (Win32Exception ex) when (ex.NativeErrorCode == 1223)
{
MessageX.Popup($"授权失败,请在 UAC 对话框弹出时点击 \"\"{ex.ToMessage()}", MessageLevel.Error, this, TabControlMain, TabPageTools);
MessageX.Error($"授权失败,请在 UAC 对话框弹出时点击 \"\"{ex.ToMessage()}", this, TabControlMain, TabPageTools);
}
#endregion
catch (Exception ex)
{
MessageX.Popup($"命令执行时发生了错误。{ex.ToMessage()}", MessageLevel.Error, this, TabControlMain, TabPageTools);
MessageX.Error($"命令执行时发生了错误。{ex.ToMessage()}", this, TabControlMain, TabPageTools);
}
}

Expand Down
12 changes: 6 additions & 6 deletions CEETimerCSharpWinForms/Modules/AppLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void StartProgram(string[] args)

if (!CurrentExecutableName.Equals(OriginalFileName, StringComparison.OrdinalIgnoreCase))
{
MessageX.Popup($"为了您的使用体验,请不要更改程序文件名! 程序将在该消息框自动关闭后尝试自动恢复到原文件名,若自动恢复失败请手动改回。\n\n当前文件名: {CurrentExecutableName}\n原始文件名: {OriginalFileName}", MessageLevel.Error, Position: FormStartPosition.CenterScreen, AutoClose: true);
MessageX.Error($"为了您的使用体验,请不要更改程序文件名! 程序将在该消息框自动关闭后尝试自动恢复到原文件名,若自动恢复失败请手动改回。\n\n当前文件名: {CurrentExecutableName}\n原始文件名: {OriginalFileName}", Position: FormStartPosition.CenterScreen, AutoClose: true);
ProcessHelper.RunProcess("cmd.exe", $"/c ren \"{CurrentExecutablePath}\" {OriginalFileName} & start \"\" \"{CurrentExecutableDir}{OriginalFileName}\" {AllArgs}");
Exit(ExitReason.InvalidExeName);
}
Expand All @@ -69,18 +69,18 @@ public static void StartProgram(string[] args)
{
case "/?":
case "/h":
MessageX.Popup("可用的命令行参数: \n\n/h 显示此帮助信息;\n/ac 检测当前用户是否具有管理员权限;\n/fr <版本号>\n 强制下载并安装指定的版本,留空则当前版本,\n 推荐在特殊情况下使用,不支持老版本。", MessageLevel.Info);
MessageX.Info("可用的命令行参数: \n\n/h 显示此帮助信息;\n/ac 检测当前用户是否具有管理员权限;\n/fr <版本号>\n 强制下载并安装指定的版本,留空则当前版本,\n 推荐在特殊情况下使用,不支持老版本。");
break;
case "/ac":
CheckAdmin(out string UserName, true);
MessageX.Popup($"当前用户 {UserName} {(IsAdmin ? "" : "不")}具有管理员权限。", MessageLevel.Info);
MessageX.Info($"当前用户 {UserName} {(IsAdmin ? "" : "不")}具有管理员权限。");
break;
case "/fr":
if (Args.Length > 1) DownloaderForm.ManualVersion = Args[1];
Application.Run(new DownloaderForm());
break;
default:
MessageX.Popup($"无法解析的命令行参数: \n{AllArgs}", MessageLevel.Error, AutoClose: true);
MessageX.Error($"无法解析的命令行参数: \n{AllArgs}", AutoClose: true);
break;
}
}
Expand All @@ -92,7 +92,7 @@ public static void StartProgram(string[] args)
{
if (Args.Length != 0)
{
MessageX.Popup("请先关闭已打开的实例再使用命令行功能。", MessageLevel.Error, AutoClose: true);
MessageX.Error("请先关闭已打开的实例再使用命令行功能。", AutoClose: true);
}

StartPipeClient();
Expand Down Expand Up @@ -152,7 +152,7 @@ private static void HandleException(Exception ex)
Clipboard.SetText(ExOutput);
File.AppendAllText(ExFilePath, ExOutput);

var _DialogResult = MessageX.Popup($"程序出现意外错误,无法继续运行,非常抱歉给您带来不便,相关错误信息已写入到安装文件夹中的 {ExFileName} 文件和系统剪切板,建议您将相关信息并发送给软件开发者以便我们更好的定位并解决问题。或者您也可以点击 \"\" 来重启应用程序,\"\" 关闭应用程序{ex.ToMessage()}", MessageLevel.Error, FormManager.OpenForms.LastOrDefault(), Buttons: MessageBoxExButtons.YesNo);
var _DialogResult = MessageX.Error($"程序出现意外错误,无法继续运行,非常抱歉给您带来不便,相关错误信息已写入到安装文件夹中的 {ExFileName} 文件和系统剪切板,建议您将相关信息并发送给软件开发者以便我们更好的定位并解决问题。或者您也可以点击 \"\" 来重启应用程序,\"\" 关闭应用程序{ex.ToMessage()}", FormManager.OpenForms.LastOrDefault(), Buttons: MessageBoxExButtons.YesNo);

OpenInstallDir();
Shutdown(Restart: _DialogResult == DialogResult.Yes);
Expand Down
20 changes: 7 additions & 13 deletions CEETimerCSharpWinForms/Modules/MessageX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ static MessageX()
ErrorIcon = GetIcon(93);
}

/// <summary>
/// 在指定的窗体或定位到其标签页上或直接显示一个具有 DialogResult 返回值的消息框
/// </summary>
/// <param name="Message">消息</param>
/// <param name="Level">消息等级</param>
/// <param name="OwnerForm">[可选] 父窗体</param>
/// <param name="ParentTabControl">[可选] 父窗体的 TabControl</param>
/// <param name="ParentTabPage">[可选] 父窗体的 TabControl 的标签页</param>
/// <param name="Buttons">[可选] 要显示的按钮</param>
/// <param name="Position">[可选] 消息框出现的位置</param>
/// <param name="AutoClose">[可选] 是否允许消息框在3s后自动关闭</param>
/// <returns>DialogResult</returns>
public static DialogResult Popup(string Message, MessageLevel Level, Form OwnerForm = null, TabControl ParentTabControl = null, TabPage ParentTabPage = null, MessageBoxExButtons Buttons = MessageBoxExButtons.OK, FormStartPosition Position = FormStartPosition.CenterParent, bool AutoClose = false)
public static DialogResult Info(string Message, Form OwnerForm = null, TabControl ParentTabControl = null, TabPage ParentTabPage = null, MessageBoxExButtons Buttons = MessageBoxExButtons.OK, FormStartPosition Position = FormStartPosition.CenterParent, bool AutoClose = false) => Popup(Message, MessageLevel.Info, OwnerForm, ParentTabControl, ParentTabPage, Buttons, Position, AutoClose);

public static DialogResult Warn(string Message, Form OwnerForm = null, TabControl ParentTabControl = null, TabPage ParentTabPage = null, MessageBoxExButtons Buttons = MessageBoxExButtons.OK, FormStartPosition Position = FormStartPosition.CenterParent, bool AutoClose = false) => Popup(Message, MessageLevel.Warning, OwnerForm, ParentTabControl, ParentTabPage, Buttons, Position, AutoClose);

public static DialogResult Error(string Message, Form OwnerForm = null, TabControl ParentTabControl = null, TabPage ParentTabPage = null, MessageBoxExButtons Buttons = MessageBoxExButtons.OK, FormStartPosition Position = FormStartPosition.CenterParent, bool AutoClose = false) => Popup(Message, MessageLevel.Error, OwnerForm, ParentTabControl, ParentTabPage, Buttons, Position, AutoClose);

private static DialogResult Popup(string Message, MessageLevel Level, Form OwnerForm, TabControl ParentTabControl, TabPage ParentTabPage, MessageBoxExButtons Buttons, FormStartPosition Position, bool AutoClose)
{
var (Title, MessageBoxExIcon, Sound) = GetStuff(Level);
using var _MessageBoxEx = new MessageBoxEx(Sound, Buttons, AutoClose);
Expand Down
Loading

0 comments on commit f5e3f28

Please sign in to comment.