爲 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,可又覺得沒什麼技術含量,有用得上的就自取吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章