MainWindow.xaml.cs 14 KB

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