WPFMessageBox.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using System.Windows.Interop;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Media.Imaging;
  12. namespace Aitex.Core.UI.Dialog
  13. {
  14. /// <summary>
  15. /// WPFMessageBox.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class WPFMessageBox : INotifyPropertyChanged
  18. {
  19. private bool _animationRan;
  20. public WPFMessageBox(Window owner, string message, string details, MessageBoxButton button, MessageBoxImage icon,
  21. MessageBoxResult defaultResult, MessageBoxOptions options)
  22. {
  23. _animationRan = false;
  24. InitializeComponent();
  25. //Owner = owner ?? Application.Current.MainWindow;
  26. CreateButtons(button, defaultResult);
  27. CreateImage(icon);
  28. MessageText.Text = message;
  29. DetailsExpander.Visibility = string.IsNullOrEmpty(details) ? Visibility.Collapsed : Visibility.Visible;
  30. DetailsText.Text = details;
  31. ApplyOptions(options);
  32. }
  33. public MessageBoxResult MessageBoxResult { get; set; }
  34. #region INotifyPropertyChanged Members
  35. public event PropertyChangedEventHandler PropertyChanged;
  36. #endregion
  37. #region Create Buttons
  38. /// <summary>
  39. /// Create the message box's button according to the user's demand
  40. /// </summary>
  41. /// <param name="button">The user's buttons selection</param>
  42. /// <param name="defaultResult">The default button</param>
  43. private void CreateButtons(MessageBoxButton button, MessageBoxResult defaultResult)
  44. {
  45. switch (button)
  46. {
  47. case MessageBoxButton.OK:
  48. ButtonsPanel.Children.Add(CreateOkButton(defaultResult));
  49. break;
  50. case MessageBoxButton.OKCancel:
  51. ButtonsPanel.Children.Add(CreateOkButton(defaultResult));
  52. ButtonsPanel.Children.Add(CreateCancelButton(defaultResult));
  53. break;
  54. case MessageBoxButton.YesNoCancel:
  55. ButtonsPanel.Children.Add(CreateYesButton(defaultResult));
  56. ButtonsPanel.Children.Add(CreateNoButton(defaultResult));
  57. ButtonsPanel.Children.Add(CreateCancelButton(defaultResult));
  58. break;
  59. case MessageBoxButton.YesNo:
  60. ButtonsPanel.Children.Add(CreateYesButton(defaultResult));
  61. ButtonsPanel.Children.Add(CreateNoButton(defaultResult));
  62. break;
  63. default:
  64. throw new ArgumentOutOfRangeException("button");
  65. }
  66. }
  67. /// <summary>
  68. /// Create the ok button on demand
  69. /// </summary>
  70. /// <param name="defaultResult"></param>
  71. /// <returns></returns>
  72. private Button CreateOkButton(MessageBoxResult defaultResult)
  73. {
  74. var okButton = new Button
  75. {
  76. Name = "okButton",
  77. //Content = ((string)Application.Current.Resources["Confirm"]),
  78. Content = "确认",
  79. IsDefault = defaultResult == MessageBoxResult.OK,
  80. Tag = MessageBoxResult.OK,
  81. };
  82. okButton.Click += ButtonClick;
  83. return okButton;
  84. }
  85. /// <summary>
  86. /// Create the cancel button on demand
  87. /// </summary>
  88. /// <param name="defaultResult"></param>
  89. /// <returns></returns>
  90. private Button CreateCancelButton(MessageBoxResult defaultResult)
  91. {
  92. var cancelButton = new Button
  93. {
  94. Name = "cancelButton",
  95. //Content = ((string)Application.Current.Resources["Cancel"]),
  96. Content = "取消",
  97. IsDefault = defaultResult == MessageBoxResult.Cancel,
  98. IsCancel = true,
  99. Tag = MessageBoxResult.Cancel,
  100. };
  101. cancelButton.Click += ButtonClick;
  102. return cancelButton;
  103. }
  104. /// <summary>
  105. /// Create the yes button on demand
  106. /// </summary>
  107. /// <param name="defaultResult"></param>
  108. /// <returns></returns>
  109. private Button CreateYesButton(MessageBoxResult defaultResult)
  110. {
  111. var yesButton = new Button
  112. {
  113. Name = "yesButton",
  114. //Content = ((string)Application.Current.Resources["Yes"]),
  115. Content = "Yes",
  116. IsDefault = defaultResult == MessageBoxResult.Yes,
  117. Tag = MessageBoxResult.Yes,
  118. };
  119. yesButton.Click += ButtonClick;
  120. return yesButton;
  121. }
  122. /// <summary>
  123. /// Create the no button on demand
  124. /// </summary>
  125. /// <param name="defaultResult"></param>
  126. /// <returns></returns>
  127. private Button CreateNoButton(MessageBoxResult defaultResult)
  128. {
  129. var noButton = new Button
  130. {
  131. Name = "noButton",
  132. //Content = ((string)Application.Current.Resources["No"]),
  133. Content = "No",
  134. IsDefault = defaultResult == MessageBoxResult.No,
  135. Tag = MessageBoxResult.No,
  136. };
  137. noButton.Click += ButtonClick;
  138. return noButton;
  139. }
  140. /// <summary>
  141. /// The event the buttons trigger.
  142. /// Each button hold it's result in the tag, so here it just sets them and close the message box.
  143. /// </summary>
  144. /// <param name="sender"></param>
  145. /// <param name="e"></param>
  146. private void ButtonClick(object sender, RoutedEventArgs e)
  147. {
  148. MessageBoxResult = (MessageBoxResult)(sender as Button).Tag;
  149. Close();
  150. }
  151. #endregion
  152. private void ApplyOptions(MessageBoxOptions options)
  153. {
  154. if ((options & MessageBoxOptions.RightAlign) == MessageBoxOptions.RightAlign)
  155. {
  156. MessageText.TextAlignment = TextAlignment.Right;
  157. DetailsText.TextAlignment = TextAlignment.Right;
  158. }
  159. if ((options & MessageBoxOptions.RtlReading) == MessageBoxOptions.RtlReading)
  160. {
  161. FlowDirection = FlowDirection.RightToLeft;
  162. }
  163. }
  164. /// <summary>
  165. /// Create the image from the system's icons
  166. /// </summary>
  167. /// <param name="icon"></param>
  168. private void CreateImage(MessageBoxImage icon)
  169. {
  170. switch (icon)
  171. {
  172. case MessageBoxImage.None:
  173. ImagePlaceholder.Visibility = Visibility.Collapsed;
  174. break;
  175. case MessageBoxImage.Information:
  176. ImagePlaceholder.Source = SystemIcons.Information.ToImageSource();
  177. break;
  178. case MessageBoxImage.Question:
  179. ImagePlaceholder.Source = SystemIcons.Question.ToImageSource();
  180. break;
  181. case MessageBoxImage.Warning:
  182. ImagePlaceholder.Source = SystemIcons.Warning.ToImageSource();
  183. break;
  184. case MessageBoxImage.Error:
  185. ImagePlaceholder.Source = SystemIcons.Error.ToImageSource();
  186. break;
  187. default:
  188. throw new ArgumentOutOfRangeException("icon");
  189. }
  190. }
  191. public void OnPropertyChanged(string propertyName)
  192. {
  193. PropertyChangedEventHandler temp = PropertyChanged;
  194. if (temp != null)
  195. {
  196. temp(this, new PropertyChangedEventArgs(propertyName));
  197. }
  198. }
  199. /// <summary>
  200. /// Enable dragging
  201. /// </summary>
  202. /// <param name="sender"></param>
  203. /// <param name="e"></param>
  204. private void Window_MouseDown(object sender, MouseButtonEventArgs e)
  205. {
  206. if (e.LeftButton == MouseButtonState.Pressed)
  207. DragMove();
  208. }
  209. /// <summary>
  210. /// Show the startup animation
  211. /// </summary>
  212. /// <param name="sender"></param>
  213. /// <param name="e"></param>
  214. private void Window_Loaded(object sender, RoutedEventArgs e)
  215. {
  216. // This is set here to height after the width has been set
  217. // so the details expander won't stretch the message box when it's opened
  218. SizeToContent = SizeToContent.Height;
  219. //var animation = TryFindResource("LoadAnimation") as Storyboard;
  220. //animation.Begin(this);
  221. }
  222. /// <summary>
  223. /// Show the closing animation
  224. /// </summary>
  225. /// <param name="sender"></param>
  226. /// <param name="e"></param>
  227. private void MessageBoxWindow_Closing(object sender, CancelEventArgs e)
  228. {
  229. if (!_animationRan)
  230. {
  231. // The animation won't run if the window is allowed to close,
  232. // so here the animation starts, and the window's closing is canceled
  233. e.Cancel = true;
  234. var animation = TryFindResource("UnloadAnimation") as Storyboard;
  235. animation.Completed += AnimationCompleted;
  236. animation.Begin(this);
  237. }
  238. }
  239. /// <summary>
  240. /// Signals the closing animation ran, and close the window (for real this time)
  241. /// </summary>
  242. /// <param name="sender"></param>
  243. /// <param name="e"></param>
  244. private void AnimationCompleted(object sender, EventArgs e)
  245. {
  246. _animationRan = true;
  247. Close();
  248. }
  249. #region Show Information
  250. /// <summary>
  251. /// Display an information message
  252. /// </summary>
  253. /// <param name="message">The message text</param>
  254. /// <param name="details">The details part text</param>
  255. /// <param name="showCancel">Display the cancel</param>
  256. /// <param name="options">Misc options</param>
  257. /// <returns>The user's selected button</returns>
  258. public static MessageBoxResult ShowInformation(string message, string details = "", bool showCancel = false,
  259. MessageBoxOptions options = MessageBoxOptions.None)
  260. {
  261. return ShowInformation(null, message, details, showCancel, options);
  262. }
  263. /// <summary>
  264. /// Display an information message
  265. /// </summary>
  266. /// <param name="owner">The message box's parent window</param>
  267. /// <param name="message">The message text</param>
  268. /// <param name="details">The details part text</param>
  269. /// <param name="showCancel">Display the cancel</param>
  270. /// <param name="options">Misc options</param>
  271. /// <returns>The user's selected button</returns>
  272. public static MessageBoxResult ShowInformation(Window owner, string message, string details = "",
  273. bool showCancel = false,
  274. MessageBoxOptions options = MessageBoxOptions.None)
  275. {
  276. return Show(owner, message, details, showCancel ? MessageBoxButton.OKCancel : MessageBoxButton.OK,
  277. MessageBoxImage.Information, MessageBoxResult.OK, options);
  278. }
  279. #endregion
  280. #region Show Question
  281. /// <summary>
  282. /// Display a question
  283. /// </summary>
  284. /// <param name="message">The message text</param>
  285. /// <param name="details">The details part text</param>
  286. /// <param name="showCancel">Display the cancel</param>
  287. /// <param name="options">Misc options</param>
  288. /// <returns>The user's selected button</returns>
  289. public static MessageBoxResult ShowQuestion(string message, string details = "",
  290. bool showCancel = false,
  291. MessageBoxOptions options = MessageBoxOptions.None)
  292. {
  293. return ShowQuestion(null, message, details, showCancel, options);
  294. }
  295. /// <summary>
  296. /// Display a question
  297. /// </summary>
  298. /// <param name="owner">The message box's parent window</param>
  299. /// <param name="message">The message text</param>
  300. /// <param name="details">The details part text</param>
  301. /// <param name="showCancel">Display the cancel</param>
  302. /// <param name="options">Misc options</param>
  303. /// <returns>The user's selected button</returns>
  304. public static MessageBoxResult ShowQuestion(Window owner, string message, string details = "",
  305. bool showCancel = false,
  306. MessageBoxOptions options = MessageBoxOptions.None)
  307. {
  308. return Show(owner, message, details, showCancel ? MessageBoxButton.YesNoCancel : MessageBoxButton.YesNo,
  309. MessageBoxImage.Question, MessageBoxResult.Yes, options);
  310. }
  311. #endregion
  312. #region Show Warning
  313. /// <summary>
  314. /// Display a warning
  315. /// </summary>
  316. /// <param name="message">The message text</param>
  317. /// <param name="details">The details part text</param>
  318. /// <param name="showCancel">Display the cancel</param>
  319. /// <param name="options">Misc options</param>
  320. /// <returns>The user's selected button</returns>
  321. public static MessageBoxResult ShowWarning(string message, string details = "",
  322. bool showCancel = false,
  323. MessageBoxOptions options = MessageBoxOptions.None)
  324. {
  325. return ShowWarning(null, message, details, showCancel, options);
  326. }
  327. /// <summary>
  328. /// Display a warning
  329. /// </summary>
  330. /// <param name="owner">The message box's parent window</param>
  331. /// <param name="message">The message text</param>
  332. /// <param name="details">The details part text</param>
  333. /// <param name="showCancel">Display the cancel</param>
  334. /// <param name="options">Misc options</param>
  335. /// <returns>The user's selected button</returns>
  336. public static MessageBoxResult ShowWarning(Window owner, string message, string details = "",
  337. bool showCancel = false,
  338. MessageBoxOptions options = MessageBoxOptions.None)
  339. {
  340. return Show(owner, message, details, showCancel ? MessageBoxButton.OKCancel : MessageBoxButton.OK,
  341. MessageBoxImage.Warning, MessageBoxResult.OK, options);
  342. }
  343. #endregion
  344. #region Show Error
  345. /// <summary>
  346. /// Display an Error
  347. /// </summary>
  348. /// <param name="exception">Display the exception's details</param>
  349. /// <param name="message">The message text</param>
  350. /// <param name="options">Misc options</param>
  351. /// <returns>The user's selected button</returns>
  352. public static MessageBoxResult ShowError(Exception exception, string message = "",
  353. MessageBoxOptions options = MessageBoxOptions.None)
  354. {
  355. return ShowError(null, exception, message, options);
  356. }
  357. /// <summary>
  358. /// Display an Error
  359. /// </summary>
  360. /// <param name="message">The message text</param>
  361. /// <param name="details">The details part text</param>
  362. /// <param name="showCancel">Display the cancel</param>
  363. /// <param name="options">Misc options</param>
  364. /// <returns>The user's selected button</returns>
  365. public static MessageBoxResult ShowError(string message, string details = "",
  366. bool showCancel = false,
  367. MessageBoxOptions options = MessageBoxOptions.None)
  368. {
  369. return ShowError(null, message, details, showCancel, options);
  370. }
  371. /// <summary>
  372. /// Display an Error
  373. /// </summary>
  374. /// <param name="owner">The message box's parent window</param>
  375. /// <param name="exception">Display the exception's details</param>
  376. /// <param name="message">The message text</param>
  377. /// <param name="options">Misc options</param>
  378. /// <returns>The user's selected button</returns>
  379. public static MessageBoxResult ShowError(Window owner, Exception exception, string message = "",
  380. MessageBoxOptions options = MessageBoxOptions.None)
  381. {
  382. string details = string.Empty;
  383. #if DEBUG
  384. details = exception.ToString();
  385. #endif
  386. return Show(owner, String.IsNullOrEmpty(message) ? exception.Message : message, details, MessageBoxButton.OK,
  387. MessageBoxImage.Error, MessageBoxResult.OK, options);
  388. }
  389. /// <summary>
  390. /// Display an Error
  391. /// </summary>
  392. /// <param name="owner">The message box's parent window</param>
  393. /// <param name="message">The message text</param>
  394. /// <param name="details">The details part text</param>
  395. /// <param name="showCancel">Display the cancel</param>
  396. /// <param name="options">Misc options</param>
  397. /// <returns>The user's selected button</returns>
  398. public static MessageBoxResult ShowError(Window owner, string message, string details = "",
  399. bool showCancel = false,
  400. MessageBoxOptions options = MessageBoxOptions.None)
  401. {
  402. return Show(owner, message, details, showCancel ? MessageBoxButton.OKCancel : MessageBoxButton.OK,
  403. MessageBoxImage.Error, MessageBoxResult.OK, options);
  404. }
  405. #endregion
  406. #region Show
  407. /// <summary>
  408. /// Show the message box with the specified parameters
  409. /// </summary>
  410. /// <param name="message">The message text</param>
  411. /// <param name="details">The details part text</param>
  412. /// <param name="button">The buttons to be displayed</param>
  413. /// <param name="icon">The message's severity</param>
  414. /// <param name="defaultResult">The default button</param>
  415. /// <param name="options">Misc options</param>
  416. /// <returns>The user's selected button</returns>
  417. public static MessageBoxResult Show(string message, string details = "",
  418. MessageBoxButton button = MessageBoxButton.OK,
  419. MessageBoxImage icon = MessageBoxImage.None,
  420. MessageBoxResult defaultResult = MessageBoxResult.None,
  421. MessageBoxOptions options = MessageBoxOptions.None)
  422. {
  423. return Show(null, message, details, button, icon, defaultResult, options);
  424. }
  425. /// <summary>
  426. /// Show the message box with the specified parameters
  427. /// </summary>
  428. /// <param name="message">The message text</param>
  429. /// <param name="button">The buttons to be displayed</param>
  430. /// <param name="icon">The message's severity</param>
  431. /// <param name="defaultResult">The default button</param>
  432. /// <param name="options">Misc options</param>
  433. /// <returns>The user's selected button</returns>
  434. public static MessageBoxResult Show(string message,
  435. MessageBoxButton button = MessageBoxButton.OK,
  436. MessageBoxImage icon = MessageBoxImage.None,
  437. MessageBoxResult defaultResult = MessageBoxResult.None,
  438. MessageBoxOptions options = MessageBoxOptions.None)
  439. {
  440. return Show(message, string.Empty, button, icon, defaultResult, options);
  441. }
  442. /// <summary>
  443. /// Show the message box with the specified parameters
  444. /// </summary>
  445. /// <param name="owner">The message box's parent window</param>
  446. /// <param name="message">The message text</param>
  447. /// <param name="button">The buttons to be displayed</param>
  448. /// <param name="icon">The message's severity</param>
  449. /// <param name="defaultResult">The default button</param>
  450. /// <param name="options">Misc options</param>
  451. /// <returns>The user's selected button</returns>
  452. public static MessageBoxResult Show(Window owner, string message,
  453. MessageBoxButton button = MessageBoxButton.OK,
  454. MessageBoxImage icon = MessageBoxImage.None,
  455. MessageBoxResult defaultResult = MessageBoxResult.None,
  456. MessageBoxOptions options = MessageBoxOptions.None)
  457. {
  458. return Show(owner, message, string.Empty, button, icon, defaultResult, options);
  459. }
  460. /// <summary>
  461. /// Show the message box with the specified parameters
  462. /// </summary>
  463. /// <param name="owner">The message box's parent window</param>
  464. /// <param name="message">The message text</param>
  465. /// <param name="details">The details part text</param>
  466. /// <param name="button">The buttons to be displayed</param>
  467. /// <param name="icon">The message's severity</param>
  468. /// <param name="defaultResult">The default button</param>
  469. /// <param name="options">Misc options</param>
  470. /// <returns>The user's selected button</returns>
  471. public static MessageBoxResult Show(Window owner, string message, string details = "",
  472. MessageBoxButton button = MessageBoxButton.OK,
  473. MessageBoxImage icon = MessageBoxImage.None,
  474. MessageBoxResult defaultResult = MessageBoxResult.None,
  475. MessageBoxOptions options = MessageBoxOptions.None)
  476. {
  477. var result = Application.Current.Dispatcher.Invoke(new Func<MessageBoxResult>(() =>
  478. {
  479. var messageBox = new WPFMessageBox(owner, message, details, button, icon, defaultResult, options);
  480. messageBox.ShowDialog();
  481. return messageBox.MessageBoxResult;
  482. }));
  483. return (MessageBoxResult)result;
  484. }
  485. #endregion
  486. }
  487. public static class IconHelper
  488. {
  489. [DllImport("gdi32.dll", SetLastError = true)]
  490. private static extern bool DeleteObject(IntPtr hObject);
  491. public static ImageSource ToImageSource(this Icon icon)
  492. {
  493. Bitmap bitmap = icon.ToBitmap();
  494. IntPtr hBitmap = bitmap.GetHbitmap();
  495. ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
  496. hBitmap,
  497. IntPtr.Zero,
  498. Int32Rect.Empty,
  499. BitmapSizeOptions.FromEmptyOptions());
  500. if (!DeleteObject(hBitmap))
  501. {
  502. throw new Win32Exception();
  503. }
  504. return wpfBitmap.GetAsFrozen() as ImageSource;
  505. }
  506. }
  507. }