PageEditContent.xaml.cs 65 KB

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