PageEditContent.xaml.cs 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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. int index = 0;
  161. //根据Vm的参数选中部分页面
  162. if (e.PageRange.Count == 1)
  163. {
  164. ListPageEdit.SelectedIndex = e.PageRange[0] - 1;
  165. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  166. index = ((PageEditItem)ListPageEdit.SelectedItem).PageNumber - 1;
  167. }
  168. else
  169. {
  170. ListPageEdit.SelectedItems.Clear();
  171. for (int i = 0; i < e.PageRange.Count; i++)
  172. {
  173. index = e.PageRange[i] - 1;
  174. if (index < ListPageEdit.Items.Count)
  175. {
  176. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[index]);
  177. //页码 下拉框 ,选择全选页面、奇数、偶数等情况,偶现IsMultiSelected=false,这里先强制设置一下
  178. viewModel.IsMultiSelected = true;
  179. }
  180. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  181. }
  182. }
  183. //缩略图 复制粘贴后,选中页面,要与阅读视图 对应
  184. if (viewModel.viewContentViewModel.isInPageEdit == false && GridBOTAHeader.Visibility == Visibility.Visible)
  185. {
  186. viewModel.PDFViewer.GoToPage(index);
  187. }
  188. break;
  189. case NotifyType.PageCountChanged:
  190. if (GridBOTAHeader.Visibility == Visibility.Visible)
  191. {
  192. viewModel?.GetSourceItems(false);
  193. }
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. private void CleanSelectAll(CleanSelectAllArgs obj)
  200. {
  201. ListPageEdit.SelectedItems.Clear();
  202. ListPageEdit.SelectedIndex = viewModel.PDFViewer.CurrentIndex;
  203. }
  204. #region UI事件
  205. /// <summary>
  206. /// 每次显示的时候就触发事件,刷新所有图片
  207. /// </summary>
  208. /// <param name="sender"></param>
  209. /// <param name="e"></param>
  210. private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  211. {
  212. if ((bool)e.NewValue)
  213. {
  214. //BOTA缩略图模式 需要先计算item宽度,再进行渲染页面的计算
  215. if (GridBOTAHeader.Visibility == Visibility.Visible)
  216. {
  217. ItemSuitAcutalWidth(this.ActualWidth);
  218. }
  219. //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
  220. PulishEvent();
  221. //当前页面发生变化时通过ScrollChanged事件来刷新图片
  222. isFirstScrollChange = true;
  223. }
  224. }
  225. /// <summary>
  226. /// 鼠标滚轮滚动时触发的事件
  227. /// </summary>
  228. /// <param name="sender"></param>
  229. /// <param name="e"></param>
  230. public void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
  231. {
  232. if (isFirstScrollChange)
  233. {
  234. PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
  235. isFirstScrollChange = false;
  236. return;
  237. }
  238. else
  239. {
  240. if (e.VerticalChange != 0)
  241. timer?.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
  242. }
  243. }
  244. /// <summary>
  245. /// 拖动右侧滑块时触发的事件
  246. /// </summary>
  247. /// <param name="sender"></param>
  248. /// <param name="e"></param>
  249. private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
  250. {
  251. scrolltype = e.ScrollEventType;
  252. if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
  253. {
  254. timer?.Stop();
  255. return;
  256. }
  257. PulishEvent();
  258. }
  259. #endregion UI事件
  260. #region 方法
  261. /// <summary>
  262. /// 发布事件
  263. /// </summary>
  264. public void PulishEvent()
  265. {
  266. //判空 避免某些乱七八糟的情况下异常
  267. if (ListPageEdit != null & ListPageEdit.Items.Count > 0)
  268. {
  269. var itemSize = new Size((ListPageEdit.Items[0] as PageEditItem).ItemSize.Width + 32, (ListPageEdit.Items[0] as PageEditItem).ItemSize.Height + 30);
  270. var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5, 10, 5, 10));
  271. if ((bool)TbnTwoLine.IsChecked)
  272. {
  273. ///双列模式下要计算两列item
  274. range = new Tuple<int, int, int>(range.Item1, range.Item2 * 2, range.Item3);
  275. }
  276. eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
  277. }
  278. }
  279. /// <summary>
  280. /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
  281. /// 返回值为页面范围 从1开始
  282. /// </summary>
  283. /// <param name="view"></param>
  284. /// <param name="itemSize"></param>
  285. /// <param name="itemMargin"></param>
  286. /// <returns></returns>
  287. private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  288. {
  289. //var scrollViewer = GetScrollHost(view);
  290. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  291. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  292. return new Tuple<int, int, int>(0, 0, 0);
  293. try
  294. {
  295. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  296. var currentWidth = scrollViewer.ActualWidth;
  297. //计算当前窗口大小能显示的行数和列数
  298. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  299. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  300. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  301. preItemCount = preItemCount * columnCount;
  302. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  303. preEnd = preEnd * columnCount + columnCount - 1;
  304. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  305. return new Tuple<int, int, int>(
  306. Math.Max(preItemCount, 0),
  307. Math.Min(view.Items.Count, preEnd),
  308. middle);
  309. }
  310. catch { }
  311. return new Tuple<int, int, int>(0, 0, 0);
  312. }
  313. /// <summary>
  314. /// 获取listbox里的ScrollViewer对象
  315. /// 留意如果有重写ListBox对象,该方法可能无效
  316. /// </summary>
  317. /// <param name="listBox"></param>
  318. /// <returns></returns>
  319. private ScrollViewer GetScrollHost(ListBox listBox)
  320. {
  321. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  322. {
  323. int s = VisualTreeHelper.GetChildrenCount(listBox);
  324. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  325. if (border != null)
  326. {
  327. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  328. }
  329. }
  330. return null;
  331. }
  332. #endregion 方法
  333. /// <summary>
  334. /// 输入框显示时,自动获取焦点
  335. /// </summary>
  336. /// <param name="sender"></param>
  337. /// <param name="e"></param>
  338. private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  339. {
  340. if ((bool)e.NewValue)
  341. {
  342. (sender as Control).Focus();
  343. }
  344. }
  345. private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
  346. {
  347. try
  348. {
  349. if (e.LeftButton == MouseButtonState.Pressed && !isDragingEnter)
  350. {
  351. //鼠标框选逻辑
  352. if (startChoose)
  353. {
  354. var position = e.GetPosition(ListPageEdit);
  355. if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
  356. {
  357. startChoose = false;
  358. RectChoose.Visibility = Visibility.Collapsed;
  359. //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
  360. Mouse.Capture(null);
  361. return;
  362. }
  363. //矩形框内的item设为选中
  364. DoSelectItems();
  365. return;
  366. }
  367. //拖拽排序的逻辑
  368. else
  369. {
  370. var pos = e.GetPosition(ListPageEdit);
  371. if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
  372. {
  373. LineInset.Visibility = Visibility.Collapsed;
  374. ImgPicture.Visibility = Visibility.Collapsed;
  375. return;
  376. }
  377. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  378. if (result == null)
  379. {
  380. return;
  381. }
  382. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  383. if (listBoxItem == null)
  384. {
  385. return;
  386. }
  387. isDragingEnter = false;
  388. tempItem = listBoxItem.DataContext as PageEditItem;
  389. var item_pos = e.GetPosition(listBoxItem);
  390. if (item_pos != null)
  391. {
  392. item_x = item_pos.X;
  393. item_y = item_pos.Y;
  394. }
  395. //string filename = Guid.NewGuid().ToString() + ".pdf";
  396. //string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  397. //System.IO.File.Create(tempPath);
  398. //string[] files = new string[1];
  399. //files[0] = tempPath;
  400. var scroll = GetScrollHost(ListPageEdit);
  401. //页面 是App内的拖拽 ,还是拖拽 页面 出 App 是一起处理
  402. //将付费锁逻辑提前,避免拖拽出窗体成功
  403. if (!ServiceHelper.IAPBeforeFunction())
  404. {
  405. return;
  406. }
  407. #region 拖拽 页面 到 桌面/文件夹 ,提取文件
  408. //C:\Users\oyxh\AppData\Local\Temp\
  409. string dir = System.IO.Path.Combine(System.IO.Path.GetTempPath());
  410. if (!System.IO.Directory.Exists(dir))
  411. {
  412. System.IO.Directory.CreateDirectory(dir);
  413. }
  414. //string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  415. List<int> pages = GetSelectedItems();
  416. pages.Sort();
  417. string[] files = new string[1];
  418. if (pages.Count > 0)
  419. {
  420. //多选页面 提取成一个文件夹
  421. string filename = viewModel.PDFViewer.Document.FileName + " " + CommonHelper.GetPageParmFromList(pages) + ".pdf";
  422. string tempPath = System.IO.Path.Combine(dir, filename);
  423. //删除 临时文件的同名文件
  424. if (File.Exists(tempPath))
  425. {
  426. File.Delete(tempPath);
  427. }
  428. string path = CommonHelper.CreateFilePath(tempPath);
  429. files[0] = path;
  430. dropMoveFiles.Add(path);
  431. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  432. string range = String.Join(",", pages);
  433. bool result1 = savedoc.ImportPages(viewModel.PDFViewer.Document, range);
  434. //if (!result1)
  435. //{
  436. // viewModel.ShowToast();
  437. // viewModel.IsLoading = Visibility.Collapsed;
  438. // return;
  439. //}
  440. result1 = savedoc.WriteToFilePath(path);
  441. if (!result1)
  442. {
  443. viewModel.ShowToast();
  444. }
  445. savedoc.Release();//释放内存
  446. }
  447. #region 多选页面 分别生成单独文件
  448. //List<int> pagenums = new List<int>();
  449. //for (int i = 0; i < viewModel.PageEditItems.Count; i++)
  450. //{
  451. // if (viewModel.PageEditItems[i].Selected)
  452. // {
  453. // pagenums.Add(viewModel.PageEditItems[i].PageNumber);
  454. // }
  455. //}
  456. //string[] files = new string[pagenums.Count];
  457. //for (int i = 0; i < pagenums.Count; i++)
  458. //{
  459. // string filename = viewModel.PDFViewer.Document.FileName + " " + pagenums[i] + ".pdf";
  460. // string tempPath = System.IO.Path.Combine(dir, filename);
  461. // string path = CommonHelper.CreateFilePath(tempPath);
  462. // files[i] = path;
  463. // dropMoveFiles.Add(path);
  464. // CPDFDocument savedoc = CPDFDocument.CreateDocument();
  465. // bool result2 = savedoc.ImportPages(viewModel.PDFViewer.Document, pagenums[i].ToString());
  466. // if (!result2)
  467. // {
  468. // savedoc.Release();
  469. // continue;
  470. // }
  471. // result2 = savedoc.WriteToFilePath(path);
  472. // if (!result2)
  473. // {
  474. // savedoc.Release();
  475. // continue;
  476. // }
  477. // savedoc.Release();//释放内存
  478. //}
  479. #endregion 多选页面 分别生成单独文件
  480. //string[] files = new string[1];
  481. //files[0] = tempPath;
  482. DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
  483. #endregion 拖拽 页面 到 桌面/文件夹 ,提取文件
  484. //禁用从窗体拖拽到桌面的方法
  485. //DataObject dataObj = new DataObject(listBoxItem);
  486. //注意!!!目前是通过DragDropEffects类型来区分app内拖拽和外部文件拖入。否则页面文件拖拽页面到PDFViewer,也会新生成一个页签
  487. //如果这里的DragDropEfeects类型有改动,也需要调整ViewContent里UserControl_Drop函数的判断逻辑
  488. DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
  489. Mouse.Capture(ListPageEdit);
  490. //CleanDropMoveFiles();
  491. return;
  492. }
  493. }
  494. RectChoose.Visibility = Visibility.Collapsed;
  495. startChoose = false;
  496. LineInset.Visibility = Visibility.Collapsed;
  497. ImgPicture.Visibility = Visibility.Collapsed;
  498. Mouse.Capture(null);
  499. }
  500. catch
  501. {
  502. LineInset.Visibility = Visibility.Collapsed;
  503. ImgPicture.Visibility = Visibility.Collapsed;
  504. }
  505. }
  506. private List<int> GetSelectedItems()
  507. {
  508. List<int> pages = new List<int>();
  509. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  510. {
  511. //这样拿 更精准
  512. if (ListPageEdit.SelectedItems[i] is PageEditItem pageEditItem)
  513. {
  514. pages.Add(pageEditItem.PageNumber);
  515. }
  516. }
  517. return pages;
  518. }
  519. /// <summary>
  520. /// 判断是否开始框选、记录框选起始位置
  521. /// </summary>
  522. /// <param name="sender"></param>
  523. /// <param name="e"></param>
  524. private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  525. {
  526. var pos = e.GetPosition(ListPageEdit);
  527. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  528. if (result == null)
  529. {
  530. return;
  531. }
  532. //未选中item 并且不是点击滑轨时 开始框选
  533. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  534. var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
  535. if (listBoxItem == null)
  536. {
  537. if (scroller != null)
  538. {
  539. startChoose = false;
  540. return;
  541. }
  542. //点击空白处时开始框选
  543. startChoose = true;
  544. if (ListPageEdit.SelectedItems.Count > 0)
  545. {
  546. ListPageEdit.SelectedItems.Clear();
  547. }
  548. starPosition = e.GetPosition(ListPageEdit);
  549. starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
  550. Mouse.Capture(ListPageEdit);
  551. return;
  552. }
  553. //选中了item 时,不能框选
  554. startChoose = false;
  555. isDragingEnter = false;
  556. //更改系统的选中规则,选中状态下,鼠标松开后取消选中
  557. //方便实现多选拖拽功能
  558. if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
  559. {
  560. e.Handled = true;
  561. }
  562. }
  563. private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  564. {
  565. var pos = e.GetPosition(ListPageEdit);
  566. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  567. if (result == null)
  568. {
  569. return;
  570. }
  571. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  572. if (listBoxItem == null)
  573. {
  574. return;
  575. }
  576. //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
  577. //拖拽框选之后的抬起鼠标不进入处理
  578. if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
  579. {
  580. ListPageEdit.SelectedItems.Clear();
  581. ListPageEdit.SelectedItem = listBoxItem;
  582. listBoxItem.IsSelected = true;
  583. return;
  584. }
  585. Mouse.Capture(null);
  586. //结束鼠标框选
  587. startChoose = false;
  588. RectChoose.Visibility = Visibility.Collapsed;
  589. }
  590. /// <summary>
  591. /// 获取Listobox的Wrappanel容器
  592. /// </summary>
  593. /// <param name="listBox"></param>
  594. /// <returns></returns>
  595. public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
  596. {
  597. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  598. var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
  599. return panel;
  600. }
  601. /// <summary>
  602. ///根据鼠标拖选的框 选中矩形框里面的Item
  603. /// </summary>
  604. private void DoSelectItems()
  605. {
  606. var s = GetScrollHost(ListPageEdit);
  607. Point start = new Point();
  608. //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
  609. start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
  610. var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
  611. RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
  612. RectChoose.Width = rec.Width;
  613. RectChoose.Height = rec.Height;
  614. RectChoose.Visibility = Visibility.Visible;
  615. //检测遍历所有项,筛选在矩形框中的Item
  616. for (int i = 0; i < ListPageEdit.Items.Count; i++)
  617. {
  618. var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  619. //通过这一步来避免重复误选中
  620. var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
  621. if (parent == null)
  622. continue;
  623. var v = VisualTreeHelper.GetOffset(_item);
  624. if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
  625. {
  626. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
  627. }
  628. else
  629. {
  630. ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
  631. }
  632. }
  633. return;
  634. }
  635. /// <summary>
  636. /// 退出拖拽模式
  637. /// </summary>
  638. private void ExitDraging()
  639. {
  640. LineInset.Visibility = Visibility.Collapsed;
  641. ImgPicture.Visibility = Visibility.Collapsed;
  642. isDraging = false;
  643. }
  644. /// <summary>
  645. /// 拖拽释放后的处理逻辑
  646. /// </summary>
  647. /// <param name="sender"></param>
  648. /// <param name="e"></param>
  649. private async void ListPageEdit_Drop(object sender, DragEventArgs e)
  650. {
  651. //isDragingEnter = false;
  652. if (!isDraging)
  653. {
  654. //未拖拽时隐藏插入标记和虚影
  655. LineInset.Visibility = Visibility.Collapsed;
  656. ImgPicture.Visibility = Visibility.Collapsed;
  657. return;
  658. }
  659. //拖入非法格式释放时 返回
  660. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  661. if (file != null)
  662. {
  663. #region 从外部拖拽插入文件
  664. if (isDragingEnter)
  665. {
  666. //不能将付费锁 放到 grid_DragOver,会卡顿
  667. //将付费锁逻辑提前,避免因弹窗显示后导致虚影无法消失问题
  668. if (!ServiceHelper.IAPBeforeFunction())
  669. {
  670. LineInset.Visibility = Visibility.Collapsed;
  671. ImgPicture.Visibility = Visibility.Collapsed;
  672. isDragingEnter = false;
  673. return;
  674. }
  675. //注释从外部拖拽插入文件的功能
  676. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  677. Array.Reverse(files);
  678. foreach (string item in files)
  679. {
  680. System.IO.FileInfo info = new System.IO.FileInfo(item);
  681. string fileName = viewModel.GetInsertFromFile(item);
  682. //if (System.IO.Path.GetExtension(item).ToLower() == ".pdf" && info.Length > 0)
  683. //{
  684. // int index = InsertIndex == -1 ? 0 : InsertIndex;
  685. // await viewModel.InsertFromFile(index, item);
  686. // viewModel.ReloadAfterOption(true, true, new Tuple<int, int>(0, ListPageEdit.Items.Count));
  687. //}
  688. if (info.Length > 0 && File.Exists(fileName))
  689. {
  690. int index = InsertIndex == -1 ? 0 : InsertIndex;
  691. bool flag = await viewModel.InsertFromFile(index, fileName);
  692. if (flag)
  693. {
  694. viewModel.ReloadAfterOption(true, true, new Tuple<int, int>(0, ListPageEdit.Items.Count));
  695. }
  696. }
  697. }
  698. //其他文件 则新增一个页签打开
  699. // DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
  700. LineInset.Visibility = Visibility.Collapsed;
  701. ImgPicture.Visibility = Visibility.Collapsed;
  702. isDragingEnter = false;
  703. return;
  704. }
  705. #endregion 从外部拖拽插入文件
  706. //return;
  707. }
  708. var pos = e.GetPosition(ListPageEdit);
  709. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  710. if (result == null)
  711. {
  712. //超出当前可控区域
  713. ExitDraging();
  714. return;
  715. }
  716. //查找元数据
  717. //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
  718. if (tempItem == null)
  719. {
  720. ExitDraging();
  721. return;
  722. }
  723. //查找目标数据
  724. int targetindex = 0;//目标插入位置
  725. if (InsertIndex != -1)
  726. {
  727. //往前移动时 此index 不是准确的,需要处理++
  728. targetindex = InsertIndex;
  729. }
  730. //else//基本不会命中 仅作为保险措施
  731. //{
  732. // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  733. // if (listBoxItem == null)
  734. // {
  735. // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
  736. // ExitDraging();
  737. // return;
  738. // }
  739. // var targetPerson = listBoxItem;
  740. // targetPerson.Opacity = 1;
  741. // sourcePerson.Opacity = 1;
  742. // if (ReferenceEquals(targetPerson, sourcePerson))
  743. // {
  744. // ExitDraging();
  745. // return;
  746. // }
  747. // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
  748. //}
  749. List<ListBoxItem> list = new List<ListBoxItem>();
  750. List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
  751. //开始排序
  752. List<int> pages = new List<int>();
  753. //要先对所有选中项 根据页码排序
  754. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  755. {
  756. var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
  757. pages.Add(pageindex);//存入的为页码索引值
  758. }
  759. if (pages.Count <= 0)
  760. {
  761. ExitDraging();
  762. return;
  763. }
  764. viewModel.DragToSort(targetindex, pages);
  765. ExitDraging();
  766. isDraging = false;
  767. CleanDropMoveFiles();
  768. }
  769. /// <summary>
  770. /// 拖拽完成后,清空临时目录 的文件
  771. /// </summary>
  772. private void CleanDropMoveFiles()
  773. {
  774. if (dropMoveFiles != null)
  775. {
  776. if (dropMoveFiles.Count > 0)
  777. {
  778. for (int i = 0; i < dropMoveFiles.Count; i++)
  779. {
  780. if (File.Exists(dropMoveFiles[i]))
  781. {
  782. File.Delete(dropMoveFiles[i]);
  783. }
  784. }
  785. dropMoveFiles.Clear();
  786. }
  787. }
  788. }
  789. private void MidLane_Drop(object sender, DragEventArgs e)
  790. {
  791. ListPageEdit_Drop(sender, e);
  792. }
  793. private void ImgPicture_Drop(object sender, DragEventArgs e)
  794. {
  795. ListPageEdit_Drop(sender, e);
  796. }
  797. /// <summary>
  798. /// 拖拽事件写在外部的Grid里,会更加流畅
  799. /// </summary>
  800. /// <param name="sender"></param>
  801. /// <param name="e"></param>
  802. private void Grid_DragOver(object sender, DragEventArgs e)
  803. {
  804. try
  805. {
  806. Trace.WriteLine("Grid_DragOver 文件拖出App");
  807. //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
  808. if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
  809. return;
  810. //从外部拖入文件时 返回
  811. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  812. if (file != null)
  813. {
  814. }
  815. //滚动后有 位置不准确 要减去滚动偏移量
  816. //控制线的位置
  817. var pos = e.GetPosition(ListPageEdit);
  818. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  819. if (result == null)
  820. {
  821. return;
  822. //MidLane.Visibility = Visibility.Collapsed;
  823. //return;
  824. }
  825. //获取当前鼠标指针下的容器
  826. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  827. if (listBoxItem == null)
  828. {
  829. //页面 拖出App后存在极少数情况会显示 标记线、缩略图,注释掉=》item中间不会有虚影,观感不好
  830. //LineInset.Visibility = Visibility.Collapsed;
  831. //ImgPicture.Visibility = Visibility.Collapsed;
  832. //MidLane.Visibility = Visibility.Collapsed;
  833. //return;
  834. }
  835. #region 计算虚影位置
  836. //xaml层 要设置 虚影控件为左上
  837. double xPos = 0;
  838. double yPos = 0;
  839. //内部拖动
  840. if (!isDragingEnter)
  841. {
  842. if (tempItem != null)
  843. {
  844. ImgPicture.Width = tempItem.ItemSize.Width;
  845. ImgPicture.Height = tempItem.ItemSize.Height;
  846. ImgPicture.Source = tempItem.Image;
  847. xPos = e.GetPosition(ListPageEdit).X - item_x;
  848. yPos = e.GetPosition(ListPageEdit).Y - item_y;
  849. }
  850. }
  851. else
  852. {
  853. DragDropHelper.DragOver(this, e);
  854. //从外部拖入的逻辑
  855. //var pic = ToBitmapSource(dragingEnterPath);
  856. //ShadowPicture.Width = pic.Width;
  857. //ShadowPicture.Height = pic.Height;
  858. //ShadowPicture.Source = pic;
  859. //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
  860. //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
  861. }
  862. ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
  863. #endregion 计算虚影位置
  864. #region 计算插入标记位置
  865. var scroll = GetScrollHost(ListPageEdit);
  866. if (listBoxItem != null)
  867. {
  868. //虚拟化影响到该值计算
  869. var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
  870. LineInset.Visibility = Visibility.Visible;
  871. if (!isDragingEnter)
  872. {
  873. ImgPicture.Visibility = Visibility.Visible;
  874. }
  875. else
  876. {
  877. ImgPicture.Visibility = Visibility.Collapsed;
  878. }
  879. var panel = GetWrapPanel(ListPageEdit);
  880. //var item = panel.ItemSize.Width;
  881. //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
  882. var item = listBoxItem.DesiredSize.Width;
  883. int count = (int)(panel.ViewportWidth / item);
  884. var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
  885. LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
  886. if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
  887. {
  888. LineInset.X2 = LineInset.X1 = p.X - gap / 2;
  889. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
  890. }
  891. else
  892. {
  893. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
  894. }
  895. Console.WriteLine("InsertIndex:{0}\r\n", InsertIndex);
  896. //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
  897. LineInset.Y1 = p.Y;
  898. if (LineInset.Y1 < 0)//避免超出上边界
  899. {
  900. LineInset.Y1 = 0;
  901. }
  902. //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
  903. LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
  904. if (LineInset.Y2 < 0)
  905. {
  906. LineInset.Y2 = 0;
  907. }
  908. }
  909. #endregion 计算插入标记位置
  910. //暂时处理 鼠标移出边框时,虚影的显示问题
  911. //230612 拖拽逻辑,为了更好的效果体现,暂时注释
  912. if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
  913. {
  914. LineInset.Visibility = Visibility.Collapsed;
  915. ImgPicture.Visibility = Visibility.Collapsed;
  916. }
  917. if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
  918. {
  919. LineInset.Visibility = Visibility.Collapsed;
  920. ImgPicture.Visibility = Visibility.Collapsed;
  921. }
  922. #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  923. speed = 0;
  924. if (pos.Y >= ListPageEdit.ActualHeight - 30)
  925. {
  926. speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
  927. }
  928. else if (pos.Y <= 30)
  929. {
  930. speed = (int)(pos.Y - 30);
  931. }
  932. var v = scroll.VerticalOffset;
  933. scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
  934. #endregion 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  935. }
  936. catch
  937. {
  938. }
  939. }
  940. /// <summary>
  941. /// 右键不会选中页面,不要跳转
  942. /// </summary>
  943. /// <param name="sender"></param>
  944. /// <param name="e"></param>
  945. private void ListBoxItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  946. {
  947. //viewModel.IsAddPages = false;
  948. viewModel.IsMouseRightButtonDown = true;
  949. viewModel.RightSelectedIndex = -1;
  950. viewModel.RightSelectedIndexs.Clear();
  951. var item = (sender as ListBoxItem).DataContext as PageEditItem;
  952. if (item != null)
  953. {
  954. viewModel.RightSelectedIndex = item.PageNumber - 1;
  955. if (ListPageEdit.SelectedItems.Count > 1)
  956. {
  957. viewModel.IsMultiSelected = true;
  958. viewModel.maxSelectedIndex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[ListPageEdit.SelectedItems.Count - 1]);
  959. foreach (var page in ListPageEdit.SelectedItems)
  960. {
  961. if (page is PageEditItem pageEdit)
  962. {
  963. viewModel.RightSelectedIndexs.Add(pageEdit.PageNumber - 1);
  964. }
  965. }
  966. }
  967. else
  968. {
  969. if (viewModel.RightSelectedIndex != viewModel.ListSelectedIndex)
  970. {
  971. viewModel.ListSelectedIndex = -1;
  972. }
  973. viewModel.IsMultiSelected = false;
  974. viewModel.maxSelectedIndex = -1;
  975. }
  976. }
  977. //拦截响应选中事件
  978. e.Handled = true;
  979. }
  980. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  981. {
  982. //viewModel.IsAddPages = false;
  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. }