为 AntdUI 扩展一个 MessageBox 方法

AntdUI 是个很不错的开源 WinFrom 界面组件,使用中感觉消息对话框调用有点麻烦,于是按照 MessageBox.Show 的使用习惯,增加了一个扩展方法来调用,废话不多说,直接上代码。

复制代码
  1 using System.Windows.Forms;
  2 
  3 namespace AntdUI
  4 {
  5     public static class WindowExtentions
  6     {
  7         /// <summary>
  8         /// 仿照 System.Window.Forms.MessageBox.Show() 简化对话框弹窗
  9         /// </summary>
 10         /// <param name="owner"></param>
 11         /// <param name="message"></param>
 12         /// <param name="title"></param>
 13         /// <param name="buttons"></param>
 14         /// <param name="icon"></param>
 15         /// <param name="closeButton"></param>
 16         /// <returns></returns>
 17         public static DialogResult MessageBox(this Window owner, string message,
 18             string title = "",
 19             MessageBoxButtons buttons = MessageBoxButtons.OK,
 20             MessageBoxIcon icon = MessageBoxIcon.None,
 21             bool closeButton = true) 
 22         {
 23             var modalCfg = new Modal.Config(form: owner, title: title, content: message);
 24             //modalCfg.CloseIcon = true; // 始终返回 No 禁用
 25             modalCfg.Btns = createButtons(buttons, modalCfg);
 26             modalCfg.Icon = createIcon(icon);
 27 
 28             var ret = DialogResult.OK;
 29             //modalCfg.OnOk = config =>
 30             //{
 31             //    return true;
 32             //};
 33 
 34             modalCfg.OkType = TTypeMini.Primary;
 35             modalCfg.OnBtns = btn =>
 36             {
 37                 switch (btn.Name)
 38                 {
 39                     case "No": ret = DialogResult.No; break;
 40                     case "Cancel": ret = DialogResult.Cancel; break;
 41                     case "Retry": ret = DialogResult.Retry; break;
 42                     case "Abort": ret = DialogResult.Abort; break;
 43                     case "Ignore": ret = DialogResult.Ignore; break;
 44 #if NET5_0_OR_GREATER
 45                     case "Continue": ret = DialogResult.Continue; break;
 46 #endif
 47                     default: ret = DialogResult.OK; break;
 48                 }
 49             };
 50             
 51             if (Modal.open(modalCfg) == DialogResult.OK)
 52                 switch (modalCfg.OkText)
 53                 {
 54                     case "":
 55                         ret = DialogResult.Yes; break;
 56                     case "重试":
 57                         ret = DialogResult.Retry; break;
 58                     case "放弃":
 59                         ret = DialogResult.Abort; break;
 60                     case "确定":
 61                     default:
 62                         ret = DialogResult.OK; break;
 63                 }    
 64             return ret;
 65         }
 66 
 67         private static Modal.Btn[] createButtons(MessageBoxButtons b, Modal.Config cfg)
 68         {
 69             cfg.CancelText = null; //禁用自带的取消按钮
 70             switch (b)
 71             {
 72                 case MessageBoxButtons.OKCancel:
 73                     return new Modal.Btn[] { new Modal.Btn("Cancel", "取消")};
 74                 case MessageBoxButtons.YesNo:
 75                     cfg.OkText = "";
 76                     return new Modal.Btn[] { new Modal.Btn("No", "") };
 77                 case MessageBoxButtons.YesNoCancel:
 78                     cfg.OkText = "";
 79                     return new Modal.Btn[] {
 80                         new Modal.Btn("No", ""),
 81                         new Modal.Btn("Cancel", "取消")
 82                     };
 83                 case MessageBoxButtons.RetryCancel:
 84                     cfg.OkText = "重试";
 85                     return new Modal.Btn[] { new Modal.Btn("Cancel", "取消") };
 86                 case MessageBoxButtons.AbortRetryIgnore:
 87                     cfg.OkText = "放弃";
 88                     return new Modal.Btn[] { 
 89                         new Modal.Btn("Retry", "重试"),
 90                         new Modal.Btn("Ignore", "忽略")
 91                     };
 92 #if NET5_0_OR_GREATER
 93                 case MessageBoxButtons.CancelTryContinue:
 94                     cfg.OkText = "重试";
 95                     return new Modal.Btn[] {
 96                         new Modal.Btn("Cancel", "取消"),
 97                         new Modal.Btn("Continue", "继续")
 98                     };
 99 #endif
100                 case MessageBoxButtons.OK:
101                 default:
102                     return new Modal.Btn[] {};
103             }
104         }
105 
106         private static TType createIcon(MessageBoxIcon icon)
107         {
108             switch (icon)
109             {
110                 case MessageBoxIcon.Information:
111                     return TType.Success;
112                 case MessageBoxIcon.Exclamation:
113                     return TType.Warn;
114                 case MessageBoxIcon.Stop:
115                     return TType.Error;
116                 case MessageBoxIcon.Question:
117                     return TType.Info;
118                 case MessageBoxIcon.None:
119                 default:
120                     return TType.None;
121             }
122         }
123     }
124 }
复制代码

本想着去提个PR,可又觉得没什么技术含量,有用得上的就自取吧。

posted @   树欲静·而风不止  阅读(416)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示