PageEditContent.xaml.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Dropbox.Api.Team;
  5. using ImTools;
  6. using PDF_Master.EventAggregators;
  7. using PDF_Master.Helper;
  8. using PDF_Master.Model.PageEdit;
  9. using PDF_Master.ViewModels.PageEdit;
  10. using Prism.Events;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Runtime.InteropServices;
  17. using System.Security;
  18. using System.Text;
  19. using System.Text.RegularExpressions;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Controls;
  23. using System.Windows.Controls.Primitives;
  24. using System.Windows.Data;
  25. using System.Windows.Documents;
  26. using System.Windows.Input;
  27. using System.Windows.Interop;
  28. using System.Windows.Media;
  29. using System.Windows.Media.Imaging;
  30. using System.Windows.Navigation;
  31. using System.Windows.Shapes;
  32. using System.Windows.Threading;
  33. using WpfToolkit.Controls;
  34. using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum;
  35. namespace PDF_Master.Views.PageEdit
  36. {
  37. /// <summary>
  38. /// PageEditContent.xaml 的交互逻辑
  39. /// </summary>
  40. public partial class PageEditContent : UserControl
  41. {
  42. /// <summary>
  43. /// 用于通知刷新图片的事件
  44. /// 因为目前的图片刷新策略依赖很多UI事件判断,因为将主要判断逻辑放在UI层,VM负责图片刷新,非必要情况尽量不要采用这种形式
  45. /// </summary>
  46. private IEventAggregator eventor;
  47. /// <summary>
  48. /// 暂存的唯一索引值,用于区分多页签
  49. /// </summary>
  50. private string unicode;
  51. /// <summary>
  52. /// 是否是加载时第一次触发滚动
  53. /// </summary>
  54. private bool isFirstScrollChange = true;
  55. /// <summary>
  56. /// 用于判断滚轮停止的计时器
  57. /// </summary>
  58. private DispatcherTimer timer = new DispatcherTimer();
  59. /// <summary>
  60. /// 判断是否开始框选
  61. /// </summary>
  62. private bool startChoose = false;
  63. /// <summary>
  64. /// 框选的起始位置
  65. /// </summary>
  66. private Point starPosition = new Point();
  67. /// <summary>
  68. /// 记录当前滑块的状态
  69. /// </summary>
  70. private ScrollEventType scrolltype = ScrollEventType.EndScroll;
  71. //鼠标点击时在item中的位置 实现类似点哪拖哪的细节
  72. private double item_x;
  73. private double item_y;
  74. //插入标记代表的插入位置
  75. private int InsertIndex = -1;
  76. //拖动的Item
  77. private PageEditItem tempItem;
  78. /// <summary>
  79. /// 是否正在拖拽排序中,通过该变量避免单击触发拖动
  80. /// </summary>
  81. private bool isDraging = false;
  82. //是否正在从外部拖入文件
  83. public bool isDragingEnter { get; set; } = false;
  84. //自动滚动速度
  85. private int speed = 0;
  86. private PageEditContentViewModel viewModel;
  87. private List<string> dropMoveFiles = null;
  88. public PageEditContent()
  89. {
  90. InitializeComponent();
  91. dropMoveFiles = new List<string>();
  92. }
  93. public PageEditContent(IEventAggregator eventAggregator) : this()
  94. {
  95. eventor = eventAggregator;
  96. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  97. timer = new DispatcherTimer();
  98. timer.Interval = TimeSpan.FromSeconds(0.3);
  99. timer.Tick += Timer_Tick;
  100. viewModel = this.DataContext as PageEditContentViewModel;
  101. viewModel.PageEditItems.CollectionChanged += PageEditItems_CollectionChanged;
  102. //订阅页面刷新事件
  103. eventor.GetEvent<PageEditNotifyEvent>().Subscribe(OneNotifyEvent, e => e.Unicode == unicode);
  104. eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  105. //当退出页面编辑后
  106. //缩略图选中页面,要和当前显示的页面,一致
  107. eventAggregator.GetEvent<PageEditSelectedItemEvent>().Subscribe(OnPageEditSelectedItemRefreshEvent, e => e.Unicode == unicode);
  108. }
  109. private void OnPageEditSelectedItemRefreshEvent(PageEditSelectedItemEventArgs args)
  110. {
  111. if (viewModel.IsBOTAThumb == Visibility.Visible && args.SelectedItemIndex != -1)
  112. {
  113. viewModel.ListSelectedIndex = args.SelectedItemIndex;
  114. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  115. }
  116. }
  117. private void Timer_Tick(object sender, EventArgs e)
  118. {
  119. PulishEvent();
  120. timer?.Stop();
  121. }
  122. private void PageEditItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  123. {
  124. //加入loaded判断,避免显示前频繁进入该事件,影响BOTA切换事件
  125. if (this.IsLoaded)
  126. {
  127. //未选 显示页面大小,插入文件后 会影响
  128. //if (viewModel.IsShowPageSize)
  129. //{
  130. // viewModel.ShowedPageSize = Visibility.Visible;
  131. // viewModel.HidePageSize = Visibility.Collapsed;
  132. //}
  133. //else
  134. //{
  135. // viewModel.ShowedPageSize = Visibility.Collapsed;
  136. // viewModel.HidePageSize = Visibility.Visible;
  137. //}
  138. //BOTA缩略图里 插入子项时,刷新子项大小
  139. if (GridBOTAHeader.Visibility == Visibility.Visible && e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
  140. {
  141. ItemSuitAcutalWidth(this.ActualWidth);
  142. }
  143. //补充判断条件,修复页数多的文档,在BOTA展开的情况下,切换到页面编辑会卡死的问题
  144. if (GridBOTAHeader.Visibility != Visibility.Visible && !viewModel.Initializing)
  145. {
  146. //页面编辑模式下,页面总数变化后用于通知BOTA面板进行数据集刷新
  147. //只需要页面编辑通知缩略图,不需要缩略图通知页面编辑,因为现在每次导航进去时都会刷新item个数
  148. eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode, NotifyType.PageCountChanged));
  149. }
  150. }
  151. }
  152. private void OneNotifyEvent(PageEditNotifyEventArgs e)
  153. {
  154. switch (e.Type)
  155. {
  156. case NotifyType.RefreshPage:
  157. //计算当前区域的页面范围后传递回VM
  158. PulishEvent();
  159. break;
  160. case NotifyType.SelectItems:
  161. int index = 0;
  162. //根据Vm的参数选中部分页面
  163. if (e.PageRange.Count == 1)
  164. {
  165. ListPageEdit.SelectedIndex = e.PageRange[0] - 1;
  166. if (ListPageEdit.SelectedItem != null)
  167. {
  168. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  169. index = ((PageEditItem)ListPageEdit.SelectedItem).PageNumber - 1;
  170. }
  171. }
  172. else
  173. {
  174. ListPageEdit.SelectedItems.Clear();
  175. for (int i = 0; i < e.PageRange.Count; i++)
  176. {
  177. index = e.PageRange[i] - 1;
  178. if (index < ListPageEdit.Items.Count)
  179. {
  180. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[index]);
  181. //页码 下拉框 ,选择全选页面、奇数、偶数等情况,偶现IsMultiSelected=false,这里先强制设置一下
  182. viewModel.IsMultiSelected = true;
  183. }
  184. if (ListPageEdit.SelectedItems.Count > 0)
  185. {
  186. if (ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[ListPageEdit.SelectedItems.Count - 1]) == -1)
  187. {
  188. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  189. }
  190. else
  191. {
  192. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItems[ListPageEdit.SelectedItems.Count - 1]);
  193. }
  194. }
  195. }
  196. }
  197. //以下方法 实用性不强,会跳转失败
  198. //缩略图 复制粘贴后,选中页面,要与阅读视图 对应
  199. //if (viewModel.viewContentViewModel.isInPageEdit == false && GridBOTAHeader.Visibility == Visibility.Visible)
  200. //{
  201. // viewModel.PDFViewer.GoToPage(index);
  202. //}
  203. break;
  204. case NotifyType.PageCountChanged:
  205. if (GridBOTAHeader.Visibility == Visibility.Visible)
  206. {
  207. viewModel?.GetSourceItems(false);
  208. }
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. private void CleanSelectAll(CleanSelectAllArgs obj)
  215. {
  216. ListPageEdit.SelectedItems.Clear();
  217. ListPageEdit.SelectedIndex = viewModel.PDFViewer.CurrentIndex;
  218. }
  219. #region UI事件
  220. /// <summary>
  221. /// 每次显示的时候就触发事件,刷新所有图片
  222. /// </summary>
  223. /// <param name="sender"></param>
  224. /// <param name="e"></param>
  225. private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  226. {
  227. if ((bool)e.NewValue)
  228. {
  229. //BOTA缩略图模式 需要先计算item宽度,再进行渲染页面的计算
  230. if (GridBOTAHeader.Visibility == Visibility.Visible)
  231. {
  232. ItemSuitAcutalWidth(this.ActualWidth);
  233. }
  234. //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
  235. PulishEvent();
  236. //当前页面发生变化时通过ScrollChanged事件来刷新图片
  237. isFirstScrollChange = true;
  238. }
  239. }
  240. /// <summary>
  241. /// 鼠标滚轮滚动时触发的事件
  242. /// </summary>
  243. /// <param name="sender"></param>
  244. /// <param name="e"></param>
  245. public void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
  246. {
  247. if (isFirstScrollChange)
  248. {
  249. PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
  250. isFirstScrollChange = false;
  251. return;
  252. }
  253. else
  254. {
  255. if (e.VerticalChange != 0)
  256. timer?.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
  257. }
  258. }
  259. /// <summary>
  260. /// 拖动右侧滑块时触发的事件
  261. /// </summary>
  262. /// <param name="sender"></param>
  263. /// <param name="e"></param>
  264. private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
  265. {
  266. scrolltype = e.ScrollEventType;
  267. if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
  268. {
  269. timer?.Stop();
  270. return;
  271. }
  272. PulishEvent();
  273. }
  274. #endregion UI事件
  275. #region 方法
  276. /// <summary>
  277. /// 发布事件
  278. /// </summary>
  279. public void PulishEvent()
  280. {
  281. //判空 避免某些乱七八糟的情况下异常
  282. if (ListPageEdit != null & ListPageEdit.Items.Count > 0)
  283. {
  284. var itemSize = new Size((ListPageEdit.Items[0] as PageEditItem).ItemSize.Width + 32, (ListPageEdit.Items[0] as PageEditItem).ItemSize.Height + 30);
  285. var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5, 10, 5, 10));
  286. if ((bool)TbnTwoLine.IsChecked)
  287. {
  288. ///双列模式下要计算两列item
  289. range = new Tuple<int, int, int>(range.Item1, range.Item2 * 2, range.Item3);
  290. }
  291. eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
  292. }
  293. }
  294. /// <summary>
  295. /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
  296. /// 返回值为页面范围 从1开始
  297. /// </summary>
  298. /// <param name="view"></param>
  299. /// <param name="itemSize"></param>
  300. /// <param name="itemMargin"></param>
  301. /// <returns></returns>
  302. private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  303. {
  304. //var scrollViewer = GetScrollHost(view);
  305. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  306. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  307. return new Tuple<int, int, int>(0, 0, 0);
  308. try
  309. {
  310. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  311. var currentWidth = scrollViewer.ActualWidth;
  312. //计算当前窗口大小能显示的行数和列数
  313. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  314. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  315. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  316. preItemCount = preItemCount * columnCount;
  317. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  318. preEnd = preEnd * columnCount + columnCount - 1;
  319. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  320. return new Tuple<int, int, int>(
  321. Math.Max(preItemCount, 0),
  322. Math.Min(view.Items.Count, preEnd),
  323. middle);
  324. }
  325. catch { }
  326. return new Tuple<int, int, int>(0, 0, 0);
  327. }
  328. /// <summary>
  329. /// 获取listbox里的ScrollViewer对象
  330. /// 留意如果有重写ListBox对象,该方法可能无效
  331. /// </summary>
  332. /// <param name="listBox"></param>
  333. /// <returns></returns>
  334. private ScrollViewer GetScrollHost(ListBox listBox)
  335. {
  336. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  337. {
  338. int s = VisualTreeHelper.GetChildrenCount(listBox);
  339. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  340. if (border != null)
  341. {
  342. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  343. }
  344. }
  345. return null;
  346. }
  347. #endregion 方法
  348. /// <summary>
  349. /// 输入框显示时,自动获取焦点
  350. /// </summary>
  351. /// <param name="sender"></param>
  352. /// <param name="e"></param>
  353. private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  354. {
  355. if ((bool)e.NewValue)
  356. {
  357. (sender as Control).Focus();
  358. }
  359. }
  360. private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
  361. {
  362. try
  363. {
  364. if (e.LeftButton == MouseButtonState.Pressed && !isDragingEnter)
  365. {
  366. //鼠标框选逻辑
  367. if (startChoose)
  368. {
  369. var position = e.GetPosition(ListPageEdit);
  370. if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
  371. {
  372. startChoose = false;
  373. RectChoose.Visibility = Visibility.Collapsed;
  374. //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
  375. Mouse.Capture(null);
  376. return;
  377. }
  378. //矩形框内的item设为选中
  379. DoSelectItems();
  380. return;
  381. }
  382. //拖拽排序的逻辑
  383. else
  384. {
  385. var pos = e.GetPosition(ListPageEdit);
  386. if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
  387. {
  388. LineInset.Visibility = Visibility.Collapsed;
  389. ImgPicture.Visibility = Visibility.Collapsed;
  390. return;
  391. }
  392. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  393. if (result == null)
  394. {
  395. return;
  396. }
  397. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  398. if (listBoxItem == null)
  399. {
  400. return;
  401. }
  402. isDragingEnter = false;
  403. tempItem = listBoxItem.DataContext as PageEditItem;
  404. var item_pos = e.GetPosition(listBoxItem);
  405. if (item_pos != null)
  406. {
  407. item_x = item_pos.X;
  408. item_y = item_pos.Y;
  409. }
  410. //string filename = Guid.NewGuid().ToString() + ".pdf";
  411. //string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  412. //System.IO.File.Create(tempPath);
  413. //string[] files = new string[1];
  414. //files[0] = tempPath;
  415. var scroll = GetScrollHost(ListPageEdit);
  416. //页面 是App内的拖拽 ,还是拖拽 页面 出 App 是一起处理
  417. //将付费锁逻辑提前,避免拖拽出窗体成功
  418. if (!ServiceHelper.IAPBeforeFunction())
  419. {
  420. return;
  421. }
  422. #region 拖拽 页面 到 桌面/文件夹 ,提取文件
  423. //C:\Users\oyxh\AppData\Local\Temp\
  424. string dir = System.IO.Path.Combine(System.IO.Path.GetTempPath());
  425. if (!System.IO.Directory.Exists(dir))
  426. {
  427. System.IO.Directory.CreateDirectory(dir);
  428. }
  429. //string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  430. List<int> pages = GetSelectedItems();
  431. pages.Sort();
  432. string[] files = new string[1];
  433. if (pages.Count > 0)
  434. {
  435. //多选页面 提取成一个文件夹
  436. string filename = viewModel.PDFViewer.Document.FileName + " " + "pages " + CommonHelper.GetPageParmFromList(pages) + ".pdf";
  437. string tempPath = System.IO.Path.Combine(dir, filename);
  438. //删除 临时文件的同名文件
  439. if (File.Exists(tempPath))
  440. {
  441. File.Delete(tempPath);
  442. }
  443. string path = CommonHelper.CreateFilePath(tempPath);
  444. files[0] = path;
  445. dropMoveFiles.Add(path);
  446. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  447. string range = String.Join(",", pages);
  448. bool result1 = savedoc.ImportPages(viewModel.PDFViewer.Document, range);
  449. //if (!result1)
  450. //{
  451. // viewModel.ShowToast();
  452. // viewModel.IsLoading = Visibility.Collapsed;
  453. // return;
  454. //}
  455. result1 = savedoc.WriteToFilePath(path);
  456. if (!result1)
  457. {
  458. viewModel.ShowToast();
  459. }
  460. savedoc.Release();//释放内存
  461. }
  462. #region 多选页面 分别生成单独文件
  463. //List<int> pagenums = new List<int>();
  464. //for (int i = 0; i < viewModel.PageEditItems.Count; i++)
  465. //{
  466. // if (viewModel.PageEditItems[i].Selected)
  467. // {
  468. // pagenums.Add(viewModel.PageEditItems[i].PageNumber);
  469. // }
  470. //}
  471. //string[] files = new string[pagenums.Count];
  472. //for (int i = 0; i < pagenums.Count; i++)
  473. //{
  474. // string filename = viewModel.PDFViewer.Document.FileName + " " + pagenums[i] + ".pdf";
  475. // string tempPath = System.IO.Path.Combine(dir, filename);
  476. // string path = CommonHelper.CreateFilePath(tempPath);
  477. // files[i] = path;
  478. // dropMoveFiles.Add(path);
  479. // CPDFDocument savedoc = CPDFDocument.CreateDocument();
  480. // bool result2 = savedoc.ImportPages(viewModel.PDFViewer.Document, pagenums[i].ToString());
  481. // if (!result2)
  482. // {
  483. // savedoc.Release();
  484. // continue;
  485. // }
  486. // result2 = savedoc.WriteToFilePath(path);
  487. // if (!result2)
  488. // {
  489. // savedoc.Release();
  490. // continue;
  491. // }
  492. // savedoc.Release();//释放内存
  493. //}
  494. #endregion 多选页面 分别生成单独文件
  495. //string[] files = new string[1];
  496. //files[0] = tempPath;
  497. DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
  498. #endregion 拖拽 页面 到 桌面/文件夹 ,提取文件
  499. //禁用从窗体拖拽到桌面的方法
  500. //DataObject dataObj = new DataObject(listBoxItem);
  501. //注意!!!目前是通过DragDropEffects类型来区分app内拖拽和外部文件拖入。否则页面文件拖拽页面到PDFViewer,也会新生成一个页签
  502. //如果这里的DragDropEfeects类型有改动,也需要调整ViewContent里UserControl_Drop函数的判断逻辑
  503. DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
  504. Mouse.Capture(ListPageEdit);
  505. //CleanDropMoveFiles();
  506. return;
  507. }
  508. }
  509. RectChoose.Visibility = Visibility.Collapsed;
  510. startChoose = false;
  511. LineInset.Visibility = Visibility.Collapsed;
  512. ImgPicture.Visibility = Visibility.Collapsed;
  513. Mouse.Capture(null);
  514. }
  515. catch
  516. {
  517. LineInset.Visibility = Visibility.Collapsed;
  518. ImgPicture.Visibility = Visibility.Collapsed;
  519. }
  520. }
  521. private List<int> GetSelectedItems()
  522. {
  523. List<int> pages = new List<int>();
  524. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  525. {
  526. //这样拿 更精准
  527. if (ListPageEdit.SelectedItems[i] is PageEditItem pageEditItem)
  528. {
  529. pages.Add(pageEditItem.PageNumber);
  530. }
  531. }
  532. return pages;
  533. }
  534. /// <summary>
  535. /// 判断是否开始框选、记录框选起始位置
  536. /// </summary>
  537. /// <param name="sender"></param>
  538. /// <param name="e"></param>
  539. private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  540. {
  541. viewModel.IsMouseRightButtonDown = false;
  542. viewModel.RightSelectedIndex = -1;
  543. viewModel.RightSelectedIndexs.Clear();
  544. var pos = e.GetPosition(ListPageEdit);
  545. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  546. if (result == null)
  547. {
  548. return;
  549. }
  550. //未选中item 并且不是点击滑轨时 开始框选
  551. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  552. var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
  553. if (listBoxItem == null)
  554. {
  555. if (scroller != null)
  556. {
  557. startChoose = false;
  558. return;
  559. }
  560. //点击空白处时开始框选
  561. startChoose = true;
  562. if (ListPageEdit.SelectedItems.Count > 0)
  563. {
  564. ListPageEdit.SelectedItems.Clear();
  565. }
  566. starPosition = e.GetPosition(ListPageEdit);
  567. starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
  568. Mouse.Capture(ListPageEdit);
  569. return;
  570. }
  571. //选中了item 时,不能框选
  572. startChoose = false;
  573. isDragingEnter = false;
  574. //更改系统的选中规则,选中状态下,鼠标松开后取消选中
  575. //方便实现多选拖拽功能
  576. if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
  577. {
  578. e.Handled = true;
  579. }
  580. }
  581. private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  582. {
  583. var pos = e.GetPosition(ListPageEdit);
  584. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  585. if (result == null)
  586. {
  587. return;
  588. }
  589. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  590. if (listBoxItem == null)
  591. {
  592. return;
  593. }
  594. //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
  595. //拖拽框选之后的抬起鼠标不进入处理
  596. if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
  597. {
  598. ListPageEdit.SelectedItems.Clear();
  599. ListPageEdit.SelectedItem = listBoxItem;
  600. listBoxItem.IsSelected = true;
  601. return;
  602. }
  603. Mouse.Capture(null);
  604. //结束鼠标框选
  605. startChoose = false;
  606. RectChoose.Visibility = Visibility.Collapsed;
  607. }
  608. /// <summary>
  609. /// 获取Listobox的Wrappanel容器
  610. /// </summary>
  611. /// <param name="listBox"></param>
  612. /// <returns></returns>
  613. public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
  614. {
  615. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  616. var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
  617. return panel;
  618. }
  619. /// <summary>
  620. ///根据鼠标拖选的框 选中矩形框里面的Item
  621. /// </summary>
  622. private void DoSelectItems()
  623. {
  624. var s = GetScrollHost(ListPageEdit);
  625. Point start = new Point();
  626. //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
  627. start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
  628. var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
  629. RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
  630. RectChoose.Width = rec.Width;
  631. RectChoose.Height = rec.Height;
  632. RectChoose.Visibility = Visibility.Visible;
  633. //检测遍历所有项,筛选在矩形框中的Item
  634. for (int i = 0; i < ListPageEdit.Items.Count; i++)
  635. {
  636. var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  637. //通过这一步来避免重复误选中
  638. var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
  639. if (parent == null)
  640. continue;
  641. var v = VisualTreeHelper.GetOffset(_item);
  642. if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
  643. {
  644. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
  645. }
  646. else
  647. {
  648. ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
  649. }
  650. }
  651. return;
  652. }
  653. /// <summary>
  654. /// 退出拖拽模式
  655. /// </summary>
  656. private void ExitDraging()
  657. {
  658. LineInset.Visibility = Visibility.Collapsed;
  659. ImgPicture.Visibility = Visibility.Collapsed;
  660. isDraging = false;
  661. }
  662. /// <summary>
  663. /// 拖拽释放后的处理逻辑
  664. /// </summary>
  665. /// <param name="sender"></param>
  666. /// <param name="e"></param>
  667. private async void ListPageEdit_Drop(object sender, DragEventArgs e)
  668. {
  669. //isDragingEnter = false;
  670. if (!isDraging)
  671. {
  672. //未拖拽时隐藏插入标记和虚影
  673. LineInset.Visibility = Visibility.Collapsed;
  674. ImgPicture.Visibility = Visibility.Collapsed;
  675. return;
  676. }
  677. //拖入非法格式释放时 返回
  678. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  679. if (file != null)
  680. {
  681. #region 从外部拖拽插入文件
  682. if (isDragingEnter)
  683. {
  684. //不能将付费锁 放到 grid_DragOver,会卡顿
  685. //将付费锁逻辑提前,避免因弹窗显示后导致虚影无法消失问题
  686. if (!ServiceHelper.IAPBeforeFunction())
  687. {
  688. LineInset.Visibility = Visibility.Collapsed;
  689. ImgPicture.Visibility = Visibility.Collapsed;
  690. isDragingEnter = false;
  691. return;
  692. }
  693. //注释从外部拖拽插入文件的功能
  694. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  695. Array.Reverse(files);
  696. foreach (string item in files)
  697. {
  698. System.IO.FileInfo info = new System.IO.FileInfo(item);
  699. string fileName = viewModel.GetInsertFromFile(item);
  700. //if (System.IO.Path.GetExtension(item).ToLower() == ".pdf" && info.Length > 0)
  701. //{
  702. // int index = InsertIndex == -1 ? 0 : InsertIndex;
  703. // await viewModel.InsertFromFile(index, item);
  704. // viewModel.ReloadAfterOption(true, true, new Tuple<int, int>(0, ListPageEdit.Items.Count));
  705. //}
  706. if (info.Length > 0 && File.Exists(fileName))
  707. {
  708. int index = InsertIndex == -1 ? 0 : InsertIndex;
  709. //将文档验证是否为空,是否解密,置前,若为空直接退出
  710. bool IsLocked = viewModel.GetDocumentIsDiscryptied(fileName, null, out CPDFDocument doc, out string password);
  711. if (IsLocked == false)
  712. {
  713. LineInset.Visibility = Visibility.Collapsed;
  714. ImgPicture.Visibility = Visibility.Collapsed;
  715. isDragingEnter = false;
  716. return;
  717. }
  718. ListPageEdit.SelectedItems.Clear();
  719. bool flag = await viewModel.InsertFromFile(index, fileName, doc, password);
  720. if (flag)
  721. {
  722. viewModel.ReloadAfterOption(true, true, new Tuple<int, int>(0, ListPageEdit.Items.Count));
  723. }
  724. }
  725. }
  726. //其他文件 则新增一个页签打开
  727. // DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
  728. LineInset.Visibility = Visibility.Collapsed;
  729. ImgPicture.Visibility = Visibility.Collapsed;
  730. isDragingEnter = false;
  731. return;
  732. }
  733. #endregion 从外部拖拽插入文件
  734. //return;
  735. }
  736. var pos = e.GetPosition(ListPageEdit);
  737. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  738. if (result == null)
  739. {
  740. //超出当前可控区域
  741. ExitDraging();
  742. return;
  743. }
  744. //查找元数据
  745. //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
  746. if (tempItem == null)
  747. {
  748. ExitDraging();
  749. return;
  750. }
  751. //查找目标数据
  752. int targetindex = 0;//目标插入位置
  753. if (InsertIndex != -1)
  754. {
  755. //往前移动时 此index 不是准确的,需要处理++
  756. targetindex = InsertIndex;
  757. }
  758. //else//基本不会命中 仅作为保险措施
  759. //{
  760. // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  761. // if (listBoxItem == null)
  762. // {
  763. // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
  764. // ExitDraging();
  765. // return;
  766. // }
  767. // var targetPerson = listBoxItem;
  768. // targetPerson.Opacity = 1;
  769. // sourcePerson.Opacity = 1;
  770. // if (ReferenceEquals(targetPerson, sourcePerson))
  771. // {
  772. // ExitDraging();
  773. // return;
  774. // }
  775. // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
  776. //}
  777. List<ListBoxItem> list = new List<ListBoxItem>();
  778. List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
  779. //开始排序
  780. List<int> pages = new List<int>();
  781. //要先对所有选中项 根据页码排序
  782. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  783. {
  784. var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
  785. pages.Add(pageindex);//存入的为页码索引值
  786. }
  787. if (pages.Count <= 0)
  788. {
  789. ExitDraging();
  790. return;
  791. }
  792. viewModel.DragToSort(targetindex, pages);
  793. ExitDraging();
  794. isDraging = false;
  795. CleanDropMoveFiles();
  796. }
  797. /// <summary>
  798. /// 拖拽完成后,清空临时目录 的文件
  799. /// </summary>
  800. private void CleanDropMoveFiles()
  801. {
  802. if (dropMoveFiles != null)
  803. {
  804. if (dropMoveFiles.Count > 0)
  805. {
  806. for (int i = 0; i < dropMoveFiles.Count; i++)
  807. {
  808. if (File.Exists(dropMoveFiles[i]))
  809. {
  810. File.Delete(dropMoveFiles[i]);
  811. }
  812. }
  813. dropMoveFiles.Clear();
  814. }
  815. }
  816. }
  817. private void MidLane_Drop(object sender, DragEventArgs e)
  818. {
  819. ListPageEdit_Drop(sender, e);
  820. }
  821. private void ImgPicture_Drop(object sender, DragEventArgs e)
  822. {
  823. ListPageEdit_Drop(sender, e);
  824. }
  825. /// <summary>
  826. /// 拖拽事件写在外部的Grid里,会更加流畅
  827. /// </summary>
  828. /// <param name="sender"></param>
  829. /// <param name="e"></param>
  830. private void Grid_DragOver(object sender, DragEventArgs e)
  831. {
  832. try
  833. {
  834. Trace.WriteLine("Grid_DragOver 文件拖出App");
  835. //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
  836. if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
  837. return;
  838. //从外部拖入文件时 返回
  839. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  840. if (file != null)
  841. {
  842. }
  843. //滚动后有 位置不准确 要减去滚动偏移量
  844. //控制线的位置
  845. var pos = e.GetPosition(ListPageEdit);
  846. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  847. if (result == null)
  848. {
  849. return;
  850. //MidLane.Visibility = Visibility.Collapsed;
  851. //return;
  852. }
  853. //获取当前鼠标指针下的容器
  854. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  855. if (listBoxItem == null)
  856. {
  857. //页面 拖出App后存在极少数情况会显示 标记线、缩略图,注释掉=》item中间不会有虚影,观感不好
  858. //LineInset.Visibility = Visibility.Collapsed;
  859. //ImgPicture.Visibility = Visibility.Collapsed;
  860. //MidLane.Visibility = Visibility.Collapsed;
  861. //return;
  862. }
  863. #region 计算虚影位置
  864. //xaml层 要设置 虚影控件为左上
  865. double xPos = 0;
  866. double yPos = 0;
  867. //内部拖动
  868. if (!isDragingEnter)
  869. {
  870. if (tempItem != null)
  871. {
  872. ImgPicture.Width = tempItem.ItemSize.Width;
  873. ImgPicture.Height = tempItem.ItemSize.Height;
  874. ImgPicture.Source = tempItem.Image;
  875. xPos = e.GetPosition(ListPageEdit).X - item_x;
  876. yPos = e.GetPosition(ListPageEdit).Y - item_y;
  877. }
  878. }
  879. else
  880. {
  881. DragDropHelper.DragOver(this, e);
  882. //从外部拖入的逻辑
  883. //var pic = ToBitmapSource(dragingEnterPath);
  884. //ShadowPicture.Width = pic.Width;
  885. //ShadowPicture.Height = pic.Height;
  886. //ShadowPicture.Source = pic;
  887. //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
  888. //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
  889. }
  890. ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
  891. #endregion 计算虚影位置
  892. #region 计算插入标记位置
  893. var scroll = GetScrollHost(ListPageEdit);
  894. if (listBoxItem != null)
  895. {
  896. //虚拟化影响到该值计算
  897. var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
  898. LineInset.Visibility = Visibility.Visible;
  899. if (!isDragingEnter)
  900. {
  901. ImgPicture.Visibility = Visibility.Visible;
  902. }
  903. else
  904. {
  905. ImgPicture.Visibility = Visibility.Collapsed;
  906. }
  907. var panel = GetWrapPanel(ListPageEdit);
  908. //var item = panel.ItemSize.Width;
  909. //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
  910. var item = listBoxItem.DesiredSize.Width;
  911. int count = (int)(panel.ViewportWidth / item);
  912. var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
  913. LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
  914. if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
  915. {
  916. LineInset.X2 = LineInset.X1 = p.X - gap / 2;
  917. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
  918. }
  919. else
  920. {
  921. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
  922. }
  923. Console.WriteLine("InsertIndex:{0}\r\n", InsertIndex);
  924. //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
  925. LineInset.Y1 = p.Y;
  926. if (LineInset.Y1 < 0)//避免超出上边界
  927. {
  928. LineInset.Y1 = 0;
  929. }
  930. //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
  931. LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
  932. if (LineInset.Y2 < 0)
  933. {
  934. LineInset.Y2 = 0;
  935. }
  936. }
  937. #endregion 计算插入标记位置
  938. //暂时处理 鼠标移出边框时,虚影的显示问题
  939. //230612 拖拽逻辑,为了更好的效果体现,暂时注释
  940. if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
  941. {
  942. LineInset.Visibility = Visibility.Collapsed;
  943. ImgPicture.Visibility = Visibility.Collapsed;
  944. }
  945. if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
  946. {
  947. LineInset.Visibility = Visibility.Collapsed;
  948. ImgPicture.Visibility = Visibility.Collapsed;
  949. }
  950. #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  951. speed = 0;
  952. if (pos.Y >= ListPageEdit.ActualHeight - 30)
  953. {
  954. speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
  955. }
  956. else if (pos.Y <= 30)
  957. {
  958. speed = (int)(pos.Y - 30);
  959. }
  960. var v = scroll.VerticalOffset;
  961. scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
  962. #endregion 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  963. }
  964. catch
  965. {
  966. }
  967. }
  968. /// <summary>
  969. /// 右键不会选中页面,不要跳转
  970. /// </summary>
  971. /// <param name="sender"></param>
  972. /// <param name="e"></param>
  973. private void ListBoxItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  974. {
  975. //viewModel.IsAddPages = false;
  976. viewModel.IsMouseRightButtonDown = true;
  977. viewModel.RightSelectedIndex = -1;
  978. viewModel.RightSelectedIndexs.Clear();
  979. var item = (sender as ListBoxItem).DataContext as PageEditItem;
  980. if (item != null)
  981. {
  982. viewModel.RightSelectedIndex = item.PageNumber - 1;
  983. if (ListPageEdit.SelectedItems.Count > 1)
  984. {
  985. viewModel.IsMultiSelected = true;
  986. viewModel.maxSelectedIndex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[ListPageEdit.SelectedItems.Count - 1]);
  987. foreach (var page in ListPageEdit.SelectedItems)
  988. {
  989. if (page is PageEditItem pageEdit)
  990. {
  991. viewModel.RightSelectedIndexs.Add(pageEdit.PageNumber - 1);
  992. }
  993. }
  994. }
  995. else
  996. {
  997. if (viewModel.RightSelectedIndex != viewModel.ListSelectedIndex)
  998. {
  999. viewModel.ListSelectedIndex = viewModel.RightSelectedIndex;
  1000. viewModel.ListSelectedIndex = -1;
  1001. }
  1002. viewModel.IsMultiSelected = false;
  1003. viewModel.maxSelectedIndex = -1;
  1004. }
  1005. }
  1006. //拦截响应选中事件
  1007. e.Handled = true;
  1008. }
  1009. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  1010. {
  1011. //viewModel.IsAddPages = false;
  1012. viewModel.IsMouseRightButtonDown = false;
  1013. viewModel.RightSelectedIndex = -1;
  1014. viewModel.RightSelectedIndexs.Clear();
  1015. //增加辅助判断,防止误触发 拖动排序
  1016. //为了排除 页面 拖出App后,点击选择页面,造成拖动
  1017. isDraging = false;
  1018. if (GridBOTAHeader.Visibility == Visibility.Visible && viewModel.PDFViewer != null)
  1019. {
  1020. if (Keyboard.Modifiers == ModifierKeys.Shift || Keyboard.Modifiers == ModifierKeys.Control)
  1021. {
  1022. return;
  1023. }
  1024. var item = (sender as ListBoxItem).DataContext as PageEditItem;
  1025. if (item != null)
  1026. {
  1027. viewModel.PDFViewer.GoToPage(item.PageNumber - 1);
  1028. }
  1029. }
  1030. }
  1031. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  1032. {
  1033. if (e.ChangedButton == MouseButton.Left)
  1034. {
  1035. //双击回到PDFViewer界面
  1036. var item = sender as ListBoxItem;
  1037. if (item != null)
  1038. {
  1039. var index = ListPageEdit.ItemContainerGenerator.IndexFromContainer(item);
  1040. //双击事件无法绑定Command 暂时采用这种方式调用VM里的方法
  1041. viewModel.BackToPDFViewer(index);
  1042. }
  1043. }
  1044. }
  1045. private void PageEdit_SizeChanged(object sender, SizeChangedEventArgs e)
  1046. {
  1047. //BOTA缩略图模式下需要调整Item宽度
  1048. if (GridBOTAHeader.Visibility == Visibility.Visible)
  1049. {
  1050. ItemSuitAcutalWidth(e.NewSize.Width);
  1051. }
  1052. if (e.NewSize.Width > 0)
  1053. {
  1054. //宽度变化后刷新一次视图 修复重复点击缩略图后图片不显示的问题
  1055. PulishEvent();
  1056. }
  1057. }
  1058. private void ItemSuitAcutalWidth(double width)
  1059. {
  1060. //缩略图模式下,保持一列或者两列显示
  1061. if ((bool)TbnTwoLine.IsChecked)
  1062. {
  1063. var itemwidth = (width - 140) / 2;
  1064. if (itemwidth < 0)
  1065. {
  1066. itemwidth = width / 4;
  1067. }
  1068. var itemHeight = 294 * itemwidth / 208.0;
  1069. viewModel.ChangeItemSize(new Size(itemwidth, itemHeight));
  1070. }
  1071. else
  1072. {
  1073. var itemwidth = 0.6 * width;
  1074. var itemheight = 294 * itemwidth / 208.0;
  1075. viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
  1076. }
  1077. }
  1078. private void TbnTwoLine_Click(object sender, RoutedEventArgs e)
  1079. {
  1080. ItemSuitAcutalWidth(this.ActualWidth);
  1081. PulishEvent();
  1082. }
  1083. /// <summary>
  1084. /// 用来判断是否有外界拖入的事件
  1085. /// </summary>
  1086. /// <param name="sender"></param>
  1087. /// <param name="e"></param>
  1088. private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
  1089. {
  1090. Trace.WriteLine("Grid_PreviewDragEnter 文件拖出App");
  1091. var files = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  1092. if (files == null)//为null 表示内部拖动 触发的
  1093. {
  1094. return;
  1095. }
  1096. else
  1097. {
  1098. string temp = System.IO.Path.Combine(System.IO.Path.GetTempPath());
  1099. isDragingEnter = false;
  1100. foreach (string file in files)
  1101. {
  1102. if (File.Exists(file))
  1103. {
  1104. string dic = System.IO.Path.GetDirectoryName(file) + "\\";
  1105. //在 App内拖拽,或将 页面 拖出App
  1106. if (string.Compare(dic, temp, StringComparison.OrdinalIgnoreCase) == 0)
  1107. {
  1108. isDragingEnter = false;
  1109. }
  1110. //从外部 拖文件 进App
  1111. else
  1112. {
  1113. System.IO.FileInfo info = new System.IO.FileInfo(file);
  1114. // 文件里包含有pdf格式文件,就允许拖入
  1115. string type = System.IO.Path.GetExtension(file).ToLower();
  1116. if (viewModel.Txt.Contains(type) ||
  1117. viewModel.Word.Contains(type) ||
  1118. viewModel.Ppt.Contains(type) ||
  1119. viewModel.Excel.Contains(type) ||
  1120. viewModel.Html.Contains(type) ||
  1121. viewModel.Image.Contains(type) ||
  1122. viewModel.Pdf.Contains(type) && info.Length > 0)
  1123. {
  1124. isDragingEnter = true;
  1125. }
  1126. //if (System.IO.Path.GetExtension(file).ToLower() == ".pdf" && info.Length > 0)
  1127. //{
  1128. // isDragingEnter = true;
  1129. //}
  1130. }
  1131. ////文件拖出app 最后在这里 结束
  1132. //if (isExtractEnter)
  1133. //{
  1134. // isExtractEnter = false;
  1135. // return;
  1136. //}
  1137. //else
  1138. //{
  1139. // System.IO.FileInfo info = new System.IO.FileInfo(file);
  1140. // //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  1141. // if (System.IO.Path.GetExtension(file).ToLower() == ".pdf" && info.Length > 0)
  1142. // {
  1143. // isDragingEnter = true;
  1144. // isExtractEnter = false;
  1145. // }
  1146. //}
  1147. }
  1148. }
  1149. viewModel.IsAddPages = true;
  1150. DragDropHelper.DragEnter(this, e);
  1151. }
  1152. }
  1153. private void Grid_DragLeave(object sender, DragEventArgs e)
  1154. {
  1155. DragDropHelper.DragLeave();
  1156. }
  1157. private void Grid_Drop(object sender, DragEventArgs e)
  1158. {
  1159. DragDropHelper.Drop(this, e);
  1160. }
  1161. private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
  1162. {
  1163. //增加辅助判断,防止误触发 拖动排序
  1164. //较大幅度拖动才能触发排序
  1165. isDraging = true;
  1166. }
  1167. private void Border_Drop(object sender, DragEventArgs e)
  1168. {
  1169. }
  1170. private async void TextBoxEx_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  1171. {
  1172. var text = sender as TextBox;
  1173. if (text != null)
  1174. {
  1175. await Task.Delay(50);
  1176. text.Focus();
  1177. }
  1178. }
  1179. private void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
  1180. {
  1181. var text = sender as TextBox;
  1182. if (text.Visibility == Visibility.Visible && string.IsNullOrEmpty(text.Text))
  1183. {
  1184. text.Focus();
  1185. }
  1186. }
  1187. private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
  1188. {
  1189. //因为需要处理点击按钮或者点击列表失去焦点是 不进行弹窗 因此写在cs里,方便获取UI控件
  1190. var element = FocusManager.GetFocusedElement(this);
  1191. var visual = VisualTreeHelper.HitTest(this, Mouse.GetPosition(this));
  1192. //点击其他区域失去焦点时 不响应页码判断逻辑
  1193. if (visual != null)
  1194. {
  1195. var listbox = CommonHelper.FindVisualParent<ListBox>(visual.VisualHit);
  1196. if (listbox != null)
  1197. {
  1198. //点击在listbox列表内
  1199. return;
  1200. }
  1201. var image = CommonHelper.FindVisualParent<Image>(visual.VisualHit);
  1202. if (image != null)
  1203. {
  1204. //点击在listbox列表内,具体某一项时,上面的判断调整可能失效,需要补充条件
  1205. return;
  1206. }
  1207. var button = CommonHelper.FindVisualParent<Button>(visual.VisualHit);
  1208. if (button != null)
  1209. {
  1210. //点击在按钮时
  1211. return;
  1212. }
  1213. var combox = CommonHelper.FindVisualParent<ComboBox>(visual.VisualHit);
  1214. if (combox != null)
  1215. {
  1216. //点击在下拉框时
  1217. return;
  1218. }
  1219. viewModel.lostfocus(e);
  1220. }
  1221. }
  1222. private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  1223. {
  1224. var visual = VisualTreeHelper.HitTest(GridHeader, Mouse.GetPosition(GridHeader));
  1225. if (visual == null)
  1226. {
  1227. //点击区域不是二级菜单区域的Grid时,返回
  1228. return;
  1229. }
  1230. var menuItem = CommonHelper.FindVisualParent<ComboBoxItem>(visual.VisualHit);
  1231. if (menuItem != null)
  1232. {
  1233. //点击位置为下拉菜单子项时,返回
  1234. return;
  1235. }
  1236. if (visual != null && (visual.VisualHit as Grid) != null)
  1237. {
  1238. //点击工具栏空白处时 失去焦点
  1239. ListPageEdit.Focus();
  1240. }
  1241. }
  1242. private void MenuItem_Click(object sender, RoutedEventArgs e)
  1243. {
  1244. //因为需要获取准确的选中页面,因此把入口放在.cs代码里
  1245. List<int> lists = new List<int>();
  1246. foreach (PageEditItem item in ListPageEdit.SelectedItems)
  1247. {
  1248. lists.Add(item.PageNumber);
  1249. }
  1250. viewModel.print(lists);
  1251. }
  1252. private void PageEdit_Unloaded(object sender, RoutedEventArgs e)
  1253. {
  1254. //释放定时器 避免内存泄露
  1255. timer?.Stop();
  1256. if (timer != null)
  1257. {
  1258. timer.Tick -= Timer_Tick;
  1259. timer = null;
  1260. }
  1261. }
  1262. private void PageEdit_Loaded(object sender, RoutedEventArgs e)
  1263. {
  1264. if (timer == null)
  1265. {
  1266. timer = new DispatcherTimer();
  1267. timer.Tick += Timer_Tick;
  1268. }
  1269. }
  1270. private void CmbPageRange_PreviewTextInput(object sender, TextCompositionEventArgs e)
  1271. {
  1272. Regex regex = new Regex("[^0-9,-]+"); // 限制仅允许数字、逗号和横杠
  1273. e.Handled = regex.IsMatch(e.Text);
  1274. }
  1275. private void BtnInsert_Initialized(object sender, EventArgs e)
  1276. {
  1277. this.BtnInsert.ContextMenu = null;
  1278. }
  1279. private void BtnInsert_Click(object sender, RoutedEventArgs e)
  1280. {
  1281. MenuInsert.PlacementTarget = this.BtnInsert;
  1282. MenuInsert.IsOpen = true;
  1283. }
  1284. private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
  1285. {
  1286. var menu = sender as System.Windows.Controls.ContextMenu;
  1287. if (menu != null)
  1288. {
  1289. List<int> pages = GetSelectedItems();
  1290. if (pages.Count >= 2)
  1291. {
  1292. foreach (var item in menu.Items)
  1293. {
  1294. if (item is MenuItem menuItem)
  1295. {
  1296. if (menuItem.Tag == null)
  1297. {
  1298. continue;
  1299. }
  1300. //右键菜单 先按照230616的原型,后续原型改动在调整
  1301. if (viewModel.IsBOTAThumb == Visibility.Visible)
  1302. {
  1303. if (menuItem.Tag.ToString() == "Insert")
  1304. {
  1305. menuItem.Visibility = Visibility.Collapsed;
  1306. }
  1307. if (menuItem.Tag.ToString() == "Reverse")
  1308. {
  1309. menuItem.Visibility = Visibility.Collapsed;
  1310. }
  1311. if (menuItem.Tag.ToString() == "Split")
  1312. {
  1313. menuItem.Visibility = Visibility.Collapsed;
  1314. }
  1315. if (menuItem.Tag.ToString() == "Extract")
  1316. {
  1317. menuItem.Visibility = Visibility.Collapsed;
  1318. }
  1319. if (menuItem.Tag.ToString() == "Replace")
  1320. {
  1321. menuItem.Visibility = Visibility.Collapsed;
  1322. }
  1323. }
  1324. else
  1325. {
  1326. if (menuItem.Tag.ToString() == "Insert")
  1327. {
  1328. menuItem.Visibility = Visibility.Collapsed;
  1329. }
  1330. if (menuItem.Tag.ToString() == "Reverse")
  1331. {
  1332. menuItem.Visibility = Visibility.Visible;
  1333. }
  1334. if (menuItem.Tag.ToString() == "Split")
  1335. {
  1336. menuItem.Visibility = Visibility.Visible;
  1337. }
  1338. }
  1339. }
  1340. }
  1341. }
  1342. else
  1343. {
  1344. foreach (var item in menu.Items)
  1345. {
  1346. if (item is MenuItem menuItem)
  1347. {
  1348. if (menuItem.Tag == null)
  1349. {
  1350. continue;
  1351. }
  1352. if (viewModel.IsBOTAThumb == Visibility.Visible)
  1353. {
  1354. if (menuItem.Tag.ToString() == "Insert")
  1355. {
  1356. menuItem.Visibility = Visibility.Collapsed;
  1357. }
  1358. if (menuItem.Tag.ToString() == "Reverse")
  1359. {
  1360. menuItem.Visibility = Visibility.Collapsed;
  1361. }
  1362. if (menuItem.Tag.ToString() == "Split")
  1363. {
  1364. menuItem.Visibility = Visibility.Collapsed;
  1365. }
  1366. if (menuItem.Tag.ToString() == "Extract")
  1367. {
  1368. menuItem.Visibility = Visibility.Collapsed;
  1369. }
  1370. if (menuItem.Tag.ToString() == "Replace")
  1371. {
  1372. menuItem.Visibility = Visibility.Collapsed;
  1373. }
  1374. }
  1375. else
  1376. {
  1377. if (menuItem.Tag.ToString() == "Insert")
  1378. {
  1379. menuItem.Visibility = Visibility.Visible;
  1380. }
  1381. if (menuItem.Tag.ToString() == "Reverse")
  1382. {
  1383. menuItem.Visibility = Visibility.Collapsed;
  1384. }
  1385. if (menuItem.Tag.ToString() == "Split")
  1386. {
  1387. menuItem.Visibility = Visibility.Collapsed;
  1388. }
  1389. }
  1390. }
  1391. }
  1392. }
  1393. }
  1394. }
  1395. }
  1396. }