MainWindow.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 (Exception ex)
  99. {
  100. }
  101. handled = true;
  102. return (IntPtr)(1);
  103. }
  104. if (msg == App.WakeId)
  105. {
  106. //将现有窗口激活 并前置
  107. try
  108. {
  109. if (WindowState == WindowState.Minimized)
  110. {
  111. WindowState = WindowState.Normal;
  112. }
  113. Activate();
  114. handled = true;
  115. }
  116. catch (Exception ex)
  117. {
  118. }
  119. return (IntPtr)(1);
  120. }
  121. return IntPtr.Zero;
  122. }
  123. /// <summary>
  124. /// 此类因为Dragablz控件的原因需要特殊处理
  125. /// </summary>
  126. /// <param name="eventAggregator"></param>
  127. public MainWindow(IEventAggregator eventAggregator, IContainerProvider container) :this()
  128. {
  129. aggregator = eventAggregator;
  130. eventAggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Opened });
  131. }
  132. private void BtnMiniSize_Click(object sender, RoutedEventArgs e)
  133. {
  134. System.Windows.SystemCommands.MinimizeWindow(this);
  135. }
  136. private void BtnClose_Click(object sender, RoutedEventArgs e)
  137. {
  138. System.Windows.SystemCommands.CloseWindow(this);
  139. }
  140. private void BtnReStore_Click(object sender, RoutedEventArgs e)
  141. {
  142. if (this.WindowState == WindowState.Maximized)
  143. {
  144. System.Windows.SystemCommands.RestoreWindow(this);
  145. }
  146. else
  147. {
  148. System.Windows.SystemCommands.MaximizeWindow(this);
  149. }
  150. }
  151. private async void Window_Activated(object sender, EventArgs e)
  152. {
  153. App.Current.MainWindow = this;
  154. App.mainWindowViewModel = this.DataContext as MainWindowViewModel;
  155. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Activated });
  156. //设为默认浏览器后,外部点击文档打开
  157. if (App.NeedOpenFilePathList.Count != 0)
  158. {
  159. for(int i=0;i<App.NeedOpenFilePathList.Count;i++)
  160. {
  161. (this.DataContext as MainWindowViewModel).AddTabItem(App.NeedOpenFilePathList[i]);
  162. await Task.Delay(50);
  163. }
  164. App.NeedOpenFilePathList.Clear();
  165. }
  166. }
  167. private void Window_Closed(object sender, EventArgs e)
  168. {
  169. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Closed });
  170. }
  171. public void LoadPdfViewer(string[] filePaths)
  172. {
  173. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  174. if (filePaths.Count() == 1)
  175. {
  176. if (App.OpenedFileList.Contains(filePaths[0]))
  177. {
  178. App.mainWindowViewModel.SelectItem(filePaths[0]);
  179. }
  180. else
  181. {
  182. content.OpenFile(filePaths[0]);
  183. }
  184. ToolMethod.SetFileThumbImg(filePaths[0]);
  185. }
  186. else
  187. {
  188. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  189. if (fileList.Count <= 0)
  190. return;
  191. content.OpenFile(filePaths[0]);
  192. for (int i = 1; i < fileList.Count(); i++)
  193. {
  194. if (!App.OpenedFileList.Contains(fileList[i]))
  195. {
  196. App.mainWindowViewModel.AddTabItem(fileList[i]);
  197. }
  198. ToolMethod.SetFileThumbImg(fileList[i]);
  199. }
  200. }
  201. Settings.Default.Save();
  202. }
  203. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  204. {
  205. UpdateTitleMargin();
  206. if (this.WindowState == WindowState.Maximized)
  207. {
  208. ico_max.Visibility = Visibility.Collapsed;
  209. }
  210. else
  211. {
  212. ico_max.Visibility = Visibility.Visible;
  213. }
  214. }
  215. /// <summary>
  216. /// 更新添加按钮的间隔位置
  217. /// </summary>
  218. private void UpdateTitleMargin()
  219. {
  220. var width = StkPnlRight.ActualWidth;
  221. BtnAdd.Margin = new Thickness(8, 13, width, 7);
  222. TabablzControl.UpdateLayout();
  223. }
  224. private void TxtRename_PreviewKeyDown(object sender, KeyEventArgs e)
  225. {
  226. if(e.Key == Key.Enter)
  227. {
  228. TabablzControl.Focus();
  229. }
  230. }
  231. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  232. {
  233. if (!(DataContext as MainWindowViewModel).closeAllTabItem())
  234. {
  235. e.Cancel = true;
  236. }
  237. }
  238. private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  239. {
  240. //通过转移焦点来触发文本框失去焦点事件
  241. var uiElement = FocusManager.GetFocusedElement(this);
  242. if (uiElement != null)
  243. {
  244. var textBox = uiElement as TextBox;
  245. //会影响页面编辑的下拉框选择 暂时注释
  246. //if (textBox != null)
  247. //{
  248. // GridRoot.Focusable = true;
  249. // GridRoot.Focus();
  250. //}
  251. }
  252. }
  253. private void Grid_Openhight_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  254. {
  255. UpdateTitleMargin();
  256. }
  257. }
  258. }