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. 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 KeplerProcessHistoryView();
  348. KeplerProcessHistoryViewModel vm = new KeplerProcessHistoryViewModel() { };
  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. if(obj.Contains("VCE"))
  417. InvokeClient.Instance.Service.DoOperation($"{obj}.HOME");
  418. else
  419. InvokeClient.Instance.Service.DoOperation($"{obj}.Home");
  420. }
  421. private void UIEvents_AbortRaiseChangedEvent(string obj)
  422. {
  423. InvokeClient.Instance.Service.DoOperation($"{obj}.Abort");
  424. }
  425. private void UIEvents_IncludeRaiseChangedEvent(IncludePara obj)
  426. {
  427. if (obj.IsInclude)
  428. {
  429. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Include}");
  430. }
  431. else
  432. {
  433. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Exclude}");
  434. }
  435. }
  436. private void UIEvents_OnlineRaiseChangedEvent(IncludePara obj)
  437. {
  438. if (obj.ModuleName == "EFEM")
  439. {
  440. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Online}", obj.IsInclude);
  441. }
  442. else if (obj.ModuleName == "System")
  443. {
  444. if (obj.IsInclude)
  445. {
  446. InvokeClient.Instance.Service.DoOperation("System.SetAutoMode");
  447. }
  448. else
  449. {
  450. InvokeClient.Instance.Service.DoOperation("System.SetManualMode");
  451. }
  452. }
  453. else
  454. {
  455. if (obj.IsInclude)
  456. {
  457. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Online}");
  458. }
  459. else
  460. {
  461. InvokeClient.Instance.Service.DoOperation($"{obj.ModuleName}.{RtOperation.Offline}");
  462. }
  463. }
  464. }
  465. #region 调用api功能部分
  466. void win_SourceInitialized(object sender, EventArgs e)
  467. {
  468. System.IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
  469. WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
  470. }
  471. private static System.IntPtr WindowProc(
  472. System.IntPtr hwnd,
  473. int msg,
  474. System.IntPtr wParam,
  475. System.IntPtr lParam,
  476. ref bool handled)
  477. {
  478. switch (msg)
  479. {
  480. case 0x0024:
  481. WmGetMinMaxInfo(hwnd, lParam);
  482. handled = true;
  483. break;
  484. }
  485. return (System.IntPtr)0;
  486. }
  487. private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
  488. {
  489. MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
  490. // Adjust the maximized size and position to fit the work area of the correct monitor
  491. int MONITOR_DEFAULTTONEAREST = 0x00000002;
  492. System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
  493. if (monitor != System.IntPtr.Zero)
  494. {
  495. MONITORINFO monitorInfo = new MONITORINFO();
  496. GetMonitorInfo(monitor, monitorInfo);
  497. RECT rcWorkArea = monitorInfo.rcWork;
  498. RECT rcMonitorArea = monitorInfo.rcMonitor;
  499. mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
  500. mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
  501. mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
  502. mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
  503. }
  504. Marshal.StructureToPtr(mmi, lParam, true);
  505. }
  506. /// <summary>
  507. /// POINT aka POINTAPI
  508. /// </summary>
  509. [StructLayout(LayoutKind.Sequential)]
  510. public struct POINT
  511. {
  512. /// <summary>
  513. /// x coordinate of point.
  514. /// </summary>
  515. public int x;
  516. /// <summary>
  517. /// y coordinate of point.
  518. /// </summary>
  519. public int y;
  520. /// <summary>
  521. /// Construct a point of coordinates (x,y).
  522. /// </summary>
  523. public POINT(int x, int y)
  524. {
  525. this.x = x;
  526. this.y = y;
  527. }
  528. }
  529. [StructLayout(LayoutKind.Sequential)]
  530. public struct MINMAXINFO
  531. {
  532. public POINT ptReserved;
  533. public POINT ptMaxSize;
  534. public POINT ptMaxPosition;
  535. public POINT ptMinTrackSize;
  536. public POINT ptMaxTrackSize;
  537. };
  538. /// <summary>
  539. /// </summary>
  540. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  541. public class MONITORINFO
  542. {
  543. /// <summary>
  544. /// </summary>
  545. public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
  546. /// <summary>
  547. /// </summary>
  548. public RECT rcMonitor = new RECT();
  549. /// <summary>
  550. /// </summary>
  551. public RECT rcWork = new RECT();
  552. /// <summary>
  553. /// </summary>
  554. public int dwFlags = 0;
  555. }
  556. /// <summary> Win32 </summary>
  557. [StructLayout(LayoutKind.Sequential, Pack = 0)]
  558. public struct RECT
  559. {
  560. /// <summary> Win32 </summary>
  561. public int left;
  562. /// <summary> Win32 </summary>
  563. public int top;
  564. /// <summary> Win32 </summary>
  565. public int right;
  566. /// <summary> Win32 </summary>
  567. public int bottom;
  568. /// <summary> Win32 </summary>
  569. public static readonly RECT Empty = new RECT();
  570. /// <summary> Win32 </summary>
  571. public int Width
  572. {
  573. get { return Math.Abs(right - left); } // Abs needed for BIDI OS
  574. }
  575. /// <summary> Win32 </summary>
  576. public int Height
  577. {
  578. get { return bottom - top; }
  579. }
  580. /// <summary> Win32 </summary>
  581. public RECT(int left, int top, int right, int bottom)
  582. {
  583. this.left = left;
  584. this.top = top;
  585. this.right = right;
  586. this.bottom = bottom;
  587. }
  588. /// <summary> Win32 </summary>
  589. public RECT(RECT rcSrc)
  590. {
  591. this.left = rcSrc.left;
  592. this.top = rcSrc.top;
  593. this.right = rcSrc.right;
  594. this.bottom = rcSrc.bottom;
  595. }
  596. /// <summary> Win32 </summary>
  597. public bool IsEmpty
  598. {
  599. get
  600. {
  601. // BUGBUG : On Bidi OS (hebrew arabic) left > right
  602. return left >= right || top >= bottom;
  603. }
  604. }
  605. /// <summary> Return a user friendly representation of this struct </summary>
  606. public override string ToString()
  607. {
  608. if (this == RECT.Empty) { return "RECT {Empty}"; }
  609. return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
  610. }
  611. /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
  612. public override bool Equals(object obj)
  613. {
  614. if (!(obj is Rect)) { return false; }
  615. return (this == (RECT)obj);
  616. }
  617. /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
  618. public override int GetHashCode()
  619. {
  620. return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
  621. }
  622. /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
  623. public static bool operator ==(RECT rect1, RECT rect2)
  624. {
  625. return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
  626. }
  627. /// <summary> Determine if 2 RECT are different(deep compare)</summary>
  628. public static bool operator !=(RECT rect1, RECT rect2)
  629. {
  630. return !(rect1 == rect2);
  631. }
  632. }
  633. [DllImport("user32")]
  634. internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
  635. /// <summary>
  636. ///
  637. /// </summary>
  638. [DllImport("User32")]
  639. internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
  640. #endregion
  641. #region 截屏
  642. private void SaveWindowContent(Window source, string fileName)
  643. {
  644. //FrameworkElement elem = source.Content as FrameworkElement;
  645. RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
  646. 960,
  647. 490,
  648. 48d,
  649. 48d,
  650. PixelFormats.Default);
  651. targetBitmap.Render(source);
  652. BmpBitmapEncoder encoder = new BmpBitmapEncoder();
  653. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  654. // save file to disk
  655. using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate))
  656. {
  657. encoder.Save(fs);
  658. }
  659. }
  660. #endregion
  661. private void Window_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
  662. {
  663. //this.buttonList[1].IsChecked = true;
  664. //await System.Threading.Tasks.Task.Delay(1);
  665. //SaveWindowContent(this, "test.jpg");
  666. //this.buttonList[0].IsChecked = true;
  667. }
  668. }
  669. }