|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows; |
| 7 | +using System.Windows.Controls; |
| 8 | +using System.Windows.Data; |
| 9 | +using System.Windows.Documents; |
| 10 | +using System.Windows.Forms; |
| 11 | +using System.Windows.Input; |
| 12 | +using System.Windows.Media; |
| 13 | +using System.Windows.Media.Imaging; |
| 14 | +using System.Windows.Shapes; |
| 15 | +using Flow.Launcher.Core.Resource; |
| 16 | +using Flow.Launcher.Infrastructure; |
| 17 | +using Flow.Launcher.Infrastructure.Image; |
| 18 | +using YamlDotNet.Core.Tokens; |
| 19 | + |
| 20 | +namespace Flow.Launcher |
| 21 | +{ |
| 22 | + public partial class MessageBoxEx : Window |
| 23 | + { |
| 24 | + public MessageBoxEx() |
| 25 | + { |
| 26 | + InitializeComponent(); |
| 27 | + } |
| 28 | + |
| 29 | + public enum MessageBoxType |
| 30 | + { |
| 31 | + ConfirmationWithYesNo = 0, |
| 32 | + ConfirmationWithYesNoCancel, |
| 33 | + YesNo, |
| 34 | + Information, |
| 35 | + Error, |
| 36 | + Warning |
| 37 | + } |
| 38 | + |
| 39 | + public enum MessageBoxImage |
| 40 | + { |
| 41 | + Warning = 0, |
| 42 | + Question, |
| 43 | + Information, |
| 44 | + Error, |
| 45 | + None |
| 46 | + } |
| 47 | + |
| 48 | + static MessageBoxEx msgBox; |
| 49 | + static MessageBoxResult _result = MessageBoxResult.No; |
| 50 | + |
| 51 | + |
| 52 | + /// 1 parameter |
| 53 | + public static MessageBoxResult Show(string msg) |
| 54 | + { |
| 55 | + return Show(string.Empty, msg, MessageBoxButton.OK, MessageBoxImage.None); |
| 56 | + } |
| 57 | + |
| 58 | + // 2 parameter |
| 59 | + public static MessageBoxResult Show(string caption, string text) |
| 60 | + { |
| 61 | + return Show(caption, text, MessageBoxButton.OK, MessageBoxImage.None); |
| 62 | + } |
| 63 | + |
| 64 | + /// 3 parameter |
| 65 | + public static MessageBoxResult Show(string caption, string msg, MessageBoxType type) |
| 66 | + { |
| 67 | + switch (type) |
| 68 | + { |
| 69 | + case MessageBoxType.ConfirmationWithYesNo: |
| 70 | + return Show(caption, msg, MessageBoxButton.YesNo, |
| 71 | + MessageBoxImage.Question); |
| 72 | + case MessageBoxType.YesNo: |
| 73 | + return Show(caption, msg, MessageBoxButton.YesNo, |
| 74 | + MessageBoxImage.Question); |
| 75 | + case MessageBoxType.ConfirmationWithYesNoCancel: |
| 76 | + return Show(caption, msg, MessageBoxButton.YesNoCancel, |
| 77 | + MessageBoxImage.Question); |
| 78 | + case MessageBoxType.Information: |
| 79 | + return Show(caption, msg, MessageBoxButton.OK, |
| 80 | + MessageBoxImage.Information); |
| 81 | + case MessageBoxType.Error: |
| 82 | + return Show(caption, msg, MessageBoxButton.OK, |
| 83 | + MessageBoxImage.Error); |
| 84 | + case MessageBoxType.Warning: |
| 85 | + return Show(caption, msg, MessageBoxButton.OK, |
| 86 | + MessageBoxImage.Warning); |
| 87 | + default: |
| 88 | + return MessageBoxResult.No; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // 4 parameter, Final Display Message. |
| 93 | + public static MessageBoxResult Show(string caption, string text, MessageBoxButton button, MessageBoxImage image) |
| 94 | + { |
| 95 | + msgBox = new MessageBoxEx(); |
| 96 | + msgBox.TitleTextBlock.Text = text; |
| 97 | + msgBox.DescTextBlock.Text = caption; |
| 98 | + msgBox.Title = text; |
| 99 | + SetVisibilityOfButtons(button); |
| 100 | + SetImageOfMessageBox(image); |
| 101 | + msgBox.ShowDialog(); |
| 102 | + return _result; |
| 103 | + } |
| 104 | + private void Button_Click(object sender, RoutedEventArgs e) |
| 105 | + { |
| 106 | + if (sender == btnOk) |
| 107 | + _result = MessageBoxResult.OK; |
| 108 | + else if (sender == btnYes) |
| 109 | + _result = MessageBoxResult.Yes; |
| 110 | + else if (sender == btnNo) |
| 111 | + _result = MessageBoxResult.No; |
| 112 | + else if (sender == btnCancel) |
| 113 | + _result = MessageBoxResult.Cancel; |
| 114 | + else |
| 115 | + _result = MessageBoxResult.None; |
| 116 | + msgBox.Close(); |
| 117 | + msgBox = null; |
| 118 | + } |
| 119 | + |
| 120 | + private static void SetVisibilityOfButtons(MessageBoxButton button) |
| 121 | + { |
| 122 | + switch (button) |
| 123 | + { |
| 124 | + case MessageBoxButton.OK: |
| 125 | + msgBox.btnCancel.Visibility = Visibility.Collapsed; |
| 126 | + msgBox.btnNo.Visibility = Visibility.Collapsed; |
| 127 | + msgBox.btnYes.Visibility = Visibility.Collapsed; |
| 128 | + msgBox.btnOk.Focus(); |
| 129 | + break; |
| 130 | + case MessageBoxButton.OKCancel: |
| 131 | + msgBox.btnNo.Visibility = Visibility.Collapsed; |
| 132 | + msgBox.btnYes.Visibility = Visibility.Collapsed; |
| 133 | + msgBox.btnCancel.Focus(); |
| 134 | + break; |
| 135 | + case MessageBoxButton.YesNo: |
| 136 | + msgBox.btnOk.Visibility = Visibility.Collapsed; |
| 137 | + msgBox.btnCancel.Visibility = Visibility.Collapsed; |
| 138 | + msgBox.btnNo.Focus(); |
| 139 | + break; |
| 140 | + case MessageBoxButton.YesNoCancel: |
| 141 | + msgBox.btnOk.Visibility = Visibility.Collapsed; |
| 142 | + msgBox.btnCancel.Focus(); |
| 143 | + break; |
| 144 | + default: |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + private static void SetImageOfMessageBox(MessageBoxImage image) |
| 149 | + { |
| 150 | + switch (image) |
| 151 | + { |
| 152 | + case MessageBoxImage.Warning: |
| 153 | + msgBox.SetImage("Warning.png"); |
| 154 | + msgBox.Img.Visibility = Visibility.Visible; |
| 155 | + break; |
| 156 | + case MessageBoxImage.Question: |
| 157 | + msgBox.SetImage("Question.png"); |
| 158 | + msgBox.Img.Visibility = Visibility.Visible; |
| 159 | + break; |
| 160 | + case MessageBoxImage.Information: |
| 161 | + msgBox.SetImage("Information.png"); |
| 162 | + msgBox.Img.Visibility = Visibility.Visible; |
| 163 | + break; |
| 164 | + case MessageBoxImage.Error: |
| 165 | + msgBox.SetImage("Error.png"); |
| 166 | + msgBox.Img.Visibility = Visibility.Visible; |
| 167 | + break; |
| 168 | + default: |
| 169 | + msgBox.Img.Visibility = Visibility.Collapsed; |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + private void SetImage(string imageName) |
| 174 | + { |
| 175 | + string uri = Constant.ProgramDirectory + "/Images/" + imageName; |
| 176 | + var uriSource = new Uri(uri, UriKind.RelativeOrAbsolute); |
| 177 | + Img.Source = new BitmapImage(uriSource); |
| 178 | + } |
| 179 | + private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e) |
| 180 | + { |
| 181 | + DialogResult = false; |
| 182 | + Close(); |
| 183 | + } |
| 184 | + |
| 185 | + private void Button_Cancel(object sender, RoutedEventArgs e) |
| 186 | + { |
| 187 | + msgBox.Close(); |
| 188 | + msgBox = null; |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | +} |
0 commit comments