MainWindow.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.Settings;
  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. }
  43. private void MainWindow_SourceInitialized(object sender, EventArgs e)
  44. {
  45. try
  46. {
  47. //限制应用多开,显示已有窗体或者直接打开文件
  48. IntPtr hwnd = new WindowInteropHelper(this).Handle;
  49. HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
  50. }
  51. catch { }
  52. }
  53. private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  54. {
  55. if (msg == App.MsgId)
  56. {
  57. try
  58. {
  59. int msgLength = (int)(lParam);
  60. IntPtr mapHandle = Win32Helper.OpenFileMapping((int)Win32Helper.FileMapAccessType.Read, 0, "Global\\sharePathNotify");
  61. IntPtr readPtr = Win32Helper.MapViewOfFileEx(mapHandle, Win32Helper.FileMapAccessType.Read, 0, 0, UIntPtr.Zero, IntPtr.Zero);
  62. string filePath = Marshal.PtrToStringAuto(readPtr, msgLength);
  63. Win32Helper.UnmapViewOfFile(readPtr);
  64. Win32Helper.CloseHandle(mapHandle);
  65. MD5 currMd5 = MD5.Create();
  66. byte[] hashArray = currMd5.ComputeHash(Encoding.Default.GetBytes(filePath));
  67. StringBuilder hexBuilder = new StringBuilder();
  68. foreach (byte code in hashArray)
  69. {
  70. hexBuilder.AppendFormat("{0:X2}", code);
  71. }
  72. IntPtr waitPtr = Win32Helper.OpenEvent(0x0002, false, "Global\\shareWaitNotify" + hexBuilder.ToString());
  73. if (waitPtr != IntPtr.Zero)
  74. {
  75. Win32Helper.SetEvent(waitPtr);
  76. Win32Helper.CloseHandle(waitPtr);
  77. }
  78. if (File.Exists(filePath))
  79. {
  80. //打开传过来的文件路径
  81. if (App.OpenedFileList.Contains(App.GuidPDFPath))
  82. {
  83. //如果已经打开时,则选中文档所在页签
  84. App.mainWindowViewModel.SelectItem(App.GuidPDFPath);
  85. }
  86. else
  87. {
  88. (this.DataContext as MainWindowViewModel).AddTabItem(filePath);
  89. }
  90. if (WindowState == WindowState.Minimized)
  91. {
  92. WindowState = WindowState.Normal;
  93. }
  94. Activate();
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. }
  100. handled = true;
  101. return (IntPtr)(1);
  102. }
  103. if (msg == App.WakeId)
  104. {
  105. //将现有窗口激活 并前置
  106. try
  107. {
  108. if (WindowState == WindowState.Minimized)
  109. {
  110. WindowState = WindowState.Normal;
  111. }
  112. Activate();
  113. handled = true;
  114. }
  115. catch (Exception ex)
  116. {
  117. }
  118. return (IntPtr)(1);
  119. }
  120. return IntPtr.Zero;
  121. }
  122. /// <summary>
  123. /// 此类因为Dragablz控件的原因需要特殊处理
  124. /// </summary>
  125. /// <param name="eventAggregator"></param>
  126. public MainWindow(IEventAggregator eventAggregator, IContainerProvider container) :this()
  127. {
  128. aggregator = eventAggregator;
  129. eventAggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Opened });
  130. }
  131. private void BtnMiniSize_Click(object sender, RoutedEventArgs e)
  132. {
  133. System.Windows.SystemCommands.MinimizeWindow(this);
  134. }
  135. private void BtnClose_Click(object sender, RoutedEventArgs e)
  136. {
  137. System.Windows.SystemCommands.CloseWindow(this);
  138. }
  139. private void BtnReStore_Click(object sender, RoutedEventArgs e)
  140. {
  141. if (this.WindowState == WindowState.Maximized)
  142. {
  143. System.Windows.SystemCommands.RestoreWindow(this);
  144. }
  145. else
  146. {
  147. System.Windows.SystemCommands.MaximizeWindow(this);
  148. }
  149. }
  150. private void Window_Activated(object sender, EventArgs e)
  151. {
  152. App.Current.MainWindow = this;
  153. App.mainWindowViewModel = this.DataContext as MainWindowViewModel;
  154. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Activated });
  155. }
  156. private void Window_Closed(object sender, EventArgs e)
  157. {
  158. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Closed });
  159. }
  160. public void LoadPdfViewer(string[] filePaths)
  161. {
  162. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  163. if (filePaths.Count() == 1)
  164. {
  165. if (App.OpenedFileList.Contains(filePaths[0]))
  166. {
  167. App.mainWindowViewModel.SelectItem(filePaths[0]);
  168. }
  169. else
  170. {
  171. content.OpenFile(filePaths[0]);
  172. }
  173. ToolMethod.SetFileThumbImg(filePaths[0]);
  174. }
  175. else
  176. {
  177. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  178. if (fileList.Count <= 0)
  179. return;
  180. content.OpenFile(filePaths[0]);
  181. for (int i = 1; i < fileList.Count(); i++)
  182. {
  183. if (!App.OpenedFileList.Contains(fileList[i]))
  184. {
  185. App.mainWindowViewModel.AddTabItem(fileList[i]);
  186. }
  187. ToolMethod.SetFileThumbImg(fileList[i]);
  188. }
  189. }
  190. Settings.Default.Save();
  191. }
  192. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  193. {
  194. var width = StkPnlRight.ActualWidth;
  195. BtnAdd.Margin = new Thickness(8,13,width,7);
  196. if (this.WindowState == WindowState.Maximized)
  197. {
  198. ico_max.Visibility = Visibility.Collapsed;
  199. }
  200. else
  201. {
  202. ico_max.Visibility = Visibility.Visible;
  203. }
  204. }
  205. private void TxtRename_PreviewKeyDown(object sender, KeyEventArgs e)
  206. {
  207. if(e.Key == Key.Enter)
  208. {
  209. TabablzControl.Focus();
  210. }
  211. }
  212. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  213. {
  214. if (!(DataContext as MainWindowViewModel).closeAllTabItem())
  215. {
  216. e.Cancel = true;
  217. }
  218. }
  219. private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  220. {
  221. //通过转移焦点来使文本框触发失去焦点事件
  222. var uiElement = FocusManager.GetFocusedElement(this);
  223. if (uiElement != null)
  224. {
  225. var textBox = uiElement as TextBox;
  226. //如果是下拉框本身的可输入文本框,则点击下拉框或者下拉选项时不失去焦点
  227. var parent = CommonHelper.FindVisualParent<ComboBox>(textBox);
  228. if (parent != null)
  229. {
  230. var visual = VisualTreeHelper.HitTest(parent, e.GetPosition(parent));
  231. if (visual != null)
  232. {
  233. //点击下拉框展开按钮时 不失去焦点
  234. var item = CommonHelper.FindVisualParent<ComboBox>(visual.VisualHit);
  235. if (item != null)
  236. {
  237. return;
  238. }
  239. }
  240. //选择下拉选项时,返回,不失去焦点,否则下拉框的其他选项无法选中
  241. if(parent.IsDropDownOpen)
  242. {
  243. return;
  244. }
  245. }
  246. //点击其他非文本框区域时,失去焦点
  247. if (textBox != null)
  248. {
  249. GridRoot.Focusable = true;
  250. GridRoot.Focus();
  251. }
  252. }
  253. }
  254. }
  255. }