ShellView.xaml.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. using Prism.Regions;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using Venus_Themes.CustomControls;
  5. using Venus_Unity;
  6. using System.Linq;
  7. using System;
  8. using Venus_MainPages.Views;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. using Venus_Core;
  12. using System.Reflection;
  13. using Venus_MainPages.Unity;
  14. using MECF.Framework.Common.DataCenter;
  15. using MECF.Framework.Common.OperationCenter;
  16. using Venus_MainPages.ViewModels;
  17. using MECF.Framework.Common.CommonData;
  18. using System.Windows.Threading;
  19. using Venus_Themes.Unity;
  20. using MECF.Framework.Common.Equipment;
  21. using WinInterop = System.Windows.Interop;
  22. using System.Runtime.InteropServices;
  23. using System.IO;
  24. using System.Windows.Media.Imaging;
  25. using Venus_MainPages.Roles;
  26. using WPF.Themes.UserControls;
  27. using System.Diagnostics;
  28. using System.Windows.Input;
  29. namespace Venus_UI.Views
  30. {
  31. /// <summary>
  32. /// ShellView.xaml 的交互逻辑
  33. /// </summary>
  34. public partial class ShellView : Window
  35. {
  36. IRegionManager m_regionManager;
  37. //IRegionNavigationService m_regionNavigationService;
  38. //List<VenusMenu> VenusMenu;
  39. List<RoleDefine> RoleDefines;
  40. public List<TabControl> centerTabViews = new List<TabControl>();
  41. public List<AduRadioButtonIcon> buttonList = new List<AduRadioButtonIcon>();
  42. readonly DispatcherTimer timer = new DispatcherTimer();
  43. readonly User currentUser;
  44. RoleDefine currentRole;
  45. int xPosition;
  46. int logoutTime;
  47. Stopwatch m_logoutStopWatch;
  48. public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(
  49. "Time", typeof(DateTime), typeof(ShellView));
  50. public DateTime Time
  51. {
  52. get { return (DateTime)this.GetValue(TimeProperty); }
  53. set { this.SetValue(TimeProperty, value); }
  54. }
  55. public ShellView(IRegionManager regionManager)
  56. {
  57. InitializeComponent();
  58. LoginView loginView = new LoginView();
  59. loginView.ShowDialog();
  60. currentUser = loginView.CurrentUser;
  61. if (!loginView.IsLoginSuccess)
  62. {
  63. this.Close();
  64. }
  65. loginView = null;
  66. // 强制进行垃圾回收
  67. GC.Collect();
  68. GC.WaitForPendingFinalizers();
  69. GC.Collect();
  70. m_regionManager = regionManager;
  71. //m_regionNavigationService = regionNavigationService;
  72. m_regionManager.RegisterViewWithRegion("TopRegion", typeof(Venus_MainPages.Views.TopView));
  73. UIEvents.PMDoorRaiseChangedEvent += UIEvents_PMDoorRaiseChangedEvent;
  74. UIEvents.LLTDoorRaiseChangedEvent += UIEvents_LLTDoorRaiseChangedEvent;
  75. UIEvents.LLEDoorRaiseChangedEvent += UIEvents_LLEDoorRaiseChangedEvent;
  76. UIEvents.ChamberCreateDeleteWaferEvent += UIEvents_ChamberCreateDeleteWaferEvent;
  77. UIEvents.InitRaiseChangedEvent += UIEvents_InitRaiseChangedEvent;
  78. UIEvents.AbortRaiseChangedEvent += UIEvents_AbortRaiseChangedEvent;
  79. UIEvents.IncludeRaiseChangedEvent += UIEvents_IncludeRaiseChangedEvent;
  80. UIEvents.OnlineRaiseChangedEvent += UIEvents_OnlineRaiseChangedEvent;
  81. VenusGlobalEvents.SlotRightClickChangedEvent += Instance_SlotRightClickChangedEvent;
  82. VenusGlobalEvents.SlotWaferTransferEvent += Instance_SlotStartTransferEvent;
  83. this.SourceInitialized += (o, e) =>
  84. {
  85. System.IntPtr handler = (new WinInterop.WindowInteropHelper(this)).Handle;
  86. WinInterop.HwndSource.FromHwnd(handler).AddHook(new WinInterop.HwndSourceHook(WindowProc));
  87. };
  88. this.MaxHeight = SystemParameters.WorkArea.Height;
  89. ResizeMode = ResizeMode.CanMinimize;
  90. this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
  91. }
  92. public ShellView()
  93. {
  94. }
  95. void timer_Tick(object sender, EventArgs e)
  96. {
  97. if (Anychange.needchange)
  98. {
  99. Anystationchange(Anychange.menuname, Anychange.args);
  100. Anychange.needchange = false;
  101. }
  102. Tag = DateTime.Now;
  103. if (currentRole.IsLocked)
  104. {
  105. if (xPosition == (int)(Mouse.GetPosition(this).X))
  106. {
  107. if (m_logoutStopWatch.ElapsedMilliseconds > currentRole.LockTime * 1000)
  108. {
  109. RestartUI();
  110. }
  111. }
  112. else
  113. {
  114. m_logoutStopWatch.Restart();
  115. }
  116. }
  117. xPosition = (int)(Mouse.GetPosition(this).X);
  118. }
  119. private void RestartUI()
  120. {
  121. // 获取当前执行的程序集
  122. Assembly assembly = Assembly.GetExecutingAssembly();
  123. // 获取当前执行的程序集的全名
  124. AssemblyName assemblyName = assembly.GetName();
  125. // 构建一个新的进程启动信息,以便重新启动当前应用程序
  126. ProcessStartInfo startInfo = new ProcessStartInfo
  127. {
  128. FileName = assembly.Location,
  129. UseShellExecute = true
  130. };
  131. // 关闭当前应用程序
  132. Application.Current.Shutdown();
  133. // 创建一个新的进程来运行当前应用程序
  134. Process.Start(startInfo);
  135. }
  136. private void Instance_SlotRightClickChangedEvent(OpenSEMI.Ctrlib.Controls.Slot slot)
  137. {
  138. if (slot != null)
  139. {
  140. ContextMenu cm = ContextMenuManager.Instance.GetSlotMenus(slot);
  141. if (cm != null)
  142. {
  143. slot.ContextMenu = cm;
  144. }
  145. return;
  146. }
  147. }
  148. private void Instance_SlotStartTransferEvent(OpenSEMI.Ctrlib.Controls.DragDropEventArgs e)
  149. {
  150. string info = " from " + e.TranferFrom.ModuleID + " slot " + (e.TranferFrom.SlotID + 1).ToString() + " to " + e.TranferTo.ModuleID + " slot " + (e.TranferTo.SlotID + 1).ToString();
  151. string message = "Are you sure to transfer the wafer: \n" + info;
  152. WaferDialogViewModel vm = new WaferDialogViewModel();
  153. vm.ConfirmText = message;
  154. WaferDialogView dialog = new WaferDialogView()
  155. {
  156. Owner = Application.Current.MainWindow,
  157. DataContext = vm,
  158. Height = 300,
  159. Width = 400
  160. };
  161. bool alignflag;
  162. bool coolingflag;
  163. double angel = 0;
  164. double coolingtime = 0;
  165. if (dialog.ShowDialog() == true)
  166. {
  167. alignflag = (bool)dialog.AlignFlag;
  168. coolingflag = (bool)dialog.CoolingFlag;
  169. if (alignflag && !string.IsNullOrEmpty(dialog.Angle))
  170. {
  171. angel = Convert.ToDouble(dialog.Angle);
  172. }
  173. if (coolingflag && !string.IsNullOrEmpty(dialog.CoolingTime))
  174. {
  175. coolingtime = Convert.ToDouble(dialog.CoolingTime);
  176. }
  177. //from robot to robot is illegal. Must be stop
  178. if (e.TranferFrom.ModuleID == e.TranferTo.ModuleID && (e.TranferTo.ModuleID == ModuleName.EfemRobot.ToString() || e.TranferTo.ModuleID == ModuleName.TMRobot.ToString()))
  179. {
  180. }
  181. else
  182. if (e.TranferTo.ModuleID == "Aligner1" || string.IsNullOrEmpty(e.TranferTo.ModuleID))
  183. InvokeClient.Instance.Service.DoOperation("System.MoveWafer",
  184. e.TranferFrom.ModuleID, e.TranferFrom.SlotID, "Aligner1", 0, true, 0, coolingflag, coolingtime, "");
  185. InvokeClient.Instance.Service.DoOperation("System.MoveWafer",
  186. e.TranferFrom.ModuleID, e.TranferFrom.SlotID, e.TranferTo.ModuleID, e.TranferTo.SlotID, alignflag, angel, coolingflag, coolingtime, "");
  187. }
  188. else
  189. {
  190. ModuleManager.OnFlashWafer(new FlashWaferInformation() { ModuleName = e.TranferFrom.ModuleID, SoltId = e.TranferFrom.SlotID });
  191. //ModuleManager.OnFlashWafer(new FlashWaferInformation() { ModuleName = e.TranferTo.ModuleID, SoltId = e.TranferTo.SlotID });
  192. }
  193. e.TranferFrom.ClearDragDropStatus();
  194. e.TranferTo.ClearDragDropStatus();
  195. }
  196. private void CustomWnd_Loaded(object sender, RoutedEventArgs e)
  197. {
  198. var h1 = SystemParameters.WorkArea.Height;
  199. var h2 = SystemParameters.PrimaryScreenHeight;
  200. //this.ResizeMode = ResizeMode.CanResize;
  201. //System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / SystemParameters.PrimaryScreenWidth
  202. //VenusMenu = SerializeHelper.Instance.ReadFromJsonFile<List<VenusMenu>>($"Config/UIMenu.json");
  203. //App.currentUser
  204. string configPath = "";
  205. string uiConfig = "";
  206. object obj2 = QueryDataClient.Instance.Service.GetData("System.ConfigType");
  207. var configType = (ConfigType)Convert.ToInt32(obj2);
  208. //if (configType == ConfigType.Other || configType == ConfigType.Kepler2300 || configType == ConfigType.Kepler2200)
  209. //{
  210. // uiConfig = "_Kepler";
  211. //}
  212. //else if (configType == ConfigType.VenusSE)
  213. //{
  214. // uiConfig = "_VenusSE";
  215. //}
  216. //else if (configType == ConfigType.VenusDE)
  217. //{
  218. // uiConfig = "_VenusDE";
  219. //}
  220. ////else if (configType == ConfigType.Venus)
  221. ////{
  222. //// uiConfig = "_Venus";
  223. ////}
  224. uiConfig = $"_{configType}";
  225. if (File.Exists($"Config/UIMenu{uiConfig}.json"))
  226. {
  227. configPath = $"Config/UIMenu{uiConfig}.json";
  228. }
  229. else if (File.Exists($"Config/Menu{uiConfig}.json"))
  230. {
  231. configPath = $"Config/Menu{uiConfig}.json";
  232. }
  233. else
  234. {
  235. WPFMessageBox.ShowError("未发现UI Config配置文件,退出UI");
  236. this.Close();
  237. }
  238. RoleDefines = SerializeHelper.Instance.ReadFromJsonFile<List<RoleDefine>>(configPath);
  239. string[] allModules = QueryDataClient.Instance.Service.GetConfig($"System.InstalledModules").ToString().Split(',');
  240. //TabControl tabControl = new TabControl();
  241. int role = (int)currentUser.Role;
  242. var menu = RoleDefines[role].Menus;
  243. List<string> modules = new List<string>();
  244. for (int i = 0; i < menu.Count; i++)
  245. {
  246. if (!modules.Contains(menu[i].FirstMenu))
  247. {
  248. modules.Add(menu[i].FirstMenu);
  249. centerTabViews.Add(new TabControl());
  250. }
  251. }
  252. int index = -1;
  253. string lastModule = "";
  254. for (int i = 0; i < menu.Count; i++)
  255. {
  256. if (menu[i].Permission == MenuPermission.None)
  257. {
  258. continue;
  259. }
  260. if (menu[i].FirstMenu == "PMA" || menu[i].FirstMenu == "PMB" || menu[i].FirstMenu == "PMC" || menu[i].FirstMenu == "PMD" || menu[i].FirstMenu == "TM")
  261. {
  262. if (!allModules.Contains(menu[i].FirstMenu))
  263. {
  264. continue;
  265. }
  266. }
  267. string className;
  268. if ((menu[i].FirstMenu == "PMA" || menu[i].FirstMenu == "PMB" || menu[i].FirstMenu == "PMC" || menu[i].FirstMenu == "PMD") && menu[i].SecondMenu == "Operation")
  269. {
  270. JetChamber jetChamber = (JetChamber)(Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig($"{menu[i].FirstMenu}.ChamberType")));
  271. className = $"Venus_MainPages.Views.Over{jetChamber.ToString()}View";
  272. }
  273. else
  274. {
  275. className = $"Venus_MainPages.Views.{menu[i].View}";
  276. }
  277. Type t = Type.GetType($"{className},Venus_MainPages");
  278. var obj = System.Activator.CreateInstance(t);
  279. if (menu[i].FirstMenu == "PMA" || menu[i].FirstMenu == "PMB" || menu[i].FirstMenu == "PMC" || menu[i].FirstMenu == "PMD")
  280. {
  281. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  282. methodInfo?.Invoke(obj, new object[] { menu[i].FirstMenu });
  283. }
  284. else if (menu[i].SecondMenu == "TM IO")
  285. {
  286. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  287. methodInfo?.Invoke(obj, new object[] { "TM" });
  288. }
  289. else if (menu[i].FirstMenu == "Operation" && menu[i].SecondMenu == "IO")
  290. {
  291. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  292. methodInfo?.Invoke(obj, new object[] { "TM" });
  293. }
  294. else if (menu[i].FirstMenu == "Operation" && menu[i].SecondMenu == "IO1")
  295. {
  296. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  297. methodInfo?.Invoke(obj, new object[] { "TM" });
  298. }
  299. if (lastModule != menu[i].FirstMenu)
  300. {
  301. AduRadioButtonIcon aduRadioButtonIcon = new AduRadioButtonIcon();
  302. index += 1;
  303. if (index == 0)
  304. {
  305. aduRadioButtonIcon.IsChecked = true;
  306. }
  307. //IconElement.SetPathData(aduRadioButtonIcon, (Geometry)aduRadioButtonIcon.FindResource($"Icon_{menu[i].se}"));
  308. aduRadioButtonIcon.Content = menu[i].FirstMenu;
  309. aduRadioButtonIcon.Checked += AduRadioButtonIcon_Click;
  310. aduRadioButtonIcon.SelectBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0076C4"));
  311. aduRadioButtonIcon.SelectColor = new SolidColorBrush((Colors.White));
  312. aduRadioButtonIcon.Width = 180;
  313. aduRadioButtonIcon.Tag = index;
  314. buttonList.Add(aduRadioButtonIcon);
  315. Bottom_Frame.Children.Add(aduRadioButtonIcon);
  316. }
  317. TabItem tabItem = new TabItem() { Header = menu[i].SecondMenu, Content = obj };
  318. if (menu[i].Permission == MenuPermission.ReadOnly)
  319. {
  320. (tabItem.Content as Control).IsEnabled = false;
  321. }
  322. centerTabViews[index].Items.Add(tabItem);
  323. (tabItem.Content as Control).IsVisibleChanged += TabItem_IsVisibleChanged;
  324. lastModule = menu[i].FirstMenu;
  325. }
  326. Main_Frame.Content = centerTabViews[0];
  327. ModuleManager.Initialize();
  328. currentRole = RoleDefines.Where(x => x.RoleName == currentUser.Role.ToString()).FirstOrDefault();
  329. if (currentRole.IsLocked == true)
  330. {
  331. m_logoutStopWatch = new Stopwatch();
  332. m_logoutStopWatch.Start();
  333. logoutTime = currentRole.LockTime;
  334. }
  335. timer.Tick += timer_Tick;
  336. timer.Interval = TimeSpan.FromSeconds(1);
  337. timer.Start();
  338. }
  339. private void TabItem_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  340. {
  341. var viewmodel = (sender as Control).DataContext;
  342. Type t = viewmodel.GetType();
  343. MethodInfo methodInfo = t.GetMethod("EnterExitView", new Type[] { typeof(bool) });
  344. methodInfo?.Invoke(viewmodel, new object[] { e.NewValue });
  345. }
  346. public void Anystationchange(string menuviewItem, WaferHistoryWafer queryFilter)
  347. {
  348. UserControl address = new KeplerProcessHistoryView();
  349. KeplerProcessHistoryViewModel vm = new KeplerProcessHistoryViewModel() { };
  350. vm.searchlot(queryFilter.WaferGuid);
  351. //vm.OnDataGridSelectionChanged(vm.Recipes[0]);
  352. address.DataContext = vm;
  353. int s = 0;
  354. for (int i = 0; i < centerTabViews.Count; i++)
  355. {
  356. var item = centerTabViews[i];
  357. foreach (TabItem v in item.Items)
  358. {
  359. if (v.Header.ToString() == "Process History")
  360. {
  361. s = i;
  362. }
  363. }
  364. }
  365. if (s != 0)
  366. {
  367. centerTabViews[s].Items[2] = new TabItem() { Header = menuviewItem, Content = address };
  368. centerTabViews[s].SelectedIndex = 2;
  369. Main_Frame.Content = centerTabViews[s];
  370. }
  371. }
  372. private void AduRadioButtonIcon_Click(object sender, RoutedEventArgs e)
  373. {
  374. var currentButton = sender as AduRadioButtonIcon;
  375. Main_Frame.Content = centerTabViews[Convert.ToInt32(currentButton.Tag)];
  376. }
  377. private void CustomWnd_Closed(object sender, EventArgs e)
  378. {
  379. System.Diagnostics.Process.GetCurrentProcess().Kill();
  380. }
  381. private void UIEvents_ChamberCreateDeleteWaferEvent(WaferOperation obj)
  382. {
  383. if (obj.IsCreate == true)
  384. {
  385. InvokeClient.Instance.Service.DoOperation("CreateWafer", obj.ModuleName, 0);
  386. }
  387. else
  388. {
  389. InvokeClient.Instance.Service.DoOperation("DeleteWafer", obj.ModuleName, 0);
  390. }
  391. }
  392. private void UIEvents_PMDoorRaiseChangedEvent(DoorPara obj)
  393. {
  394. //var value = SC.GetValue<int>($"{obj?.ModuleName}.ChamberType");
  395. //var value=(JetChamber)Enum.Parse(typeof(JetChamber), QueryDataClient.Instance.Service.GetConfig($"{obj?.ModuleName}.ChamberType").ToString());
  396. JetChamber jetChamber = (JetChamber)Enum.Parse(typeof(JetChamber), QueryDataClient.Instance.Service.GetConfig($"{obj?.ModuleName}.ChamberType").ToString());
  397. if (jetChamber == JetChamber.VenusDE)
  398. {
  399. InvokeClient.Instance.Service.DoOperation($"{obj?.ModuleName}.SetLinerDoor", obj.IsOpen == "Open" ? true : false);
  400. }
  401. else
  402. {
  403. InvokeClient.Instance.Service.DoOperation($"TM.SetMFSlitDoor", obj.ModuleName, obj.IsOpen == "Open" ? true : false);
  404. }
  405. //InvokeClient.Instance.Service.DoOperation($"{obj?.ModuleName}.SlitDoor.{obj?.IsOpen}");
  406. }
  407. private void UIEvents_LLTDoorRaiseChangedEvent(DoorPara obj)
  408. {
  409. InvokeClient.Instance.Service.DoOperation($"TM.SetMFSlitDoor", obj.ModuleName, obj.IsOpen == "Open" ? true : false);
  410. }
  411. private void UIEvents_LLEDoorRaiseChangedEvent(DoorPara obj)
  412. {
  413. InvokeClient.Instance.Service.DoOperation($"TM.SetEFEMSlitDoor", obj.ModuleName, obj.IsOpen == "Open" ? true : false);
  414. }
  415. private void UIEvents_InitRaiseChangedEvent(string obj)
  416. {
  417. if(obj.Contains("VCE"))
  418. InvokeClient.Instance.Service.DoOperation($"{obj}.HOME");
  419. else
  420. InvokeClient.Instance.Service.DoOperation($"{obj}.Home");
  421. }
  422. private void UIEvents_AbortRaiseChangedEvent(string obj)
  423. {
  424. InvokeClient.Instance.Service.DoOperation($"{obj}.Abort");
  425. }
  426. private void UIEvents_IncludeRaiseChangedEvent(IncludePara obj)
  427. {
  428. if (obj.IsInclude)
  429. {
  430. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Include}");
  431. }
  432. else
  433. {
  434. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Exclude}");
  435. }
  436. }
  437. private void UIEvents_OnlineRaiseChangedEvent(IncludePara obj)
  438. {
  439. if (obj.ModuleName == "EFEM")
  440. {
  441. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Online}", obj.IsInclude);
  442. }
  443. else if (obj.ModuleName == "System")
  444. {
  445. if (obj.IsInclude)
  446. {
  447. InvokeClient.Instance.Service.DoOperation("System.SetAutoMode");
  448. }
  449. else
  450. {
  451. InvokeClient.Instance.Service.DoOperation("System.SetManualMode");
  452. }
  453. }
  454. else
  455. {
  456. if (obj.IsInclude)
  457. {
  458. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Online}");
  459. }
  460. else
  461. {
  462. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Offline}");
  463. }
  464. }
  465. }
  466. #region 调用api功能部分
  467. void win_SourceInitialized(object sender, EventArgs e)
  468. {
  469. System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
  470. WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
  471. }
  472. private static System.IntPtr WindowProc(
  473. System.IntPtr hwnd,
  474. int msg,
  475. System.IntPtr wParam,
  476. System.IntPtr lParam,
  477. ref bool handled)
  478. {
  479. switch (msg)
  480. {
  481. case 0x0024:
  482. WmGetMinMaxInfo(hwnd, lParam);
  483. handled = true;
  484. break;
  485. }
  486. return (System.IntPtr)0;
  487. }
  488. private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
  489. {
  490. MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
  491. // Adjust the maximized size and position to fit the work area of the correct monitor
  492. int MONITOR_DEFAULTTONEAREST = 0x00000002;
  493. System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
  494. if (monitor != System.IntPtr.Zero)
  495. {
  496. MONITORINFO monitorInfo = new MONITORINFO();
  497. GetMonitorInfo(monitor, monitorInfo);
  498. RECT rcWorkArea = monitorInfo.rcWork;
  499. RECT rcMonitorArea = monitorInfo.rcMonitor;
  500. mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
  501. mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
  502. mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
  503. mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
  504. }
  505. Marshal.StructureToPtr(mmi, lParam, true);
  506. }
  507. /// <summary>
  508. /// POINT aka POINTAPI
  509. /// </summary>
  510. [StructLayout(LayoutKind.Sequential)]
  511. public struct POINT
  512. {
  513. /// <summary>
  514. /// x coordinate of point.
  515. /// </summary>
  516. public int x;
  517. /// <summary>
  518. /// y coordinate of point.
  519. /// </summary>
  520. public int y;
  521. /// <summary>
  522. /// Construct a point of coordinates (x,y).
  523. /// </summary>
  524. public POINT(int x, int y)
  525. {
  526. this.x = x;
  527. this.y = y;
  528. }
  529. }
  530. [StructLayout(LayoutKind.Sequential)]
  531. public struct MINMAXINFO
  532. {
  533. public POINT ptReserved;
  534. public POINT ptMaxSize;
  535. public POINT ptMaxPosition;
  536. public POINT ptMinTrackSize;
  537. public POINT ptMaxTrackSize;
  538. };
  539. /// <summary>
  540. /// </summary>
  541. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  542. public class MONITORINFO
  543. {
  544. /// <summary>
  545. /// </summary>
  546. public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
  547. /// <summary>
  548. /// </summary>
  549. public RECT rcMonitor = new RECT();
  550. /// <summary>
  551. /// </summary>
  552. public RECT rcWork = new RECT();
  553. /// <summary>
  554. /// </summary>
  555. public int dwFlags = 0;
  556. }
  557. /// <summary> Win32 </summary>
  558. [StructLayout(LayoutKind.Sequential, Pack = 0)]
  559. public struct RECT
  560. {
  561. /// <summary> Win32 </summary>
  562. public int left;
  563. /// <summary> Win32 </summary>
  564. public int top;
  565. /// <summary> Win32 </summary>
  566. public int right;
  567. /// <summary> Win32 </summary>
  568. public int bottom;
  569. /// <summary> Win32 </summary>
  570. public static readonly RECT Empty = new RECT();
  571. /// <summary> Win32 </summary>
  572. public int Width
  573. {
  574. get { return Math.Abs(right - left); } // Abs needed for BIDI OS
  575. }
  576. /// <summary> Win32 </summary>
  577. public int Height
  578. {
  579. get { return bottom - top; }
  580. }
  581. /// <summary> Win32 </summary>
  582. public RECT(int left, int top, int right, int bottom)
  583. {
  584. this.left = left;
  585. this.top = top;
  586. this.right = right;
  587. this.bottom = bottom;
  588. }
  589. /// <summary> Win32 </summary>
  590. public RECT(RECT rcSrc)
  591. {
  592. this.left = rcSrc.left;
  593. this.top = rcSrc.top;
  594. this.right = rcSrc.right;
  595. this.bottom = rcSrc.bottom;
  596. }
  597. /// <summary> Win32 </summary>
  598. public bool IsEmpty
  599. {
  600. get
  601. {
  602. // BUGBUG : On Bidi OS (hebrew arabic) left > right
  603. return left >= right || top >= bottom;
  604. }
  605. }
  606. /// <summary> Return a user friendly representation of this struct </summary>
  607. public override string ToString()
  608. {
  609. if (this == RECT.Empty) { return "RECT {Empty}"; }
  610. return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
  611. }
  612. /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
  613. public override bool Equals(object obj)
  614. {
  615. if (!(obj is Rect)) { return false; }
  616. return (this == (RECT)obj);
  617. }
  618. /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
  619. public override int GetHashCode()
  620. {
  621. return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
  622. }
  623. /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
  624. public static bool operator ==(RECT rect1, RECT rect2)
  625. {
  626. return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
  627. }
  628. /// <summary> Determine if 2 RECT are different(deep compare)</summary>
  629. public static bool operator !=(RECT rect1, RECT rect2)
  630. {
  631. return !(rect1 == rect2);
  632. }
  633. }
  634. [DllImport("user32")]
  635. internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
  636. /// <summary>
  637. ///
  638. /// </summary>
  639. [DllImport("User32")]
  640. internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
  641. #endregion
  642. #region 截屏
  643. private void SaveWindowContent(Window source, string fileName)
  644. {
  645. //FrameworkElement elem = source.Content as FrameworkElement;
  646. RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
  647. 960,
  648. 490,
  649. 48d,
  650. 48d,
  651. PixelFormats.Default);
  652. targetBitmap.Render(source);
  653. BmpBitmapEncoder encoder = new BmpBitmapEncoder();
  654. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  655. // save file to disk
  656. using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate))
  657. {
  658. encoder.Save(fs);
  659. }
  660. }
  661. #endregion
  662. private void Window_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
  663. {
  664. //this.buttonList[1].IsChecked = true;
  665. //await System.Threading.Tasks.Task.Delay(1);
  666. //SaveWindowContent(this, "test.jpg");
  667. //this.buttonList[0].IsChecked = true;
  668. }
  669. }
  670. }