PageEditContent.xaml.cs 59 KB

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