PageEditContent.xaml.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. using PDF_Master.EventAggregators;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model.PageEdit;
  4. using PDF_Master.ViewModels.PageEdit;
  5. using Prism.Events;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Controls.Primitives;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using System.Windows.Threading;
  22. using WpfToolkit.Controls;
  23. namespace PDF_Master.Views.PageEdit
  24. {
  25. /// <summary>
  26. /// PageEditContent.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class PageEditContent : UserControl
  29. {
  30. /// <summary>
  31. /// 用于通知刷新图片的事件
  32. /// 因为目前的图片刷新策略依赖很多UI事件判断,因为将主要判断逻辑放在UI层,VM负责图片刷新,非必要情况尽量不要采用这种形式
  33. /// </summary>
  34. private IEventAggregator eventor;
  35. /// <summary>
  36. /// 暂存的唯一索引值,用于区分多页签
  37. /// </summary>
  38. private string unicode;
  39. /// <summary>
  40. /// 是否是加载时第一次触发滚动
  41. /// </summary>
  42. private bool isFirstScrollChange = true;
  43. /// <summary>
  44. /// 用于判断滚轮停止的计时器
  45. /// </summary>
  46. private DispatcherTimer timer = new DispatcherTimer();
  47. /// <summary>
  48. /// 判断是否开始框选
  49. /// </summary>
  50. private bool startChoose = false;
  51. /// <summary>
  52. /// 框选的起始位置
  53. /// </summary>
  54. private Point starPosition = new Point();
  55. /// <summary>
  56. /// 记录当前滑块的状态
  57. /// </summary>
  58. private ScrollEventType scrolltype = ScrollEventType.EndScroll;
  59. //鼠标点击时在item中的位置 实现类似点哪拖哪的细节
  60. private double item_x;
  61. private double item_y;
  62. //插入标记代表的插入位置
  63. private int InsertIndex = -1;
  64. //拖动的Item
  65. private PageEditItem tempItem;
  66. /// <summary>
  67. /// 是否正在拖拽排序中,通过该变量避免单击触发拖动
  68. /// </summary>
  69. private bool isDraging = false;
  70. //是否正在从外部拖入文件
  71. public bool isDragingEnter { get; set; } = false;
  72. /// <summary>
  73. /// 是否需要自动滚动
  74. /// </summary>
  75. private bool needScroll = false;
  76. //自动滚动速度
  77. private int speed = 0;
  78. private PageEditContentViewModel viewModel;
  79. public PageEditContent()
  80. {
  81. InitializeComponent();
  82. }
  83. private void PageEditItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  84. {
  85. //BOTA缩略图里 插入子项时,刷新子项大小
  86. if (GridBOTAHeader.Visibility == Visibility.Visible&&e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
  87. {
  88. ItemSuitAcutalWidth(this.ActualWidth);
  89. }
  90. //页面总数变化后用于通知BOTA面板进行数据集刷新
  91. eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode,NotifyType.PageCountChanged));
  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. private void CleanSelectAll(CleanSelectAllArgs obj)
  107. {
  108. ListPageEdit.SelectedItems.Clear();
  109. ListPageEdit.SelectedIndex = viewModel.PDFViewer.CurrentIndex;
  110. }
  111. private void Timer_Tick(object sender, EventArgs e)
  112. {
  113. PulishEvent();
  114. timer?.Stop();
  115. }
  116. private void OneNotifyEvent(PageEditNotifyEventArgs e)
  117. {
  118. switch (e.Type)
  119. {
  120. case NotifyType.RefreshPage:
  121. PulishEvent();
  122. break;
  123. case NotifyType.SelectItems:
  124. if (e.PageRange.Count == 1)
  125. {
  126. ListPageEdit.SelectedIndex = e.PageRange[0] - 1;
  127. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  128. }
  129. else
  130. {
  131. ListPageEdit.SelectedItems.Clear();
  132. for (int i = 0; i < e.PageRange.Count; i++)
  133. {
  134. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[e.PageRange[i] - 1]);
  135. }
  136. }
  137. break;
  138. }
  139. }
  140. #region UI事件
  141. /// <summary>
  142. /// 每次显示的时候就触发事件,刷新所有图片
  143. /// </summary>
  144. /// <param name="sender"></param>
  145. /// <param name="e"></param>
  146. private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  147. {
  148. if ((bool)e.NewValue)
  149. {
  150. //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
  151. PulishEvent();
  152. //当前页面发生变化时通过ScrollChanged事件来刷新图片
  153. isFirstScrollChange = true;
  154. if (GridBOTAHeader.Visibility == Visibility.Visible)
  155. {
  156. ItemSuitAcutalWidth(this.ActualWidth);
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// 初始化listbox滚动条范围
  162. /// </summary>
  163. private int _scrollbarMaximum = -1;
  164. /// <summary>
  165. /// 查找组成控件
  166. /// </summary>
  167. /// <param name="element"></param>
  168. /// <param name="type"></param>
  169. /// <returns></returns>
  170. private DependencyObject GetDescendantByType(DependencyObject element, Type type)
  171. {
  172. if (element.GetType() == type) return element;
  173. DependencyObject foundElement = null;
  174. int childrenCount = VisualTreeHelper.GetChildrenCount(element);
  175. for (int i = 0; i < childrenCount; i++)
  176. {
  177. var child = VisualTreeHelper.GetChild(element, i);
  178. if (child.GetType() == type)
  179. {
  180. foundElement = child;
  181. break;
  182. }
  183. foundElement = GetDescendantByType(child, type);
  184. if (foundElement != null)
  185. break;
  186. }
  187. return foundElement;
  188. }
  189. /// <summary>
  190. /// 滚动条范围发生变化触发事件
  191. /// </summary>
  192. /// <param name="sender"></param>
  193. /// <param name="e"></param>
  194. private void OnLayoutUpdated(object sender, EventArgs e)
  195. {
  196. var scrollViewer = GetDescendantByType(ListPageEdit, typeof(ScrollViewer)) as ScrollViewer;
  197. if (scrollViewer != null && scrollViewer.ScrollableHeight != _scrollbarMaximum)
  198. {
  199. PulishEvent();
  200. _scrollbarMaximum = (int)scrollViewer.ScrollableHeight;
  201. }
  202. }
  203. /// <summary>
  204. /// 鼠标滚轮滚动时触发的事件
  205. /// </summary>
  206. /// <param name="sender"></param>
  207. /// <param name="e"></param>
  208. public void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
  209. {
  210. if (isFirstScrollChange)
  211. {
  212. PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
  213. isFirstScrollChange = false;
  214. return;
  215. }
  216. else
  217. {
  218. if (e.VerticalChange != 0)
  219. timer?.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
  220. }
  221. }
  222. /// <summary>
  223. /// 拖动右侧滑块时触发的事件
  224. /// </summary>
  225. /// <param name="sender"></param>
  226. /// <param name="e"></param>
  227. private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
  228. {
  229. scrolltype = e.ScrollEventType;
  230. if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
  231. {
  232. timer?.Stop();
  233. return;
  234. }
  235. PulishEvent();
  236. }
  237. #endregion
  238. #region 方法
  239. /// <summary>
  240. /// 发布事件
  241. /// </summary>
  242. public void PulishEvent()
  243. {
  244. //判空 避免某些乱七八糟的情况下异常
  245. if (ListPageEdit != null & ListPageEdit.Items.Count > 0)
  246. {
  247. var itemSize = new Size((ListPageEdit.Items[0] as PageEditItem).ItemSize.Width + 32, (ListPageEdit.Items[0] as PageEditItem).ItemSize.Height + 30);
  248. var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5, 10, 5, 10));
  249. if ((bool)TbnTwoLine.IsChecked)
  250. {
  251. ///双列模式下要计算两列item
  252. range = new Tuple<int, int, int>(range.Item1, range.Item2 * 2, range.Item3);
  253. }
  254. eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
  255. }
  256. }
  257. /// <summary>
  258. /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
  259. /// 返回值为页面范围 从1开始
  260. /// </summary>
  261. /// <param name="view"></param>
  262. /// <param name="itemSize"></param>
  263. /// <param name="itemMargin"></param>
  264. /// <returns></returns>
  265. private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  266. {
  267. //var scrollViewer = GetScrollHost(view);
  268. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  269. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  270. return new Tuple<int, int, int>(0, 0, 0);
  271. try
  272. {
  273. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  274. var currentWidth = scrollViewer.ActualWidth;
  275. //计算当前窗口大小能显示的行数和列数
  276. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  277. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  278. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  279. preItemCount = preItemCount * columnCount;
  280. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  281. preEnd = preEnd * columnCount + columnCount - 1;
  282. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  283. return new Tuple<int, int, int>(
  284. Math.Max(preItemCount, 0),
  285. Math.Min(view.Items.Count, preEnd),
  286. middle);
  287. }
  288. catch { }
  289. return new Tuple<int, int, int>(0, 0, 0);
  290. }
  291. /// <summary>
  292. /// 获取listbox里的ScrollViewer对象
  293. /// 留意如果有重写ListBox对象,该方法可能无效
  294. /// </summary>
  295. /// <param name="listBox"></param>
  296. /// <returns></returns>
  297. private ScrollViewer GetScrollHost(ListBox listBox)
  298. {
  299. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  300. {
  301. int s = VisualTreeHelper.GetChildrenCount(listBox);
  302. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  303. if (border != null)
  304. {
  305. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  306. }
  307. }
  308. return null;
  309. }
  310. #endregion
  311. /// <summary>
  312. /// 输入框显示时,自动获取焦点
  313. /// </summary>
  314. /// <param name="sender"></param>
  315. /// <param name="e"></param>
  316. private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  317. {
  318. if ((bool)e.NewValue)
  319. {
  320. (sender as Control).Focus();
  321. }
  322. }
  323. private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
  324. {
  325. try
  326. {
  327. if (e.LeftButton == MouseButtonState.Pressed&&!isDragingEnter)
  328. {
  329. //鼠标框选逻辑
  330. if (startChoose)
  331. {
  332. var position = e.GetPosition(ListPageEdit);
  333. if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
  334. {
  335. startChoose = false;
  336. RectChoose.Visibility = Visibility.Collapsed;
  337. //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
  338. Mouse.Capture(null);
  339. return;
  340. }
  341. //矩形框内的item设为选中
  342. DoSelectItems();
  343. return;
  344. }
  345. //拖拽排序的逻辑
  346. var pos = e.GetPosition(ListPageEdit);
  347. if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
  348. {
  349. LineInset.Visibility = Visibility.Collapsed;
  350. ImgPicture.Visibility = Visibility.Collapsed;
  351. return;
  352. }
  353. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  354. if (result == null)
  355. {
  356. return;
  357. }
  358. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  359. if (listBoxItem == null)
  360. {
  361. return;
  362. }
  363. isDragingEnter = false;
  364. tempItem = listBoxItem.DataContext as PageEditItem;
  365. var item_pos = e.GetPosition(listBoxItem);
  366. if (item_pos != null)
  367. {
  368. item_x = item_pos.X;
  369. item_y = item_pos.Y;
  370. }
  371. var scroll = GetScrollHost(ListPageEdit);
  372. string filename = Guid.NewGuid().ToString() + ".pdf";
  373. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  374. System.IO.File.Create(tempPath);
  375. string[] files = new string[1];
  376. files[0] = tempPath;
  377. //禁用从窗体拖拽到桌面的方法
  378. //DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
  379. DataObject dataObj = new DataObject(listBoxItem);
  380. DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
  381. Mouse.Capture(ListPageEdit);
  382. return;
  383. }
  384. RectChoose.Visibility = Visibility.Collapsed;
  385. startChoose = false;
  386. Mouse.Capture(null);
  387. }
  388. catch
  389. {
  390. LineInset.Visibility = Visibility.Collapsed;
  391. ImgPicture.Visibility = Visibility.Collapsed;
  392. }
  393. }
  394. /// <summary>
  395. /// 判断是否开始框选、记录框选起始位置
  396. /// </summary>
  397. /// <param name="sender"></param>
  398. /// <param name="e"></param>
  399. private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  400. {
  401. var pos = e.GetPosition(ListPageEdit);
  402. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  403. if (result == null)
  404. {
  405. return;
  406. }
  407. //未选中item 并且不是点击滑轨时 开始框选
  408. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  409. var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
  410. if (listBoxItem == null)
  411. {
  412. if (scroller != null)
  413. {
  414. startChoose = false;
  415. return;
  416. }
  417. //点击空白处时开始框选
  418. startChoose = true;
  419. if (ListPageEdit.SelectedItems.Count > 0)
  420. {
  421. ListPageEdit.SelectedItems.Clear();
  422. }
  423. starPosition = e.GetPosition(ListPageEdit);
  424. starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
  425. Mouse.Capture(ListPageEdit);
  426. return;
  427. }
  428. //选中了item 时,不能框选
  429. startChoose = false;
  430. //更改系统的选中规则,选中状态下,鼠标松开后取消选中
  431. //方便实现多选拖拽功能
  432. if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
  433. {
  434. e.Handled = true;
  435. }
  436. }
  437. private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  438. {
  439. var pos = e.GetPosition(ListPageEdit);
  440. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  441. if (result == null)
  442. {
  443. return;
  444. }
  445. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  446. if (listBoxItem == null)
  447. {
  448. return;
  449. }
  450. //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
  451. //拖拽框选之后的抬起鼠标不进入处理
  452. if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
  453. {
  454. ListPageEdit.SelectedItems.Clear();
  455. ListPageEdit.SelectedItem = listBoxItem;
  456. listBoxItem.IsSelected = true;
  457. return;
  458. }
  459. Mouse.Capture(null);
  460. //结束鼠标框选
  461. startChoose = false;
  462. RectChoose.Visibility = Visibility.Collapsed;
  463. }
  464. /// <summary>
  465. /// 获取Listobox的Wrappanel容器
  466. /// </summary>
  467. /// <param name="listBox"></param>
  468. /// <returns></returns>
  469. public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
  470. {
  471. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  472. var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
  473. return panel;
  474. }
  475. /// <summary>
  476. ///根据鼠标拖选的框 选中矩形框里面的Item
  477. /// </summary>
  478. private void DoSelectItems()
  479. {
  480. var s = GetScrollHost(ListPageEdit);
  481. Point start = new Point();
  482. //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
  483. start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
  484. var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
  485. RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
  486. RectChoose.Width = rec.Width;
  487. RectChoose.Height = rec.Height;
  488. RectChoose.Visibility = Visibility.Visible;
  489. //检测遍历所有项,筛选在矩形框中的Item
  490. for (int i = 0; i < ListPageEdit.Items.Count; i++)
  491. {
  492. var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  493. //通过这一步来避免重复误选中
  494. var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
  495. if (parent == null)
  496. continue;
  497. var v = VisualTreeHelper.GetOffset(_item);
  498. if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
  499. {
  500. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
  501. }
  502. else
  503. {
  504. ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
  505. }
  506. }
  507. return;
  508. }
  509. /// <summary>
  510. /// 退出拖拽模式
  511. /// </summary>
  512. private void ExitDraging()
  513. {
  514. LineInset.Visibility = Visibility.Collapsed;
  515. ImgPicture.Visibility = Visibility.Collapsed;
  516. isDraging = false;
  517. }
  518. /// <summary>
  519. /// 拖拽释放后的处理逻辑
  520. /// </summary>
  521. /// <param name="sender"></param>
  522. /// <param name="e"></param>
  523. private async void ListPageEdit_Drop(object sender, DragEventArgs e)
  524. {
  525. needScroll = false;
  526. if (!isDraging)
  527. {
  528. //未拖拽时隐藏插入标记和虚影
  529. LineInset.Visibility = Visibility.Collapsed;
  530. ImgPicture.Visibility = Visibility.Collapsed;
  531. return;
  532. }
  533. //拖入非法格式释放时 返回
  534. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  535. if (file != null)
  536. {
  537. return;
  538. }
  539. #region 功能付费锁
  540. // //if (!App.IsActive())
  541. // //{
  542. // // MidLane.Visibility = Visibility.Collapsed;
  543. // // IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
  544. // // dialog.ShowDialog();
  545. // // return;
  546. // //}
  547. #endregion
  548. #region 从外部拖拽插入文件
  549. if (isDragingEnter)
  550. {
  551. //注释从外部拖拽插入文件的功能
  552. //var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  553. //Array.Reverse(files);
  554. //foreach(string file in files)
  555. //{
  556. // System.IO.FileInfo info = new System.IO.FileInfo(file);
  557. // if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
  558. // {
  559. // int index = InsertIndex == -1 ? 0 : InsertIndex;
  560. // viewModel.InsertFromFile(index, file);
  561. // viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
  562. // }
  563. //}
  564. ////其他文件 则新增一个页签打开
  565. //// DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
  566. //LineInset.Visibility = Visibility.Collapsed;
  567. //ImgPicture.Visibility = Visibility.Collapsed;
  568. //isDragingEnter = false;
  569. return;
  570. }
  571. #endregion
  572. var pos = e.GetPosition(ListPageEdit);
  573. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  574. //if (result == null)
  575. //{
  576. // //超出当前可控区域
  577. // ExitDraging();
  578. // return;
  579. //}
  580. ////查找元数据
  581. //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
  582. //if (sourcePerson == null)
  583. //{
  584. // ExitDraging();
  585. // return;
  586. //}
  587. //查找目标数据
  588. int targetindex = 0;//目标插入位置
  589. if (InsertIndex != -1)
  590. {
  591. //往前移动时 此index 不是准确的,需要处理++
  592. targetindex = InsertIndex;
  593. }
  594. //else//基本不会命中 仅作为保险措施
  595. //{
  596. // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  597. // if (listBoxItem == null)
  598. // {
  599. // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
  600. // ExitDraging();
  601. // return;
  602. // }
  603. // var targetPerson = listBoxItem;
  604. // targetPerson.Opacity = 1;
  605. // sourcePerson.Opacity = 1;
  606. // if (ReferenceEquals(targetPerson, sourcePerson))
  607. // {
  608. // ExitDraging();
  609. // return;
  610. // }
  611. // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
  612. //}
  613. List<ListBoxItem> list = new List<ListBoxItem>();
  614. List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
  615. //开始排序
  616. List<int> pages = new List<int>();
  617. //要先对所有选中项 根据页码排序
  618. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  619. {
  620. var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
  621. pages.Add(pageindex);//存入的为页码索引值
  622. }
  623. if (pages.Count <= 0)
  624. {
  625. ExitDraging();
  626. return;
  627. }
  628. viewModel.DragToSort(targetindex,pages);
  629. ExitDraging();
  630. isDraging = false;
  631. }
  632. private void MidLane_Drop(object sender, DragEventArgs e)
  633. {
  634. ListPageEdit_Drop(sender, e);
  635. }
  636. private void ImgPicture_Drop(object sender, DragEventArgs e)
  637. {
  638. ListPageEdit_Drop(sender, e);
  639. }
  640. /// <summary>
  641. /// 拖拽事件写在外部的Grid里,会更加流畅
  642. /// </summary>
  643. /// <param name="sender"></param>
  644. /// <param name="e"></param>
  645. private void Grid_DragOver(object sender, DragEventArgs e)
  646. {
  647. try
  648. {
  649. //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
  650. if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
  651. return;
  652. //从外部拖入文件时 返回
  653. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  654. if(file!=null)
  655. {
  656. return;
  657. }
  658. //滚动后有 位置不准确 要减去滚动偏移量
  659. //控制线的位置
  660. var pos = e.GetPosition(ListPageEdit);
  661. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  662. if (result == null)
  663. {
  664. return;
  665. //MidLane.Visibility = Visibility.Collapsed;
  666. //return;
  667. }
  668. //获取当前鼠标指针下的容器
  669. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  670. if (listBoxItem == null)
  671. {
  672. //MidLane.Visibility = Visibility.Collapsed;
  673. //return;
  674. }
  675. #region 计算虚影位置
  676. //xaml层 要设置 虚影控件为左上
  677. double xPos = 0;
  678. double yPos = 0;
  679. //内部拖动
  680. if (!isDragingEnter)
  681. {
  682. if(tempItem!=null)
  683. {
  684. ImgPicture.Width = tempItem.ItemSize.Width;
  685. ImgPicture.Height = tempItem.ItemSize.Height;
  686. ImgPicture.Source = tempItem.Image;
  687. xPos = e.GetPosition(ListPageEdit).X - item_x;
  688. yPos = e.GetPosition(ListPageEdit).Y - item_y;
  689. }
  690. }
  691. else
  692. {
  693. DragDropHelper.DragOver(this, e);
  694. //从外部拖入的逻辑
  695. //var pic = ToBitmapSource(dragingEnterPath);
  696. //ShadowPicture.Width = pic.Width;
  697. //ShadowPicture.Height = pic.Height;
  698. //ShadowPicture.Source = pic;
  699. //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
  700. //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
  701. }
  702. ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
  703. #endregion
  704. #region 计算插入标记位置
  705. var scroll = GetScrollHost(ListPageEdit);
  706. if (listBoxItem != null)
  707. {
  708. //虚拟化影响到该值计算
  709. var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
  710. LineInset.Visibility = Visibility.Visible;
  711. if(!isDragingEnter)
  712. {
  713. ImgPicture.Visibility = Visibility.Visible;
  714. }
  715. else
  716. {
  717. ImgPicture.Visibility = Visibility.Collapsed;
  718. }
  719. var panel = GetWrapPanel(ListPageEdit);
  720. //var item = panel.ItemSize.Width;
  721. //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
  722. var item = listBoxItem.DesiredSize.Width;
  723. int count = (int)(panel.ViewportWidth / item);
  724. var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
  725. LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
  726. if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
  727. {
  728. LineInset.X2 = LineInset.X1 = p.X - gap / 2;
  729. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
  730. }
  731. else
  732. {
  733. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
  734. }
  735. Console.WriteLine("InsertIndex:{0}\r\n",InsertIndex);
  736. //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
  737. LineInset.Y1 = p.Y;
  738. if (LineInset.Y1 < 0)//避免超出上边界
  739. {
  740. LineInset.Y1 = 0;
  741. }
  742. //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
  743. LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
  744. if (LineInset.Y2 < 0)
  745. {
  746. LineInset.Y2 = 0;
  747. }
  748. }
  749. #endregion
  750. //暂时处理 鼠标移出边框时,虚影的显示问题
  751. if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
  752. {
  753. LineInset.Visibility = Visibility.Collapsed;
  754. ImgPicture.Visibility = Visibility.Collapsed;
  755. needScroll = false;
  756. }
  757. if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
  758. {
  759. LineInset.Visibility = Visibility.Collapsed;
  760. ImgPicture.Visibility = Visibility.Collapsed;
  761. needScroll = false;
  762. }
  763. #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  764. speed = 0;
  765. if (pos.Y >= ListPageEdit.ActualHeight - 30)
  766. {
  767. speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
  768. needScroll = true;
  769. }
  770. else if (pos.Y <= 30)
  771. {
  772. speed = (int)(pos.Y - 30);
  773. needScroll = true;
  774. }
  775. else
  776. needScroll = false;
  777. var v = scroll.VerticalOffset;
  778. scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
  779. #endregion
  780. }
  781. catch
  782. {
  783. }
  784. }
  785. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  786. {
  787. if(GridBOTAHeader.Visibility == Visibility.Visible&&viewModel.PDFViewer!=null)
  788. {
  789. if(Keyboard.Modifiers == ModifierKeys.Shift||Keyboard.Modifiers== ModifierKeys.Control)
  790. {
  791. return;
  792. }
  793. var item = (sender as ListBoxItem).DataContext as PageEditItem;
  794. if(item != null)
  795. {
  796. viewModel.PDFViewer.GoToPage(item.PageNumber-1);
  797. }
  798. }
  799. }
  800. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  801. {
  802. if (e.ChangedButton == MouseButton.Left)
  803. {
  804. //双击回到PDFViewer界面
  805. var item = sender as ListBoxItem;
  806. if (item != null)
  807. {
  808. var index = ListPageEdit.ItemContainerGenerator.IndexFromContainer(item);
  809. //双击事件无法绑定Command 暂时采用这种方式调用VM里的方法
  810. viewModel.BackToPDFViewer(index);
  811. }
  812. }
  813. }
  814. private void PageEdit_SizeChanged(object sender, SizeChangedEventArgs e)
  815. {
  816. //BOTA缩略图模式下需要调整Item宽度
  817. if (GridBOTAHeader.Visibility == Visibility.Visible)
  818. {
  819. ItemSuitAcutalWidth(e.NewSize.Width);
  820. }
  821. if(e.NewSize.Width>0)
  822. {
  823. //宽度变化后刷新一次视图 修复重复点击缩略图后图片不显示的问题
  824. PulishEvent();
  825. }
  826. }
  827. private void ItemSuitAcutalWidth(double width)
  828. {
  829. //缩略图模式下,保持一列或者两列显示
  830. if ((bool)TbnTwoLine.IsChecked)
  831. {
  832. var itemwidth = (width - 140) / 2;
  833. if(itemwidth<0)
  834. {
  835. itemwidth = width / 4;
  836. }
  837. var itemHeight = 294 * itemwidth / 208.0;
  838. viewModel.ChangeItemSize(new Size(itemwidth, itemHeight));
  839. }
  840. else
  841. {
  842. var itemwidth = 0.6 * width;
  843. var itemheight = 294 * itemwidth / 208.0;
  844. viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
  845. }
  846. }
  847. private void TbnTwoLine_Click(object sender, RoutedEventArgs e)
  848. {
  849. ItemSuitAcutalWidth(this.ActualWidth);
  850. PulishEvent();
  851. }
  852. /// <summary>
  853. /// 用来判断是否有外界拖入的事件
  854. /// </summary>
  855. /// <param name="sender"></param>
  856. /// <param name="e"></param>
  857. private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
  858. {
  859. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  860. if (file == null)//为null 表示内部拖动 触发的
  861. {
  862. return;
  863. }
  864. isDragingEnter = false;
  865. foreach (string f in file)
  866. {
  867. System.IO.FileInfo info = new System.IO.FileInfo(f);
  868. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  869. if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
  870. {
  871. isDragingEnter = true;
  872. }
  873. }
  874. DragDropHelper.DragEnter(this, e);
  875. }
  876. private void Grid_DragLeave(object sender, DragEventArgs e)
  877. {
  878. DragDropHelper.DragLeave();
  879. }
  880. private void Grid_Drop(object sender, DragEventArgs e)
  881. {
  882. DragDropHelper.Drop(this,e);
  883. }
  884. private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
  885. {
  886. //增加辅助判断,防止误触发 拖动排序
  887. //较大幅度拖动才能触发排序
  888. isDraging = true;
  889. }
  890. private void Border_Drop(object sender, DragEventArgs e)
  891. {
  892. }
  893. private async void TextBoxEx_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  894. {
  895. var text = sender as TextBox;
  896. if(text!=null)
  897. {
  898. await Task.Delay(50);
  899. text.Focus();
  900. }
  901. }
  902. private void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
  903. {
  904. var text = sender as TextBox;
  905. if(text.Visibility==Visibility.Visible&&string.IsNullOrEmpty(text.Text))
  906. {
  907. text.Focus();
  908. }
  909. }
  910. private void TextBoxEx_LostFocus(object sender, RoutedEventArgs e)
  911. {
  912. //因为需要处理点击按钮或者点击列表失去焦点是 不进行弹窗 因此写在cs里,方便获取UI控件
  913. var element = FocusManager.GetFocusedElement(this);
  914. var visual = VisualTreeHelper.HitTest(this, Mouse.GetPosition(this));
  915. //点击其他区域失去焦点时 不响应页码判断逻辑
  916. if (visual != null)
  917. {
  918. var listbox = CommonHelper.FindVisualParent<ListBox>(visual.VisualHit);
  919. if (listbox != null)
  920. {
  921. //点击在listbox列表内
  922. return;
  923. }
  924. var image = CommonHelper.FindVisualParent<Image>(visual.VisualHit);
  925. if (image != null)
  926. {
  927. //点击在listbox列表内,具体某一项时,上面的判断调整可能失效,需要补充条件
  928. return;
  929. }
  930. var button =CommonHelper.FindVisualParent<Button>(visual.VisualHit);
  931. if(button!=null)
  932. {
  933. //点击在按钮时
  934. return;
  935. }
  936. var combox = CommonHelper.FindVisualParent<ComboBox>(visual.VisualHit);
  937. if(combox!=null)
  938. {
  939. //点击在下拉框时
  940. return;
  941. }
  942. viewModel.lostfocus(e);
  943. }
  944. }
  945. private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  946. {
  947. var visual = VisualTreeHelper.HitTest(this, Mouse.GetPosition(this));
  948. if (visual!=null&&(visual.VisualHit as Grid)!=null)
  949. {
  950. //点击工具栏空白处时 失去焦点
  951. ListPageEdit.Focus();
  952. }
  953. }
  954. private void MenuItem_Click(object sender, RoutedEventArgs e)
  955. {
  956. //因为需要获取准确的选中页面,因此把入口放在.cs代码里
  957. List<int> lists = new List<int>();
  958. foreach(PageEditItem item in ListPageEdit.SelectedItems)
  959. {
  960. lists.Add(item.PageNumber);
  961. }
  962. viewModel.print(lists);
  963. }
  964. private void PageEdit_Unloaded(object sender, RoutedEventArgs e)
  965. {
  966. //释放定时器 避免内存泄露
  967. timer?.Stop();
  968. if (timer != null)
  969. {
  970. timer.Tick -= Timer_Tick;
  971. timer = null;
  972. }
  973. }
  974. private void PageEdit_Loaded(object sender, RoutedEventArgs e)
  975. {
  976. if (timer == null)
  977. {
  978. timer = new DispatcherTimer();
  979. timer.Tick += Timer_Tick;
  980. }
  981. }
  982. }
  983. }