PageEditContent.xaml.cs 66 KB

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