MainWindow.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using PDF_Master.ViewModels;
  2. using Prism.Regions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using Prism.Events;
  18. using PDF_Master.EventAggregators;
  19. using PDF_Master.CustomControl;
  20. using Prism.Ioc;
  21. using PDF_Master.Helper;
  22. using System.IO;
  23. using ComPDFKit.PDFDocument;
  24. using PDFSettings;
  25. using PDF_Master.Properties;
  26. using System.Windows.Interop;
  27. using System.Runtime.InteropServices;
  28. using System.Security.Cryptography;
  29. using System.Diagnostics;
  30. namespace PDF_Master.Views
  31. {
  32. /// <summary>
  33. /// MainWindow.xaml 的交互逻辑
  34. /// </summary>
  35. public partial class MainWindow : Window
  36. {
  37. public IEventAggregator aggregator;
  38. public IRegionManager region;
  39. /// <summary>
  40. /// 窗口大小是否改变
  41. /// </summary>
  42. private bool sizeChange = false;
  43. public MainWindow()
  44. {
  45. InitializeComponent();
  46. this.SourceInitialized += MainWindow_SourceInitialized;
  47. System.Windows.Forms.Application.EnableVisualStyles();
  48. }
  49. private void MainWindow_SourceInitialized(object sender, EventArgs e)
  50. {
  51. try
  52. {
  53. //限制应用多开,显示已有窗体或者直接打开文件
  54. IntPtr hwnd = new WindowInteropHelper(this).Handle;
  55. HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
  56. }
  57. catch { }
  58. }
  59. private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  60. {
  61. if (msg == App.MsgId)
  62. {
  63. try
  64. {
  65. int msgLength = (int)(lParam);
  66. IntPtr mapHandle = Win32Helper.OpenFileMapping((int)Win32Helper.FileMapAccessType.Read, 0, "Global\\sharePathNotify");
  67. IntPtr readPtr = Win32Helper.MapViewOfFileEx(mapHandle, Win32Helper.FileMapAccessType.Read, 0, 0, UIntPtr.Zero, IntPtr.Zero);
  68. string filePath = Marshal.PtrToStringAuto(readPtr, msgLength);
  69. Win32Helper.UnmapViewOfFile(readPtr);
  70. Win32Helper.CloseHandle(mapHandle);
  71. MD5 currMd5 = MD5.Create();
  72. byte[] hashArray = currMd5.ComputeHash(Encoding.Default.GetBytes(filePath));
  73. StringBuilder hexBuilder = new StringBuilder();
  74. foreach (byte code in hashArray)
  75. {
  76. hexBuilder.AppendFormat("{0:X2}", code);
  77. }
  78. IntPtr waitPtr = Win32Helper.OpenEvent(0x0002, false, "Global\\shareWaitNotify" + hexBuilder.ToString());
  79. if (waitPtr != IntPtr.Zero)
  80. {
  81. Win32Helper.SetEvent(waitPtr);
  82. Win32Helper.CloseHandle(waitPtr);
  83. }
  84. if (File.Exists(filePath))
  85. {
  86. //打开传过来的文件路径
  87. if (App.OpenedFileList.Contains(filePath))
  88. {
  89. //如果已经打开时,则选中文档所在页签
  90. App.mainWindowViewModel.SelectItem(filePath);
  91. }
  92. else
  93. {
  94. (this.DataContext as MainWindowViewModel).AddTabItem(filePath);
  95. }
  96. if (WindowState == WindowState.Minimized)
  97. {
  98. WindowState = WindowState.Normal;
  99. }
  100. Activate();
  101. }
  102. }
  103. catch
  104. {
  105. }
  106. handled = true;
  107. return (IntPtr)(1);
  108. }
  109. if (msg == App.WakeId || msg == App.WebopenexpiredId)
  110. {
  111. //将现有窗口激活 并前置
  112. try
  113. {
  114. Activate();
  115. //最小化后激活
  116. if (this.WindowState == WindowState.Minimized)
  117. {
  118. System.Windows.SystemCommands.RestoreWindow(this);
  119. }
  120. //窗体前置
  121. Topmost = true;
  122. Topmost = false;
  123. ServiceHelper.GetUser();
  124. if (msg == App.WebopenexpiredId)
  125. {
  126. if (App.IsLogin == true && App.UserOpen == false)
  127. {
  128. App.mainWindowViewModel.OpenUser();
  129. App.WebOpencase = "";
  130. }
  131. else if (App.IsLogin == false && App.LoginOpen == false)
  132. {
  133. App.mainWindowViewModel.OpenLogin();
  134. App.WebOpencase = "";
  135. }
  136. }
  137. handled = true;
  138. }
  139. catch
  140. {
  141. }
  142. return (IntPtr)(1);
  143. }
  144. return IntPtr.Zero;
  145. }
  146. /// <summary>
  147. /// 此类因为Dragablz控件的原因需要特殊处理
  148. /// </summary>
  149. /// <param name="eventAggregator"></param>
  150. public MainWindow(IEventAggregator eventAggregator, IContainerProvider container) : this()
  151. {
  152. aggregator = eventAggregator;
  153. eventAggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Opened });
  154. }
  155. private void BtnMiniSize_Click(object sender, RoutedEventArgs e)
  156. {
  157. System.Windows.SystemCommands.MinimizeWindow(this);
  158. }
  159. private void BtnClose_Click(object sender, RoutedEventArgs e)
  160. {
  161. System.Windows.SystemCommands.CloseWindow(this);
  162. }
  163. private void BtnReStore_Click(object sender, RoutedEventArgs e)
  164. {
  165. if (this.WindowState == WindowState.Maximized)
  166. {
  167. System.Windows.SystemCommands.RestoreWindow(this);
  168. }
  169. else
  170. {
  171. System.Windows.SystemCommands.MaximizeWindow(this);
  172. }
  173. }
  174. private async void Window_Activated(object sender, EventArgs e)
  175. {
  176. App.Current.MainWindow = this;
  177. App.mainWindowViewModel = this.DataContext as MainWindowViewModel;
  178. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Activated });
  179. //只需要第一次激活时获取一次信息
  180. if (App.IsFirstOpen)
  181. {
  182. //判断本地有没有token,没有显示登录,有再判断登录状态
  183. if (Settings.Default.AppProperties.LoginToken != "")
  184. {
  185. string code = ServiceHelper.GetUser();
  186. if (code == "330" || code == "304")
  187. {
  188. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  189. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  190. App.mainWindowViewModel.RegisterVis = Visibility.Collapsed;
  191. App.mainWindowViewModel.OphVis = Visibility.Visible;
  192. if (Settings.Default.UserDate.IsLoginoff == false)
  193. {
  194. App.mainWindowViewModel.OpenLoginoff();
  195. Settings.Default.UserDate.IsLoginoff = true;
  196. Settings.Default.Save();
  197. }
  198. }
  199. else
  200. {
  201. App.IsLogin = true;
  202. App.mainWindowViewModel.Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
  203. App.mainWindowViewModel.UserVis = Visibility.Visible;
  204. }
  205. }
  206. else if (Settings.Default.UserDate.Email != "")
  207. {
  208. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  209. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  210. App.mainWindowViewModel.RegisterVis = Visibility.Collapsed;
  211. App.mainWindowViewModel.OphVis = Visibility.Visible;
  212. }
  213. else
  214. {
  215. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  216. App.mainWindowViewModel.RegisterVis = Visibility.Visible;
  217. App.mainWindowViewModel.LoginVis = Visibility.Collapsed;
  218. App.mainWindowViewModel.OphVis = Visibility.Visible;
  219. }
  220. App.IsFirstOpen = false;
  221. }
  222. //控制首页UPgrade按钮的显示与否
  223. if (Settings.Default.UserDate.subscribestatus == 1)
  224. {
  225. App.mainWindowViewModel.UpgradeVis = Visibility.Collapsed;
  226. }
  227. else
  228. {
  229. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  230. }
  231. //设为默认浏览器后,外部点击文档打开
  232. if (App.NeedOpenFilePathList.Count != 0)
  233. {
  234. for (int i = 0; i < App.NeedOpenFilePathList.Count; i++)
  235. {
  236. (this.DataContext as MainWindowViewModel).AddTabItem(App.NeedOpenFilePathList[i]);
  237. await Task.Delay(50);
  238. }
  239. App.NeedOpenFilePathList.Clear();
  240. }
  241. }
  242. private void Window_Closed(object sender, EventArgs e)
  243. {
  244. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Closed });
  245. }
  246. public void LoadPdfViewer(string[] filePaths)
  247. {
  248. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  249. if (filePaths.Count() == 1)
  250. {
  251. if (App.OpenedFileList.Contains(filePaths[0]))
  252. {
  253. App.mainWindowViewModel.SelectItem(filePaths[0]);
  254. }
  255. else
  256. {
  257. content.OpenFile(filePaths[0]);
  258. }
  259. ToolMethod.SetFileThumbImg(filePaths[0]);
  260. }
  261. else
  262. {
  263. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  264. if (fileList.Count <= 0)
  265. return;
  266. content.OpenFile(filePaths[0]);
  267. for (int i = 1; i < fileList.Count(); i++)
  268. {
  269. if (!App.OpenedFileList.Contains(fileList[i]))
  270. {
  271. App.mainWindowViewModel.AddTabItem(fileList[i]);
  272. }
  273. ToolMethod.SetFileThumbImg(fileList[i]);
  274. }
  275. }
  276. Settings.Default.Save();
  277. }
  278. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  279. {
  280. if (this.WindowState == WindowState.Maximized)
  281. {
  282. ico_max.Visibility = Visibility.Collapsed;
  283. }
  284. else
  285. {
  286. ico_max.Visibility = Visibility.Visible;
  287. }
  288. UpdateTitleMargin();
  289. if (this.ActualWidth != 1280)
  290. {
  291. sizeChange = true;
  292. }
  293. }
  294. /// <summary>
  295. /// 更新添加按钮的间隔位置
  296. /// </summary>
  297. private void UpdateTitleMargin()
  298. {
  299. var width = StkPnlRight.ActualWidth;
  300. if (BorderUpgrade.Visibility == Visibility.Collapsed)
  301. {
  302. BtnAdd.Margin = new Thickness(8, 13, width, 7);
  303. }
  304. else
  305. {
  306. if (this.WindowState == WindowState.Maximized)
  307. {
  308. BtnAdd.Margin = new Thickness(8, 13, width, 7);
  309. }
  310. else
  311. {
  312. if (sizeChange)
  313. {
  314. BtnAdd.Margin = new Thickness(8, 13, width, 7);
  315. }
  316. else
  317. {
  318. sizeChange = false;
  319. BtnAdd.Margin = new Thickness(8, 13, width + 50, 7);
  320. }
  321. }
  322. }
  323. TabablzControl.UpdateLayout();
  324. }
  325. private void TxtRename_PreviewKeyDown(object sender, KeyEventArgs e)
  326. {
  327. if (e.Key == Key.Enter)
  328. {
  329. TabablzControl.Focus();
  330. }
  331. }
  332. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  333. {
  334. if (!(DataContext as MainWindowViewModel).closeAllTabItem())
  335. {
  336. e.Cancel = true;
  337. }
  338. }
  339. private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  340. {
  341. //通过转移焦点来触发文本框失去焦点事件
  342. var uiElement = FocusManager.GetFocusedElement(this);
  343. if (uiElement != null)
  344. {
  345. var textBox = uiElement as TextBox;
  346. //会影响页面编辑的下拉框选择 暂时注释
  347. //if (textBox != null)
  348. //{
  349. // GridRoot.Focusable = true;
  350. // GridRoot.Focus();
  351. //}
  352. }
  353. }
  354. private void Grid_Openhight_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  355. {
  356. UpdateTitleMargin();
  357. }
  358. private async void Window_Loaded(object sender, RoutedEventArgs e)
  359. {
  360. if (App.Current.Windows.Count <= 1)
  361. {
  362. //软件打开,第一个窗体加载时,回传事件
  363. //放在主窗体的显示事件里,避免因网络请求问题导致启动缓慢
  364. await Task.Run(() =>
  365. {
  366. //上传启动信息至服务端
  367. ServiceHelper.CreateDeviceLog();
  368. });
  369. }
  370. }
  371. }
  372. }