PageEditContent.xaml.cs 47 KB

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