BFormRealTime.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Drawing;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace WWPipeLine.WPF
  15. {
  16. /// <summary>
  17. /// BForm.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class BFormRealTime : Window
  20. {
  21. readonly IResposeWPF m_IResposeWPF;
  22. public BFormRealTime(IResposeWPF respose)
  23. {
  24. InitializeComponent();
  25. Init();
  26. m_IResposeWPF = respose;
  27. this.m_DataGridEvents.RowHeaderWidth = 0;
  28. }
  29. public DataGrid DataGridEvents
  30. {
  31. get
  32. {
  33. return m_DataGridEvents;
  34. }
  35. }
  36. public System.Windows.Shapes.Ellipse EllipseAll
  37. {
  38. get
  39. {
  40. return m_EllipseAll;
  41. }
  42. }
  43. public System.Windows.Shapes.Ellipse EllipseWS
  44. {
  45. get
  46. {
  47. return m_EllipseDL;
  48. }
  49. }
  50. public System.Windows.Shapes.Ellipse EllipseYS
  51. {
  52. get
  53. {
  54. return m_EllipseDX;
  55. }
  56. }
  57. public System.Windows.Controls.Image ImageInfo
  58. {
  59. get
  60. {
  61. return m_ImageInfo;
  62. }
  63. }
  64. public System.Windows.Controls.Image ImagePan
  65. {
  66. get
  67. {
  68. return m_ImagePan;
  69. }
  70. }
  71. public System.Windows.Controls.Image ImageTerrain
  72. {
  73. get
  74. {
  75. return m_ImageTerrain;
  76. }
  77. }
  78. protected override void OnKeyUp(KeyEventArgs e)
  79. {
  80. base.OnKeyUp(e);
  81. if (e.Key == Key.Escape)
  82. this.Hide();
  83. }
  84. public void SetInfo(string infoName,NameValueCollection nvc)
  85. {
  86. if (this.Visibility == Visibility.Hidden) return;
  87. if (nvc.Count == 0)
  88. {
  89. m_InfoGrid.Visibility = Visibility.Hidden;
  90. return;
  91. }
  92. InfoScrollViewer.Height = this.m_InfoBorder.ActualHeight - 40;
  93. m_InfoGrid.Visibility = Visibility.Visible;
  94. m_InfoItems.Children.Clear();
  95. m_InfoTitle.Content = infoName;
  96. for (int i = 0;i < nvc.Count; i++)
  97. {
  98. var nvKey = nvc.GetKey(i);
  99. var nvValue = nvc.Get(i);
  100. WPF.PropertiesEx prop = new WPF.PropertiesEx(nvKey, nvValue);
  101. m_InfoItems.Children.Add(prop);
  102. }
  103. }
  104. void SetImageSource(System.Windows.Controls.Image image,string fileName)
  105. {
  106. string path = Commons.Paths.ApplicationResourcesDir + "\\" + fileName;
  107. Bitmap bitmap = new Bitmap(path);
  108. image.Tag = path;
  109. image.Source = BitmapToBitmapSource(bitmap);
  110. }
  111. void Init()
  112. {
  113. SetImageSource(m_ImageLogoBg, "BigScreen\\形状512.png");
  114. SetImageSource(m_ImageLogoICO, "BigScreen\\logo.png");
  115. SetImageSource(m_ImageTitle, "BigScreen\\云创经纬三维管线信息系统.png");
  116. SetImageSource(m_ImageLogoBg, "BigScreen\\形状512.png");
  117. SetImageSource(m_ImageGJXX, "BigScreen\\RightArrow.png");
  118. SetImageSource(m_ImageJCZB, "BigScreen\\RightArrow.png");
  119. SetImageSource(m_ImageExist, "exit.png");
  120. m_ImageExist.MouseUp += ImageExist_MouseUp;
  121. SetImageSource(m_ImageHome, "BigScreen\\ImageHome.png");
  122. m_ImageHome.MouseUp += ImageHome_MouseUp;
  123. m_ImageHome.MouseEnter += ImageButton_MouseEnter;
  124. m_ImageHome.MouseLeave += ImageButton_MouseLeave;
  125. SetImageSource(m_ImageInfo, "BigScreen\\ImageInfo.png");
  126. m_ImageInfo.MouseEnter += ImageButton_MouseEnter;
  127. m_ImageInfo.MouseLeave += ImageButton_MouseLeave;
  128. SetImageSource(m_ImagePan, "BigScreen\\ImagePan.png");
  129. m_ImagePan.MouseEnter += ImageButton_MouseEnter;
  130. m_ImagePan.MouseLeave += ImageButton_MouseLeave;
  131. SetImageSource(m_ImageTerrain, "BigScreen\\ImageTerrain.png");
  132. m_ImageTerrain.MouseEnter += ImageButton_MouseEnter;
  133. m_ImageTerrain.MouseLeave += ImageButton_MouseLeave;
  134. SetImageSource(AlertImage, "BigScreen\\alert.png");
  135. InfoScrollViewer.MouseEnter += InfoScrollViewer_MouseEnter;
  136. InfoScrollViewer.MouseLeave += InfoScrollViewer_MouseLeave;
  137. string alertTime = DateTime.Now.AddMinutes(-3).ToString("HH:mm:ss");
  138. m_LabelAlertTime.Content =alertTime;
  139. Grid oo = this.Content as Grid;
  140. UIElementCollection Childrens = oo.Children;
  141. foreach (UIElement ui in Childrens)
  142. {
  143. IEnumerable<Grid> gds = FindVisualChildren<Grid>(ui);
  144. foreach (Grid gd in gds)
  145. {
  146. if (gd.Tag != null && gd.Tag.ToString() == "BTN")
  147. {
  148. /*
  149. gd.MouseEnter += Grid_MouseEnter;
  150. gd.MouseLeave += Grid_MouseLeave;
  151. */
  152. }
  153. }
  154. IEnumerable<Ellipse> ee = FindVisualChildren<System.Windows.Shapes.Ellipse>(ui);
  155. foreach(Ellipse e in ee)
  156. {
  157. if (e.Tag != null && e.Tag.ToString() == "TYPES")
  158. {
  159. e.Opacity = .3;
  160. e.Cursor = System.Windows.Input.Cursors.Hand;
  161. if (e.Parent is Grid)
  162. {
  163. Grid g = e.Parent as Grid;
  164. string path = Commons.Paths.ApplicationResourcesDir + "\\BigScreen\\" + e.Name.Replace("m_", "") + ".png";
  165. g.Tag = path;
  166. e.MouseEnter += MenuMouseEnter;
  167. e.MouseLeave += MenuMouseLeave;
  168. if (!System.IO.File.Exists(path)) continue;
  169. var bitmap = new Bitmap(path);
  170. g.Background = new System.Windows.Media.ImageBrush(BitmapToBitmapSource(bitmap));
  171. }
  172. }
  173. }
  174. IEnumerable<TextBlock> texts = FindVisualChildren<TextBlock>(ui);
  175. foreach(TextBlock txt in texts)
  176. {
  177. if (txt.Tag == null || txt.Tag.ToString() != "ZB") continue;
  178. txt.MouseUp += Txt_MouseUp;
  179. ZBNames.Add(txt);
  180. }
  181. }
  182. FlashAlertMsg();
  183. }
  184. private void FlashAlertMsg()
  185. {
  186. Task.Factory.StartNew(() => {
  187. while (true)
  188. {
  189. Dispatcher.Invoke(new System.Action(() =>
  190. {
  191. var margin = AlertPanel.Margin;
  192. var actLeft = margin.Left;
  193. if (actLeft >= 1) actLeft--;
  194. if (actLeft <= 0) actLeft = this.AlertPanel.ActualWidth;
  195. AlertPanel.Margin = new Thickness(actLeft, margin.Top, margin.Right, margin.Bottom);
  196. DateTime dtime = DateTime.Now;
  197. string sxw = "上午";
  198. if (dtime.Hour > 12) sxw = "下午";
  199. string time = dtime.ToString("h:mm:ss");
  200. m_TimeLabel.Content = sxw + " " + time;
  201. m_DateLabel.Content = dtime.ToString("yyyy MM dd");
  202. }));
  203. Thread.Sleep(10);
  204. }
  205. }).ContinueWith((t) => {
  206. }, TaskScheduler.FromCurrentSynchronizationContext());
  207. }
  208. private void ImageButton_MouseEnter(object sender, MouseEventArgs e)
  209. {
  210. System.Windows.Controls.Image img = sender as System.Windows.Controls.Image;
  211. string defaultPath = img.Tag as string;
  212. string bluePath = defaultPath.Replace(".png","-blue.png");
  213. Bitmap bitmap = new Bitmap(bluePath);
  214. img.Source = BitmapToBitmapSource(bitmap);
  215. }
  216. private void ImageButton_MouseLeave(object sender, MouseEventArgs e)
  217. {
  218. System.Windows.Controls.Image img = sender as System.Windows.Controls.Image;
  219. string defaultPath = img.Tag as string;
  220. Bitmap bitmap = new Bitmap(defaultPath);
  221. img.Source = BitmapToBitmapSource(bitmap);
  222. }
  223. private void MenuMouseLeave(object sender, MouseEventArgs e)
  224. {
  225. Ellipse ell = sender as Ellipse;
  226. Grid g = ell.Parent as Grid;
  227. string defaultPath = g.Tag as string;
  228. Bitmap bitmap = new Bitmap(defaultPath);
  229. g.Background = new System.Windows.Media.ImageBrush(BitmapToBitmapSource(bitmap));
  230. }
  231. private void MenuMouseEnter(object sender, MouseEventArgs e)
  232. {
  233. Ellipse ell = sender as Ellipse;
  234. Grid g = ell.Parent as Grid;
  235. string defaultPath = g.Tag as string;
  236. string blueBg = defaultPath.Replace(".png","-blue.png");
  237. Bitmap bitmap = new Bitmap(blueBg);
  238. g.Background = new System.Windows.Media.ImageBrush(BitmapToBitmapSource(bitmap));
  239. }
  240. public List<TextBlock> ZBNames = new List<TextBlock>();
  241. private void Txt_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
  242. {
  243. TextBlock tb = sender as TextBlock;
  244. if (tb.Text.StartsWith("√"))
  245. {
  246. tb.Text = tb.Text.Substring(2);
  247. }
  248. else
  249. {
  250. tb.Text = "√\n" + tb.Text;
  251. }
  252. }
  253. private void Sp_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
  254. {
  255. StackPanel sp = sender as StackPanel;
  256. sp.Opacity = .5;
  257. }
  258. private void Sp_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
  259. {
  260. StackPanel sp = sender as StackPanel;
  261. sp.Opacity = 1;
  262. }
  263. private void Grid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
  264. {
  265. Grid gd = sender as Grid;
  266. gd.Opacity = .2;
  267. }
  268. private void Grid_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
  269. {
  270. Grid gd = sender as Grid;
  271. gd.Opacity = 1;
  272. }
  273. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  274. {
  275. if (depObj != null)
  276. {
  277. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  278. {
  279. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  280. if (child != null && child is T t)
  281. {
  282. yield return t;
  283. }
  284. foreach (T childOfChild in FindVisualChildren<T>(child))
  285. {
  286. yield return childOfChild;
  287. }
  288. }
  289. }
  290. }
  291. private void InfoScrollViewer_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
  292. {
  293. InfoScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
  294. }
  295. private void InfoScrollViewer_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
  296. {
  297. InfoScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  298. }
  299. private void ImageHome_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
  300. {
  301. this.m_IResposeWPF.ResposeWPF("VIEWENTIRE");
  302. }
  303. private void ImageExist_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
  304. {
  305. this.Hide();
  306. }
  307. //public DockPanel StackPanel01
  308. //{
  309. // get { return this.m_PanelLeftCahrt01; }
  310. //}
  311. public Grid StackPanel02
  312. {
  313. get { return this.m_PanelLeftCahrt02; }
  314. }
  315. public Grid StackPanel03
  316. {
  317. get { return this.m_PanelLeftCahrt03; }
  318. }
  319. [DllImport("gdi32.dll", SetLastError = true)]
  320. private static extern bool DeleteObject(IntPtr hObject);
  321. //将 Bitmap转换为BitmapSource
  322. //使用过System.Drawing.Bitmap后一定要用DeleteObject释放掉对象,不然内存不释放,很快系统内存就消耗光了。
  323. private static ImageSource BitmapToBitmapSource(Bitmap bitmap)
  324. {
  325. IntPtr hBitmap = bitmap.GetHbitmap();
  326. ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
  327. hBitmap,
  328. IntPtr.Zero,
  329. Int32Rect.Empty,
  330. BitmapSizeOptions.FromEmptyOptions());
  331. if (!DeleteObject(hBitmap))//记得要进行内存释放。否则会有内存不足的报错。
  332. {
  333. throw new System.ComponentModel.Win32Exception();
  334. }
  335. return wpfBitmap;
  336. }
  337. }
  338. }