MainWindow.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using PDF_Office.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_Office.EventAggregators;
  19. using PDF_Office.CustomControl;
  20. using Prism.Ioc;
  21. using PDF_Office.Helper;
  22. using System.IO;
  23. using ComPDFKit.PDFDocument;
  24. using PDFSettings.Settings;
  25. using PDF_Office.Properties;
  26. namespace PDF_Office.Views
  27. {
  28. /// <summary>
  29. /// MainWindow.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class MainWindow : Window
  32. {
  33. public IEventAggregator aggregator;
  34. public IRegionManager region;
  35. public MainWindow()
  36. {
  37. InitializeComponent();
  38. }
  39. /// <summary>
  40. /// 此类因为Dragablz控件的原因需要特殊处理
  41. /// </summary>
  42. /// <param name="eventAggregator"></param>
  43. public MainWindow(IEventAggregator eventAggregator, IContainerProvider container) :this()
  44. {
  45. aggregator = eventAggregator;
  46. eventAggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Opened });
  47. }
  48. private void BtnMiniSize_Click(object sender, RoutedEventArgs e)
  49. {
  50. System.Windows.SystemCommands.MinimizeWindow(this);
  51. }
  52. private void BtnClose_Click(object sender, RoutedEventArgs e)
  53. {
  54. System.Windows.SystemCommands.CloseWindow(this);
  55. }
  56. private void BtnReStore_Click(object sender, RoutedEventArgs e)
  57. {
  58. if (this.WindowState == WindowState.Maximized)
  59. {
  60. System.Windows.SystemCommands.RestoreWindow(this);
  61. }
  62. else
  63. {
  64. System.Windows.SystemCommands.MaximizeWindow(this);
  65. }
  66. }
  67. private void Window_Activated(object sender, EventArgs e)
  68. {
  69. App.Current.MainWindow = this;
  70. App.mainWindowViewModel = this.DataContext as MainWindowViewModel;
  71. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Activated });
  72. }
  73. private void Window_Closed(object sender, EventArgs e)
  74. {
  75. aggregator?.GetEvent<DragablzWindowEvent>().Publish(new DragablzWindowEventArgs() { TabControl = TabablzControl, Type = DragablzWindowEventType.Closed });
  76. }
  77. public void LoadPdfViewer(string[] filePaths)
  78. {
  79. var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
  80. if (filePaths.Count() == 1)
  81. {
  82. if (App.OpenedFileList.Contains(filePaths[0]))
  83. {
  84. App.mainWindowViewModel.SelectItem(filePaths[0]);
  85. }
  86. else
  87. {
  88. content.OpenFile(filePaths[0]);
  89. }
  90. ToolMethod.SetFileThumbImg(filePaths[0]);
  91. }
  92. else
  93. {
  94. var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  95. if (fileList.Count <= 0)
  96. return;
  97. content.OpenFile(filePaths[0]);
  98. for (int i = 1; i < fileList.Count(); i++)
  99. {
  100. if (!App.OpenedFileList.Contains(fileList[i]))
  101. {
  102. App.mainWindowViewModel.AddTabItem(fileList[i]);
  103. }
  104. ToolMethod.SetFileThumbImg(fileList[i]);
  105. }
  106. }
  107. Settings.Default.Save();
  108. }
  109. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  110. {
  111. var width = StkPnlRight.ActualWidth;
  112. BtnAdd.Margin = new Thickness(8,13,width,7);
  113. if (this.WindowState == WindowState.Maximized)
  114. {
  115. ico_max.Visibility = Visibility.Visible;
  116. }
  117. else
  118. {
  119. ico_max.Visibility = Visibility.Collapsed;
  120. }
  121. }
  122. private void TxtRename_PreviewKeyDown(object sender, KeyEventArgs e)
  123. {
  124. if(e.Key == Key.Enter)
  125. {
  126. TabablzControl.Focus();
  127. }
  128. }
  129. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  130. {
  131. if (!(DataContext as MainWindowViewModel).closeAllTabItem())
  132. {
  133. e.Cancel = true;
  134. }
  135. }
  136. private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  137. {
  138. //通过转移焦点来使文本框触发失去焦点事件
  139. var uiElement = FocusManager.GetFocusedElement(this);
  140. if (uiElement != null)
  141. {
  142. var textBox = uiElement as TextBox;
  143. //如果是下拉框本身的可输入文本框,则点击下拉框或者下拉选项时不失去焦点
  144. var parent = CommonHelper.FindVisualParent<ComboBox>(textBox);
  145. if (parent != null)
  146. {
  147. var visual = VisualTreeHelper.HitTest(parent, e.GetPosition(parent));
  148. if (visual != null)
  149. {
  150. //点击下拉框展开按钮时 不失去焦点
  151. var item = CommonHelper.FindVisualParent<ComboBox>(visual.VisualHit);
  152. if (item != null)
  153. {
  154. return;
  155. }
  156. }
  157. //选择下拉选项时,返回,不失去焦点,否则下拉框的其他选项无法选中
  158. if(parent.IsDropDownOpen)
  159. {
  160. return;
  161. }
  162. }
  163. //点击其他非文本框区域时,失去焦点
  164. if (textBox != null)
  165. {
  166. GridRoot.Focusable = true;
  167. GridRoot.Focus();
  168. }
  169. }
  170. }
  171. }
  172. }