ShellView.xaml.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. if (File.Exists($"Config/UIMenu{uiConfig}.json"))
  225. {
  226. configPath = $"Config/UIMenu{uiConfig}.json";
  227. }
  228. else if (File.Exists($"Config/Menu{uiConfig}.json"))
  229. {
  230. configPath = $"Config/Menu{uiConfig}.json";
  231. }
  232. else
  233. {
  234. WPFMessageBox.ShowError("未发现UI Config配置文件,退出UI");
  235. this.Close();
  236. }
  237. RoleDefines = SerializeHelper.Instance.ReadFromJsonFile<List<RoleDefine>>(configPath);
  238. string[] allModules = QueryDataClient.Instance.Service.GetConfig($"System.InstalledModules").ToString().Split(',');
  239. //TabControl tabControl = new TabControl();
  240. int role = (int)currentUser.Role;
  241. var menu = RoleDefines[role].Menus;
  242. List<string> modules = new List<string>();
  243. for (int i = 0; i < menu.Count; i++)
  244. {
  245. if (!modules.Contains(menu[i].FirstMenu))
  246. {
  247. modules.Add(menu[i].FirstMenu);
  248. centerTabViews.Add(new TabControl());
  249. }
  250. }
  251. int index = -1;
  252. string lastModule = "";
  253. for (int i = 0; i < menu.Count; i++)
  254. {
  255. if (menu[i].Permission == MenuPermission.None)
  256. {
  257. continue;
  258. }
  259. if (menu[i].FirstMenu == "PMA" || menu[i].FirstMenu == "PMB" || menu[i].FirstMenu == "PMC" || menu[i].FirstMenu == "PMD" || menu[i].FirstMenu == "TM")
  260. {
  261. if (!allModules.Contains(menu[i].FirstMenu))
  262. {
  263. continue;
  264. }
  265. }
  266. string className;
  267. if ((menu[i].FirstMenu == "PMA" || menu[i].FirstMenu == "PMB" || menu[i].FirstMenu == "PMC" || menu[i].FirstMenu == "PMD") && menu[i].SecondMenu == "Operation")
  268. {
  269. JetChamber jetChamber = (JetChamber)(Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig($"{menu[i].FirstMenu}.ChamberType")));
  270. className = $"Venus_MainPages.Views.Over{jetChamber.ToString()}View";
  271. }
  272. else
  273. {
  274. className = $"Venus_MainPages.Views.{menu[i].View}";
  275. }
  276. Type t = Type.GetType($"{className},Venus_MainPages");
  277. var obj = System.Activator.CreateInstance(t);
  278. if (menu[i].FirstMenu == "PMA" || menu[i].FirstMenu == "PMB" || menu[i].FirstMenu == "PMC" || menu[i].FirstMenu == "PMD")
  279. {
  280. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  281. methodInfo?.Invoke(obj, new object[] { menu[i].FirstMenu });
  282. }
  283. else if (menu[i].SecondMenu == "TM IO")
  284. {
  285. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  286. methodInfo?.Invoke(obj, new object[] { "TM" });
  287. }
  288. else if (menu[i].FirstMenu == "Operation" && menu[i].SecondMenu == "IO")
  289. {
  290. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  291. methodInfo?.Invoke(obj, new object[] { "TM" });
  292. }
  293. else if (menu[i].FirstMenu == "Operation" && menu[i].SecondMenu == "IO1")
  294. {
  295. MethodInfo methodInfo = t.GetMethod("Init", new Type[] { typeof(string) });
  296. methodInfo?.Invoke(obj, new object[] { "TM" });
  297. }
  298. if (lastModule != menu[i].FirstMenu)
  299. {
  300. AduRadioButtonIcon aduRadioButtonIcon = new AduRadioButtonIcon();
  301. index += 1;
  302. if (index == 0)
  303. {
  304. aduRadioButtonIcon.IsChecked = true;
  305. }
  306. //IconElement.SetPathData(aduRadioButtonIcon, (Geometry)aduRadioButtonIcon.FindResource($"Icon_{menu[i].se}"));
  307. aduRadioButtonIcon.Content = menu[i].FirstMenu;
  308. aduRadioButtonIcon.Checked += AduRadioButtonIcon_Click;
  309. aduRadioButtonIcon.SelectBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0076C4"));
  310. aduRadioButtonIcon.SelectColor = new SolidColorBrush((Colors.White));
  311. aduRadioButtonIcon.Width = 180;
  312. aduRadioButtonIcon.Tag = index;
  313. buttonList.Add(aduRadioButtonIcon);
  314. Bottom_Frame.Children.Add(aduRadioButtonIcon);
  315. }
  316. TabItem tabItem = new TabItem() { Header = menu[i].SecondMenu, Content = obj };
  317. if (menu[i].Permission == MenuPermission.ReadOnly)
  318. {
  319. (tabItem.Content as Control).IsEnabled = false;
  320. }
  321. centerTabViews[index].Items.Add(tabItem);
  322. (tabItem.Content as Control).IsVisibleChanged += TabItem_IsVisibleChanged;
  323. lastModule = menu[i].FirstMenu;
  324. }
  325. Main_Frame.Content = centerTabViews[0];
  326. ModuleManager.Initialize();
  327. currentRole = RoleDefines.Where(x => x.RoleName == currentUser.Role.ToString()).FirstOrDefault();
  328. if (currentRole.IsLocked == true)
  329. {
  330. m_logoutStopWatch = new Stopwatch();
  331. m_logoutStopWatch.Start();
  332. logoutTime = currentRole.LockTime;
  333. }
  334. timer.Tick += timer_Tick;
  335. timer.Interval = TimeSpan.FromSeconds(1);
  336. timer.Start();
  337. }
  338. private void TabItem_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  339. {
  340. var viewmodel = (sender as Control).DataContext;
  341. Type t = viewmodel.GetType();
  342. MethodInfo methodInfo = t.GetMethod("EnterExitView", new Type[] { typeof(bool) });
  343. methodInfo?.Invoke(viewmodel, new object[] { e.NewValue });
  344. }
  345. public void Anystationchange(string menuviewItem, WaferHistoryWafer queryFilter)
  346. {
  347. UserControl address = new ProcessHistoryView();
  348. ProcessHistoryViewModel vm = new ProcessHistoryViewModel() { };
  349. vm.searchlot(queryFilter.WaferGuid);
  350. //vm.OnDataGridSelectionChanged(vm.Recipes[0]);
  351. address.DataContext = vm;
  352. int s = 0;
  353. for (int i = 0; i < centerTabViews.Count; i++)
  354. {
  355. var item = centerTabViews[i];
  356. foreach (TabItem v in item.Items)
  357. {
  358. if (v.Header.ToString() == "Process History")
  359. {
  360. s = i;
  361. }
  362. }
  363. }
  364. if (s != 0)
  365. {
  366. centerTabViews[s].Items[2] = new TabItem() { Header = menuviewItem, Content = address };
  367. centerTabViews[s].SelectedIndex = 2;
  368. Main_Frame.Content = centerTabViews[s];
  369. }
  370. }
  371. private void AduRadioButtonIcon_Click(object sender, RoutedEventArgs e)
  372. {
  373. var currentButton = sender as AduRadioButtonIcon;
  374. Main_Frame.Content = centerTabViews[Convert.ToInt32(currentButton.Tag)];
  375. }
  376. private void CustomWnd_Closed(object sender, EventArgs e)
  377. {
  378. System.Diagnostics.Process.GetCurrentProcess().Kill();
  379. }
  380. private void UIEvents_ChamberCreateDeleteWaferEvent(WaferOperation obj)
  381. {
  382. if (obj.IsCreate == true)
  383. {
  384. InvokeClient.Instance.Service.DoOperation("CreateWafer", obj.ModuleName, 0);
  385. }
  386. else
  387. {
  388. InvokeClient.Instance.Service.DoOperation("DeleteWafer", obj.ModuleName, 0);
  389. }
  390. }
  391. private void UIEvents_PMDoorRaiseChangedEvent(DoorPara obj)
  392. {
  393. //var value = SC.GetValue<int>($"{obj?.ModuleName}.ChamberType");
  394. //var value=(JetChamber)Enum.Parse(typeof(JetChamber), QueryDataClient.Instance.Service.GetConfig($"{obj?.ModuleName}.ChamberType").ToString());
  395. JetChamber jetChamber = (JetChamber)Enum.Parse(typeof(JetChamber), QueryDataClient.Instance.Service.GetConfig($"{obj?.ModuleName}.ChamberType").ToString());
  396. if (jetChamber == JetChamber.VenusDE)
  397. {
  398. InvokeClient.Instance.Service.DoOperation($"{obj?.ModuleName}.SetLinerDoor", obj.IsOpen == "Open" ? true : false);
  399. }
  400. else
  401. {
  402. InvokeClient.Instance.Service.DoOperation($"TM.SetMFSlitDoor", obj.ModuleName, obj.IsOpen == "Open" ? true : false);
  403. }
  404. //InvokeClient.Instance.Service.DoOperation($"{obj?.ModuleName}.SlitDoor.{obj?.IsOpen}");
  405. }
  406. private void UIEvents_LLTDoorRaiseChangedEvent(DoorPara obj)
  407. {
  408. InvokeClient.Instance.Service.DoOperation($"TM.SetMFSlitDoor", obj.ModuleName, obj.IsOpen == "Open" ? true : false);
  409. }
  410. private void UIEvents_LLEDoorRaiseChangedEvent(DoorPara obj)
  411. {
  412. InvokeClient.Instance.Service.DoOperation($"TM.SetEFEMSlitDoor", obj.ModuleName, obj.IsOpen == "Open" ? true : false);
  413. }
  414. private void UIEvents_InitRaiseChangedEvent(string obj)
  415. {
  416. InvokeClient.Instance.Service.DoOperation($"{obj}.Home");
  417. }
  418. private void UIEvents_AbortRaiseChangedEvent(string obj)
  419. {
  420. InvokeClient.Instance.Service.DoOperation($"{obj}.Abort");
  421. }
  422. private void UIEvents_IncludeRaiseChangedEvent(IncludePara obj)
  423. {
  424. if (obj.IsInclude)
  425. {
  426. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Include}");
  427. }
  428. else
  429. {
  430. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Exclude}");
  431. }
  432. }
  433. private void UIEvents_OnlineRaiseChangedEvent(IncludePara obj)
  434. {
  435. if (obj.ModuleName == "EFEM")
  436. {
  437. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Online}", obj.IsInclude);
  438. }
  439. else if (obj.ModuleName == "System")
  440. {
  441. if (obj.IsInclude)
  442. {
  443. InvokeClient.Instance.Service.DoOperation("System.SetAutoMode");
  444. }
  445. else
  446. {
  447. InvokeClient.Instance.Service.DoOperation("System.SetManualMode");
  448. }
  449. }
  450. else
  451. {
  452. if (obj.IsInclude)
  453. {
  454. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Online}");
  455. }
  456. else
  457. {
  458. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Offline}");
  459. }
  460. }
  461. }
  462. #region 调用api功能部分
  463. void win_SourceInitialized(object sender, EventArgs e)
  464. {
  465. System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
  466. WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
  467. }
  468. private static System.IntPtr WindowProc(
  469. System.IntPtr hwnd,
  470. int msg,
  471. System.IntPtr wParam,
  472. System.IntPtr lParam,
  473. ref bool handled)
  474. {
  475. switch (msg)
  476. {
  477. case 0x0024:
  478. WmGetMinMaxInfo(hwnd, lParam);
  479. handled = true;
  480. break;
  481. }
  482. return (System.IntPtr)0;
  483. }
  484. private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
  485. {
  486. MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
  487. // Adjust the maximized size and position to fit the work area of the correct monitor
  488. int MONITOR_DEFAULTTONEAREST = 0x00000002;
  489. System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
  490. if (monitor != System.IntPtr.Zero)
  491. {
  492. MONITORINFO monitorInfo = new MONITORINFO();
  493. GetMonitorInfo(monitor, monitorInfo);
  494. RECT rcWorkArea = monitorInfo.rcWork;
  495. RECT rcMonitorArea = monitorInfo.rcMonitor;
  496. mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
  497. mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
  498. mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
  499. mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
  500. }
  501. Marshal.StructureToPtr(mmi, lParam, true);
  502. }
  503. /// <summary>
  504. /// POINT aka POINTAPI
  505. /// </summary>
  506. [StructLayout(LayoutKind.Sequential)]
  507. public struct POINT
  508. {
  509. /// <summary>
  510. /// x coordinate of point.
  511. /// </summary>
  512. public int x;
  513. /// <summary>
  514. /// y coordinate of point.
  515. /// </summary>
  516. public int y;
  517. /// <summary>
  518. /// Construct a point of coordinates (x,y).
  519. /// </summary>
  520. public POINT(int x, int y)
  521. {
  522. this.x = x;
  523. this.y = y;
  524. }
  525. }
  526. [StructLayout(LayoutKind.Sequential)]
  527. public struct MINMAXINFO
  528. {
  529. public POINT ptReserved;
  530. public POINT ptMaxSize;
  531. public POINT ptMaxPosition;
  532. public POINT ptMinTrackSize;
  533. public POINT ptMaxTrackSize;
  534. };
  535. /// <summary>
  536. /// </summary>
  537. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  538. public class MONITORINFO
  539. {
  540. /// <summary>
  541. /// </summary>
  542. public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
  543. /// <summary>
  544. /// </summary>
  545. public RECT rcMonitor = new RECT();
  546. /// <summary>
  547. /// </summary>
  548. public RECT rcWork = new RECT();
  549. /// <summary>
  550. /// </summary>
  551. public int dwFlags = 0;
  552. }
  553. /// <summary> Win32 </summary>
  554. [StructLayout(LayoutKind.Sequential, Pack = 0)]
  555. public struct RECT
  556. {
  557. /// <summary> Win32 </summary>
  558. public int left;
  559. /// <summary> Win32 </summary>
  560. public int top;
  561. /// <summary> Win32 </summary>
  562. public int right;
  563. /// <summary> Win32 </summary>
  564. public int bottom;
  565. /// <summary> Win32 </summary>
  566. public static readonly RECT Empty = new RECT();
  567. /// <summary> Win32 </summary>
  568. public int Width
  569. {
  570. get { return Math.Abs(right - left); } // Abs needed for BIDI OS
  571. }
  572. /// <summary> Win32 </summary>
  573. public int Height
  574. {
  575. get { return bottom - top; }
  576. }
  577. /// <summary> Win32 </summary>
  578. public RECT(int left, int top, int right, int bottom)
  579. {
  580. this.left = left;
  581. this.top = top;
  582. this.right = right;
  583. this.bottom = bottom;
  584. }
  585. /// <summary> Win32 </summary>
  586. public RECT(RECT rcSrc)
  587. {
  588. this.left = rcSrc.left;
  589. this.top = rcSrc.top;
  590. this.right = rcSrc.right;
  591. this.bottom = rcSrc.bottom;
  592. }
  593. /// <summary> Win32 </summary>
  594. public bool IsEmpty
  595. {
  596. get
  597. {
  598. // BUGBUG : On Bidi OS (hebrew arabic) left > right
  599. return left >= right || top >= bottom;
  600. }
  601. }
  602. /// <summary> Return a user friendly representation of this struct </summary>
  603. public override string ToString()
  604. {
  605. if (this == RECT.Empty) { return "RECT {Empty}"; }
  606. return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
  607. }
  608. /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
  609. public override bool Equals(object obj)
  610. {
  611. if (!(obj is Rect)) { return false; }
  612. return (this == (RECT)obj);
  613. }
  614. /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
  615. public override int GetHashCode()
  616. {
  617. return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
  618. }
  619. /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
  620. public static bool operator ==(RECT rect1, RECT rect2)
  621. {
  622. return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
  623. }
  624. /// <summary> Determine if 2 RECT are different(deep compare)</summary>
  625. public static bool operator !=(RECT rect1, RECT rect2)
  626. {
  627. return !(rect1 == rect2);
  628. }
  629. }
  630. [DllImport("user32")]
  631. internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
  632. /// <summary>
  633. ///
  634. /// </summary>
  635. [DllImport("User32")]
  636. internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
  637. #endregion
  638. #region 截屏
  639. private void SaveWindowContent(Window source, string fileName)
  640. {
  641. //FrameworkElement elem = source.Content as FrameworkElement;
  642. RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
  643. 960,
  644. 490,
  645. 48d,
  646. 48d,
  647. PixelFormats.Default);
  648. targetBitmap.Render(source);
  649. BmpBitmapEncoder encoder = new BmpBitmapEncoder();
  650. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  651. // save file to disk
  652. using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate))
  653. {
  654. encoder.Save(fs);
  655. }
  656. }
  657. #endregion
  658. private void Window_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
  659. {
  660. //this.buttonList[1].IsChecked = true;
  661. //await System.Threading.Tasks.Task.Delay(1);
  662. //SaveWindowContent(this, "test.jpg");
  663. //this.buttonList[0].IsChecked = true;
  664. }
  665. }
  666. }