终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

354 líneas
12 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Interop;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using System.Windows.Threading;
  18. namespace BPASmartClient.SCADAControl.Windows
  19. {
  20. /// <summary>
  21. /// Notifiaction.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class Notifiaction : Window
  24. {
  25. private const byte MAX_NOTIFICATIONS = 10;
  26. private readonly ObservableCollection<NotifiactionModel> buffer = new ObservableCollection<NotifiactionModel>();
  27. private ObservableCollection<NotifiactionModel> NotifiactionList = new ObservableCollection<NotifiactionModel>();
  28. private const double topOffset = 40;
  29. private const double leftOffset = 350;
  30. public DispatcherTimer uptimer = null;
  31. public Notifiaction()
  32. {
  33. InitializeComponent();
  34. this.NotificationsControl.DataContext = this.NotifiactionList;
  35. this.Top = 130;
  36. this.Left = SystemParameters.WorkArea.Left + SystemParameters.WorkArea.Width - this.Width;
  37. this.Height = SystemParameters.WorkArea.Height - 460;
  38. this.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
  39. if (uptimer == null)
  40. uptimer = new DispatcherTimer();
  41. uptimer.Tick += new EventHandler(_timer_Tick);
  42. uptimer.Interval = TimeSpan.FromSeconds(1);
  43. uptimer.Start();
  44. }
  45. #region 显示控制
  46. /// <summary>
  47. /// 线程消失函数
  48. /// </summary>
  49. /// <param name="sender"></param>
  50. /// <param name="e"></param>
  51. private void _timer_Tick(object sender, EventArgs e)
  52. {
  53. NotifiactionList.ToList().ForEach(par =>
  54. {
  55. if (par.time > 0)
  56. par.time = par.time - 1;
  57. });
  58. List<NotifiactionModel> Notifia = NotifiactionList.ToList().FindAll(x => x.time <= 0);
  59. foreach (var item in Notifia)
  60. {
  61. RemoveNotification(item);
  62. }
  63. }
  64. /// <summary>
  65. /// 清除窗体显示函数
  66. /// </summary>
  67. /// <param name="type"></param>
  68. public void Clear(EnumPromptType type)
  69. {
  70. List<NotifiactionModel> buff = buffer.ToList().FindAll(x => x.NotifiactionType == type);
  71. foreach (var item in buff)
  72. {
  73. buffer.Remove(item);
  74. }
  75. List<NotifiactionModel> Notifia = NotifiactionList.ToList().FindAll(x => x.NotifiactionType == type);
  76. foreach (var item in Notifia)
  77. {
  78. NotifiactionList.Remove(item);
  79. }
  80. }
  81. /// <summary>
  82. /// 增加一个消息弹框
  83. /// </summary>
  84. /// <param name="notification"></param>
  85. public void AddNotifiaction(NotifiactionModel notification)
  86. {
  87. try
  88. {
  89. var window = Window.GetWindow(notification.window);
  90. var intPtr = new WindowInteropHelper(window).Handle;
  91. var screen = System.Windows.Forms.Screen.FromHandle(intPtr);
  92. this.Left = screen.WorkingArea.Left + screen.WorkingArea.Width / 2 - this.Width / 2;// screen.WorkingArea.Left + screen.WorkingArea.Width - this.Width;
  93. WindowInteropHelper helper = new WindowInteropHelper(this);
  94. helper.Owner = new WindowInteropHelper(notification.window).Handle;
  95. this.Topmost = false;
  96. if (notification.window != null)
  97. {
  98. if (notification.window.WindowState == WindowState.Minimized)
  99. {
  100. this.WindowState = WindowState.Minimized;
  101. }
  102. else
  103. {
  104. this.WindowState = WindowState.Normal;
  105. }
  106. }
  107. if (NotifiactionList.Count + 1 > MAX_NOTIFICATIONS)//且数量超过了限制
  108. {
  109. buffer.Add(notification);
  110. }
  111. else//已经显示的没找到,数量没有超过限制
  112. {
  113. NotifiactionList.Add(notification);
  114. }
  115. if (NotifiactionList.Count > 0 && !IsActive)
  116. this.Show();
  117. }
  118. catch (Exception ex)
  119. {
  120. }
  121. }
  122. /// <summary>
  123. /// 将显示窗体移除
  124. /// </summary>
  125. /// <param name="notification"></param>
  126. public void RemoveNotification(NotifiactionModel notification)
  127. {
  128. if (notification == null)
  129. return;
  130. if (NotifiactionList.Contains(notification))
  131. NotifiactionList.Remove(notification);
  132. if (buffer.Count > 0)
  133. {
  134. if (notification.NotifiactionType == EnumPromptType.Warn)
  135. {
  136. NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Info);
  137. if (mode == null)
  138. {
  139. NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Success);
  140. if (mode1 == null)
  141. {
  142. NotifiactionList.Add(buffer[0]);
  143. buffer.RemoveAt(0);
  144. }
  145. else
  146. {
  147. NotifiactionList.Add(mode1);
  148. buffer.Remove(mode);
  149. }
  150. }
  151. else
  152. {
  153. NotifiactionList.Add(mode);
  154. buffer.Remove(mode);
  155. }
  156. }
  157. else if (notification.NotifiactionType == EnumPromptType.Info)
  158. {
  159. NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Success);
  160. if (mode == null)
  161. {
  162. NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Warn);
  163. if (mode1 == null)
  164. {
  165. NotifiactionList.Add(buffer[0]);
  166. buffer.RemoveAt(0);
  167. }
  168. else
  169. {
  170. NotifiactionList.Add(mode1);
  171. buffer.Remove(mode);
  172. }
  173. }
  174. else
  175. {
  176. NotifiactionList.Add(mode);
  177. buffer.Remove(mode);
  178. }
  179. }
  180. else if (notification.NotifiactionType == EnumPromptType.Success)
  181. {
  182. NotifiactionModel mode = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Warn);
  183. if (mode == null)
  184. {
  185. NotifiactionModel mode1 = buffer.ToList().Find(par => par.NotifiactionType == EnumPromptType.Info);
  186. if (mode1 == null)
  187. {
  188. NotifiactionList.Add(buffer[0]);
  189. buffer.RemoveAt(0);
  190. }
  191. else
  192. {
  193. NotifiactionList.Add(mode1);
  194. buffer.Remove(mode);
  195. }
  196. }
  197. else
  198. {
  199. NotifiactionList.Add(mode);
  200. buffer.Remove(mode);
  201. }
  202. }
  203. else
  204. {
  205. NotifiactionList.Add(buffer[0]);
  206. buffer.RemoveAt(0);
  207. }
  208. }
  209. //Close window if there's nothing to show
  210. if (NotifiactionList.Count < 1)
  211. Hide();
  212. }
  213. private void NoticeGrid_SizeChanged(object sender, SizeChangedEventArgs e)
  214. {
  215. }
  216. #endregion
  217. #region 窗体事件
  218. /// <summary>
  219. /// 关闭窗口
  220. /// </summary>
  221. /// <param name="sender"></param>
  222. /// <param name="e"></param>
  223. private void PART_CloseButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  224. {
  225. Image border = (Image)sender;
  226. NotifiactionModel mode = border.DataContext as NotifiactionModel;
  227. RemoveNotification(mode);
  228. }
  229. #endregion
  230. }
  231. public class NotifiactionModel : INotifyPropertyChanged
  232. {
  233. private int _count;
  234. private string _content;
  235. private int _time;
  236. /// <summary>
  237. /// Id不需要赋值
  238. /// </summary>
  239. public string Id { get; set; }
  240. /// <summary>
  241. /// 通知标题
  242. /// </summary>
  243. public string Title { get; set; }
  244. /// <summary>
  245. /// 通知内容
  246. /// </summary>
  247. public string Content
  248. {
  249. get { return _content; }
  250. set
  251. {
  252. _content = value;
  253. if (this.PropertyChanged != null)
  254. {
  255. this.PropertyChanged(this, new PropertyChangedEventArgs("Content"));
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// 通知内容-显示全体
  261. /// </summary>
  262. public string ContentToolTip { get; set; }
  263. /// <summary>
  264. /// 通知类型
  265. /// </summary>
  266. private EnumPromptType enumPromptType;
  267. public EnumPromptType NotifiactionType
  268. {
  269. get { return enumPromptType; }
  270. set
  271. {
  272. enumPromptType = value;
  273. switch (enumPromptType)
  274. {
  275. case EnumPromptType.Info:
  276. color = Color.FromRgb(35, 132, 190);
  277. break;
  278. case EnumPromptType.Warn:
  279. color = Color.FromRgb(255, 170, 22);
  280. break;
  281. case EnumPromptType.Error:
  282. color = Color.FromRgb(245, 49, 49);
  283. break;
  284. case EnumPromptType.Success:
  285. color = Color.FromRgb(28, 194, 59);
  286. break;
  287. }
  288. if (this.PropertyChanged != null)
  289. this.PropertyChanged(this, new PropertyChangedEventArgs("enumPromptType"));
  290. }
  291. }
  292. /// <summary>
  293. /// 背景颜色主题
  294. /// </summary>
  295. public Color color { get; set; }
  296. /// <summary>
  297. /// 文本颜色
  298. /// </summary>
  299. public Color textColor { get; set; }
  300. /// <summary>
  301. /// 外部windows
  302. /// </summary>
  303. public System.Windows.Window window { get; set; }
  304. /// <summary>
  305. /// 弹窗显示时长
  306. /// </summary>
  307. public int time
  308. {
  309. get { return _time; }
  310. set
  311. {
  312. _time = value;
  313. if (this.PropertyChanged != null)
  314. this.PropertyChanged(this, new PropertyChangedEventArgs("time"));
  315. }
  316. }
  317. public int count
  318. {
  319. get { return _count; }
  320. set
  321. {
  322. _count = value;
  323. if (this.PropertyChanged != null)
  324. this.PropertyChanged(this, new PropertyChangedEventArgs("count"));
  325. }
  326. }//超过1显示数量
  327. /// <summary>
  328. /// 设置默认之
  329. /// </summary>
  330. public NotifiactionModel()
  331. {
  332. Id = Guid.NewGuid().ToString();
  333. time = 5;
  334. count = 1;
  335. }
  336. public event PropertyChangedEventHandler PropertyChanged;
  337. }
  338. }