PageEditContent.xaml.cs 41 KB

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