PageEditContent.xaml.cs 57 KB

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