PageEditContent.xaml.cs 61 KB

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