WinMessageTip.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using FWindSoft.WindowsApi;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Interop;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace FWindSoft.Wpf
  18. {
  19. /// <summary>
  20. /// WinMessageTip.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class WinMessageTip : System.Windows.Window
  23. {
  24. public WinMessageTip(List<TipButtonItem> items)
  25. {
  26. InitializeComponent();
  27. this.DisableUseClose = true;
  28. this.ShowInTaskbar = false;
  29. this.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click));
  30. items.ForEach(item => StackButtons.Children.Add(CreateButton(item)));
  31. this.Loaded += WinMessageTip_Loaded;
  32. this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, (s,e)=> {
  33. Clipboard.SetDataObject(Message, false);
  34. }));
  35. }
  36. private void WinMessageTip_Loaded(object sender, RoutedEventArgs e)
  37. {
  38. if(DisableUseClose)
  39. {
  40. DisableClose();
  41. }
  42. }
  43. private void Button_Click(object sender, RoutedEventArgs e)
  44. {
  45. Button button = e.Source as Button;
  46. if (button == null)
  47. return;
  48. TipButtonItem tipItem = button.Tag as TipButtonItem;
  49. try
  50. {
  51. if (tipItem == null)
  52. {
  53. Flag = 0;
  54. }
  55. else
  56. {
  57. Flag = Convert.ToInt32(tipItem.Flag);
  58. }
  59. }
  60. catch (Exception)
  61. {
  62. Flag = 0;
  63. }
  64. this.DialogResult = true;
  65. }
  66. internal bool DisableUseClose { get; set; }
  67. /// <summary>
  68. /// 选中标志
  69. /// </summary>
  70. public int Flag { get; private set; }
  71. public string Message {
  72. get { return TxtMessage.Text; }
  73. set { this.TxtMessage.Text = value;
  74. }
  75. }
  76. private Button CreateButton(TipButtonItem item)
  77. {
  78. Button button = new Button();
  79. button.MinWidth = 60;
  80. button.MinHeight = 23;
  81. button.Margin = new Thickness(10, 0, 10, 0);
  82. button.VerticalAlignment = VerticalAlignment.Center;
  83. button.Content = item.Content;
  84. button.IsDefault = item.IsDefault;
  85. button.IsCancel = item.IsCancel;
  86. if (item.ContentTemplate != null)
  87. {
  88. button.ContentTemplate = item.ContentTemplate;
  89. }
  90. button.Tag = item;
  91. return button;
  92. }
  93. private void DisableClose()
  94. {
  95. this.Closing += WinMessageTip_Closing;
  96. var interop = new WindowInteropHelper(this);
  97. int handle = interop.EnsureHandle().ToInt32();
  98. CloseButton.Disable(handle);
  99. }
  100. private void WinMessageTip_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  101. {
  102. //当关闭按钮不能用,并且dialogResult不是ture时,定义为atl+F4,关闭,这种关闭方式取消
  103. if (DisableUseClose&&this.DialogResult!=true)
  104. {
  105. e.Cancel = true;
  106. }
  107. }
  108. }
  109. }