PageEditContent.xaml.cs 47 KB

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