using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; 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.Shapes; using System.Windows.Threading; namespace BPASmartClient.SCADAControl.Windows { /// /// Notifiaction.xaml 的交互逻辑 /// public partial class Notifiaction : Window { private const byte MAX_NOTIFICATIONS = 10; private readonly ObservableCollection buffer = new ObservableCollection(); private ObservableCollection NotifiactionList = new ObservableCollection(); private const double topOffset = 40; private const double leftOffset = 350; public DispatcherTimer uptimer = null; public Notifiaction() { InitializeComponent(); this.NotificationsControl.DataContext = this.NotifiactionList; this.Top = 130; this.Left = SystemParameters.WorkArea.Left + SystemParameters.WorkArea.Width - this.Width; this.Height = SystemParameters.WorkArea.Height - 460; this.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual; if (uptimer == null) uptimer = new DispatcherTimer(); uptimer.Tick += new EventHandler(_timer_Tick); uptimer.Interval = TimeSpan.FromSeconds(1); uptimer.Start(); } #region 显示控制 /// /// 线程消失函数 /// /// /// private void _timer_Tick(object sender, EventArgs e) { NotifiactionList.ToList().ForEach(par => { if (par.time > 0) par.time = par.time - 1; }); List Notifia = NotifiactionList.ToList().FindAll(x => x.time <= 0); foreach (var item in Notifia) { RemoveNotification(item); } } /// /// 清除窗体显示函数 /// /// public void Clear(EnumPromptType type) { List buff = buffer.ToList().FindAll(x => x.NotifiactionType == type); foreach (var item in buff) { buffer.Remove(item); } List Notifia = NotifiactionList.ToList().FindAll(x => x.NotifiactionType == type); foreach (var item in Notifia) { NotifiactionList.Remove(item); } } /// /// 增加一个消息弹框 /// /// public void AddNotifiaction(NotifiactionModel notification) { try { var window = Window.GetWindow(notification.window); var intPtr = new WindowInteropHelper(window).Handle; var screen = System.Windows.Forms.Screen.FromHandle(intPtr); this.Left = screen.WorkingArea.Left + screen.WorkingArea.Width / 2 - this.Width / 2;// screen.WorkingArea.Left + screen.WorkingArea.Width - this.Width; WindowInteropHelper helper = new WindowInteropHelper(this); helper.Owner = new WindowInteropHelper(notification.window).Handle; this.Topmost = false; if (notification.window != null) { if (notification.window.WindowState == WindowState.Minimized) { this.WindowState = WindowState.Minimized; } else { this.WindowState = WindowState.Normal; } } if (NotifiactionList.Count + 1 > MAX_NOTIFICATIONS)//且数量超过了限制 { buffer.Add(notification); } else//已经显示的没找到,数量没有超过限制 { NotifiactionList.Add(notification); } if (NotifiactionList.Count > 0 && !IsActive) this.Show(); } catch (Exception ex) { } } /// /// 将显示窗体移除 /// /// public void RemoveNotification(NotifiactionModel notification) { if (notification == null) return; if (NotifiactionList.Contains(notification)) NotifiactionList.Remove(notification); if (buffer.Count > 0) { if (notification.NotifiactionType == EnumPromptType.Warn) { NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Info); if (mode == null) { NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Success); if (mode1 == null) { NotifiactionList.Add(buffer[0]); buffer.RemoveAt(0); } else { NotifiactionList.Add(mode1); buffer.Remove(mode); } } else { NotifiactionList.Add(mode); buffer.Remove(mode); } } else if (notification.NotifiactionType == EnumPromptType.Info) { NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Success); if (mode == null) { NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Warn); if (mode1 == null) { NotifiactionList.Add(buffer[0]); buffer.RemoveAt(0); } else { NotifiactionList.Add(mode1); buffer.Remove(mode); } } else { NotifiactionList.Add(mode); buffer.Remove(mode); } } else if (notification.NotifiactionType == EnumPromptType.Success) { NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Warn); if (mode == null) { NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Info); if (mode1 == null) { NotifiactionList.Add(buffer[0]); buffer.RemoveAt(0); } else { NotifiactionList.Add(mode1); buffer.Remove(mode); } } else { NotifiactionList.Add(mode); buffer.Remove(mode); } } else { NotifiactionList.Add(buffer[0]); buffer.RemoveAt(0); } } //Close window if there's nothing to show if (NotifiactionList.Count < 1) Hide(); } private void NoticeGrid_SizeChanged(object sender, SizeChangedEventArgs e) { } #endregion #region 窗体事件 /// /// 关闭窗口 /// /// /// private void PART_CloseButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Image border = (Image)sender; NotifiactionModel mode = border.DataContext as NotifiactionModel; RemoveNotification(mode); } #endregion } public class NotifiactionModel : INotifyPropertyChanged { private int _count; private string _content; private int _time; /// /// Id不需要赋值 /// public string Id { get; set; } /// /// 通知标题 /// public string Title { get; set; } /// /// 通知内容 /// public string Content { get { return _content; } set { _content = value; if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("Content")); } } } /// /// 通知内容-显示全体 /// public string ContentToolTip { get; set; } /// /// 通知类型 /// private EnumPromptType enumPromptType; public EnumPromptType NotifiactionType { get { return enumPromptType; } set { enumPromptType = value; switch (enumPromptType) { case EnumPromptType.Info: color = Color.FromRgb(35, 132, 190); break; case EnumPromptType.Warn: color = Color.FromRgb(255, 170, 22); break; case EnumPromptType.Error: color = Color.FromRgb(245, 49, 49); break; case EnumPromptType.Success: color = Color.FromRgb(28, 194, 59); break; } if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("enumPromptType")); } } /// /// 背景颜色主题 /// public Color color { get; set; } /// /// 文本颜色 /// public Color textColor { get; set; } /// /// 外部windows /// public System.Windows.Window window { get; set; } /// /// 弹窗显示时长 /// public int time { get { return _time; } set { _time = value; if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("time")); } } public int count { get { return _count; } set { _count = value; if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("count")); } }//超过1显示数量 /// /// 设置默认之 /// public NotifiactionModel() { Id = Guid.NewGuid().ToString(); time = 5; count = 1; } public event PropertyChangedEventHandler PropertyChanged; } }