MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. if (WindowState == WindowState.Minimized)
  110. {
  111. WindowState = WindowState.Normal;
  112. }
  113. Activate();
  114. ServiceHelper.GetUser();
  115. if(msg==App.WebopenexpiredId)
  116. {
  117. if (App.IsLogin == true)
  118. {
  119. App.mainWindowViewModel.OpenUser();
  120. App.WebOpencase = "";
  121. }
  122. else
  123. {
  124. App.mainWindowViewModel.OpenLogin();
  125. App.WebOpencase = "";
  126. }
  127. }
  128. handled = true;
  129. }
  130. catch
  131. {
  132. }
  133. return (IntPtr)(1);
  134. }
  135. return IntPtr.Zero;
  136. }
  137. /// <summary>
  138. /// 此类因为Dragablz控件的原因需要特殊处理
  139. /// </summary>
  140. /// <param name="eventAggregator"></param>
  141. public MainWindow(IEventAggregator eventAggregator, IContainerProvider container) :this()
  142. {
  143. aggregator = eventAggregator;
  144. eventAggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Opened });
  145. }
  146. private void BtnMiniSize_Click(object sender, RoutedEventArgs e)
  147. {
  148. System.Windows.SystemCommands.MinimizeWindow(this);
  149. }
  150. private void BtnClose_Click(object sender, RoutedEventArgs e)
  151. {
  152. System.Windows.SystemCommands.CloseWindow(this);
  153. }
  154. private void BtnReStore_Click(object sender, RoutedEventArgs e)
  155. {
  156. if (this.WindowState == WindowState.Maximized)
  157. {
  158. System.Windows.SystemCommands.RestoreWindow(this);
  159. }
  160. else
  161. {
  162. System.Windows.SystemCommands.MaximizeWindow(this);
  163. }
  164. }
  165. private async void Window_Activated(object sender, EventArgs e)
  166. {
  167. App.Current.MainWindow = this;
  168. App.mainWindowViewModel = this.DataContext as MainWindowViewModel;
  169. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Activated });
  170. //只需要第一次激活时获取一次信息
  171. if(App.IsFirstOpen)
  172. {
  173. //判断本地有没有token,没有显示登录,有再判断登录状态
  174. if (Settings.Default.AppProperties.LoginToken != "")
  175. {
  176. string code = ServiceHelper.GetUser();
  177. if (code == "330" || code == "304")
  178. {
  179. if (Settings.Default.UserDate.IsLoginoff == false)
  180. {
  181. App.mainWindowViewModel.OpenLoginoff();
  182. }
  183. }
  184. }
  185. }
  186. //控制首页UPgrade按钮的显示与否
  187. if (Settings.Default.UserDate.subscribestatus == 1)
  188. {
  189. App.mainWindowViewModel.UpgradeVis = Visibility.Collapsed;
  190. }
  191. else
  192. {
  193. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  194. }
  195. //设为默认浏览器后,外部点击文档打开
  196. if (App.NeedOpenFilePathList.Count != 0)
  197. {
  198. for(int i=0;i<App.NeedOpenFilePathList.Count;i++)
  199. {
  200. (this.DataContext as MainWindowViewModel).AddTabItem(App.NeedOpenFilePathList[i]);
  201. await Task.Delay(50);
  202. }
  203. App.NeedOpenFilePathList.Clear();
  204. }
  205. }
  206. private void Window_Closed(object sender, EventArgs e)
  207. {
  208. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Closed });
  209. }
  210. public void LoadPdfViewer(string[] filePaths)
  211. {
  212. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  213. if (filePaths.Count() == 1)
  214. {
  215. if (App.OpenedFileList.Contains(filePaths[0]))
  216. {
  217. App.mainWindowViewModel.SelectItem(filePaths[0]);
  218. }
  219. else
  220. {
  221. content.OpenFile(filePaths[0]);
  222. }
  223. ToolMethod.SetFileThumbImg(filePaths[0]);
  224. }
  225. else
  226. {
  227. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  228. if (fileList.Count <= 0)
  229. return;
  230. content.OpenFile(filePaths[0]);
  231. for (int i = 1; i < fileList.Count(); i++)
  232. {
  233. if (!App.OpenedFileList.Contains(fileList[i]))
  234. {
  235. App.mainWindowViewModel.AddTabItem(fileList[i]);
  236. }
  237. ToolMethod.SetFileThumbImg(fileList[i]);
  238. }
  239. }
  240. Settings.Default.Save();
  241. }
  242. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  243. {
  244. UpdateTitleMargin();
  245. if (this.WindowState == WindowState.Maximized)
  246. {
  247. ico_max.Visibility = Visibility.Collapsed;
  248. }
  249. else
  250. {
  251. ico_max.Visibility = Visibility.Visible;
  252. }
  253. }
  254. /// <summary>
  255. /// 更新添加按钮的间隔位置
  256. /// </summary>
  257. private void UpdateTitleMargin()
  258. {
  259. var width = StkPnlRight.ActualWidth;
  260. BtnAdd.Margin = new Thickness(8, 13, width, 7);
  261. TabablzControl.UpdateLayout();
  262. }
  263. private void TxtRename_PreviewKeyDown(object sender, KeyEventArgs e)
  264. {
  265. if(e.Key == Key.Enter)
  266. {
  267. TabablzControl.Focus();
  268. }
  269. }
  270. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  271. {
  272. if (!(DataContext as MainWindowViewModel).closeAllTabItem())
  273. {
  274. e.Cancel = true;
  275. }
  276. }
  277. private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  278. {
  279. //通过转移焦点来触发文本框失去焦点事件
  280. var uiElement = FocusManager.GetFocusedElement(this);
  281. if (uiElement != null)
  282. {
  283. var textBox = uiElement as TextBox;
  284. //会影响页面编辑的下拉框选择 暂时注释
  285. //if (textBox != null)
  286. //{
  287. // GridRoot.Focusable = true;
  288. // GridRoot.Focus();
  289. //}
  290. }
  291. }
  292. private void Grid_Openhight_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  293. {
  294. UpdateTitleMargin();
  295. }
  296. private async void Window_Loaded(object sender, RoutedEventArgs e)
  297. {
  298. if (App.Current.Windows.Count <= 1)
  299. {
  300. //软件打开,第一个窗体加载时,回传事件
  301. //放在主窗体的显示事件里,避免因网络请求问题导致启动缓慢
  302. await Task.Run(() =>
  303. {
  304. //上传启动信息至服务端
  305. ServiceHelper.CreateDeviceLog();
  306. });
  307. }
  308. }
  309. }
  310. }