using FWindSoft.WindowsApi; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace FWindSoft.Wpf { /// /// WinMessageTip.xaml 的交互逻辑 /// public partial class WinMessageTip : System.Windows.Window { public WinMessageTip(List items) { InitializeComponent(); this.DisableUseClose = true; this.ShowInTaskbar = false; this.AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click)); items.ForEach(item => StackButtons.Children.Add(CreateButton(item))); this.Loaded += WinMessageTip_Loaded; this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, (s,e)=> { Clipboard.SetDataObject(Message, false); })); } private void WinMessageTip_Loaded(object sender, RoutedEventArgs e) { if(DisableUseClose) { DisableClose(); } } private void Button_Click(object sender, RoutedEventArgs e) { Button button = e.Source as Button; if (button == null) return; TipButtonItem tipItem = button.Tag as TipButtonItem; try { if (tipItem == null) { Flag = 0; } else { Flag = Convert.ToInt32(tipItem.Flag); } } catch (Exception) { Flag = 0; } this.DialogResult = true; } internal bool DisableUseClose { get; set; } /// /// 选中标志 /// public int Flag { get; private set; } public string Message { get { return TxtMessage.Text; } set { this.TxtMessage.Text = value; } } private Button CreateButton(TipButtonItem item) { Button button = new Button(); button.MinWidth = 60; button.MinHeight = 23; button.Margin = new Thickness(10, 0, 10, 0); button.VerticalAlignment = VerticalAlignment.Center; button.Content = item.Content; button.IsDefault = item.IsDefault; button.IsCancel = item.IsCancel; if (item.ContentTemplate != null) { button.ContentTemplate = item.ContentTemplate; } button.Tag = item; return button; } private void DisableClose() { this.Closing += WinMessageTip_Closing; var interop = new WindowInteropHelper(this); int handle = interop.EnsureHandle().ToInt32(); CloseButton.Disable(handle); } private void WinMessageTip_Closing(object sender, System.ComponentModel.CancelEventArgs e) { //当关闭按钮不能用,并且dialogResult不是ture时,定义为atl+F4,关闭,这种关闭方式取消 if (DisableUseClose&&this.DialogResult!=true) { e.Cancel = true; } } } }