PageEditContent.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. using PDF_Office.EventAggregators;
  2. using PDF_Office.Helper;
  3. using PDF_Office.Model.PageEdit;
  4. using PDF_Office.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_Office.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. public PageEditContent(IEventAggregator eventAggregator) : this()
  84. {
  85. eventor = eventAggregator;
  86. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  87. timer.Interval = TimeSpan.FromSeconds(0.3);
  88. timer.Tick += Timer_Tick;
  89. viewModel = this.DataContext as PageEditContentViewModel;
  90. //订阅页面刷新事件
  91. eventor.GetEvent<PageEditNotifyEvent>().Subscribe(OneNotifyEvent, e => e.Unicode == unicode);
  92. }
  93. private void Timer_Tick(object sender, EventArgs e)
  94. {
  95. PulishEvent();
  96. timer.Stop();
  97. }
  98. private void OneNotifyEvent(PageEditNotifyEventArgs e)
  99. {
  100. if (e.Type == NotifyType.RefreshPage)
  101. {
  102. PulishEvent();
  103. }
  104. else
  105. {
  106. if (e.Type == NotifyType.SelectItems)
  107. {
  108. if (e.PageRange.Count == 1)
  109. {
  110. ListPageEdit.SelectedIndex = e.PageRange[0] - 1;
  111. ListPageEdit.ScrollIntoView(ListPageEdit.SelectedItem);
  112. }
  113. else
  114. {
  115. ListPageEdit.SelectedItems.Clear();
  116. for (int i = 0; i < e.PageRange.Count; i++)
  117. {
  118. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[e.PageRange[i] - 1]);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. #region UI事件
  125. /// <summary>
  126. /// 每次显示的时候就触发事件,刷新所有图片
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  131. {
  132. if ((bool)e.NewValue)
  133. {
  134. //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
  135. PulishEvent();
  136. //当前页面发生变化时通过ScrollChanged事件来刷新图片
  137. isFirstScrollChange = true;
  138. }
  139. }
  140. /// <summary>
  141. /// 鼠标滚轮滚动时触发的事件
  142. /// </summary>
  143. /// <param name="sender"></param>
  144. /// <param name="e"></param>
  145. private void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
  146. {
  147. if (isFirstScrollChange)
  148. {
  149. PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
  150. isFirstScrollChange = false;
  151. return;
  152. }
  153. else
  154. {
  155. if (e.VerticalChange != 0)
  156. timer.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
  157. }
  158. }
  159. /// <summary>
  160. /// 拖动右侧滑块时触发的事件
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
  165. {
  166. scrolltype = e.ScrollEventType;
  167. if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
  168. {
  169. timer.Stop();
  170. return;
  171. }
  172. PulishEvent();
  173. }
  174. #endregion
  175. #region 方法
  176. /// <summary>
  177. /// 发布事件
  178. /// </summary>
  179. private void PulishEvent()
  180. {
  181. var itemSize = (ListPageEdit.Items[0] as PageEditItem).ItemSize;
  182. var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5, 10, 5, 10));
  183. eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
  184. }
  185. /// <summary>
  186. /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
  187. /// 返回值为页面范围 从1开始
  188. /// </summary>
  189. /// <param name="view"></param>
  190. /// <param name="itemSize"></param>
  191. /// <param name="itemMargin"></param>
  192. /// <returns></returns>
  193. private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  194. {
  195. //var scrollViewer = GetScrollHost(view);
  196. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  197. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  198. return new Tuple<int, int, int>(0, 0, 0);
  199. try
  200. {
  201. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  202. var currentWidth = scrollViewer.ActualWidth;
  203. //计算当前窗口大小能显示的行数和列数
  204. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  205. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  206. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  207. preItemCount = preItemCount * columnCount;
  208. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  209. preEnd = preEnd * columnCount + columnCount - 1;
  210. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  211. return new Tuple<int, int, int>(
  212. Math.Max(preItemCount, 0),
  213. Math.Min(view.Items.Count, preEnd),
  214. middle);
  215. }
  216. catch { }
  217. return new Tuple<int, int, int>(0, 0, 0);
  218. }
  219. /// <summary>
  220. /// 获取listbox里的ScrollViewer对象
  221. /// 留意如果有重写ListBox对象,该方法可能无效
  222. /// </summary>
  223. /// <param name="listBox"></param>
  224. /// <returns></returns>
  225. private ScrollViewer GetScrollHost(ListBox listBox)
  226. {
  227. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  228. {
  229. int s = VisualTreeHelper.GetChildrenCount(listBox);
  230. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  231. if (border != null)
  232. {
  233. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  234. }
  235. }
  236. return null;
  237. }
  238. #endregion
  239. /// <summary>
  240. /// 输入框显示时,自动获取焦点
  241. /// </summary>
  242. /// <param name="sender"></param>
  243. /// <param name="e"></param>
  244. private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  245. {
  246. if ((bool)e.NewValue)
  247. {
  248. (sender as Control).Focus();
  249. }
  250. }
  251. private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
  252. {
  253. try
  254. {
  255. if (e.LeftButton == MouseButtonState.Pressed)
  256. {
  257. //鼠标框选逻辑
  258. if (startChoose)
  259. {
  260. var position = e.GetPosition(ListPageEdit);
  261. if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
  262. {
  263. startChoose = false;
  264. RectChoose.Visibility = Visibility.Collapsed;
  265. //暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
  266. Mouse.Capture(null);
  267. return;
  268. }
  269. //矩形框内的item设为选中
  270. DoSelectItems();
  271. return;
  272. }
  273. //拖拽排序的逻辑
  274. var pos = e.GetPosition(ListPageEdit);
  275. if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
  276. {
  277. LineInset.Visibility = Visibility.Collapsed;
  278. ImgPicture.Visibility = Visibility.Collapsed;
  279. return;
  280. }
  281. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  282. if (result == null)
  283. {
  284. return;
  285. }
  286. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  287. if (listBoxItem == null)
  288. {
  289. return;
  290. }
  291. isDragingEnter = false;
  292. tempItem = listBoxItem.DataContext as PageEditItem;
  293. var item_pos = e.GetPosition(listBoxItem);
  294. if (item_pos != null)
  295. {
  296. item_x = item_pos.X;
  297. item_y = item_pos.Y;
  298. }
  299. var scroll = GetScrollHost(ListPageEdit);
  300. string filename = Guid.NewGuid().ToString() + ".pdf";
  301. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
  302. System.IO.File.Create(tempPath);
  303. string[] files = new string[1];
  304. files[0] = tempPath;
  305. DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
  306. DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
  307. Mouse.Capture(ListPageEdit);
  308. return;
  309. }
  310. RectChoose.Visibility = Visibility.Collapsed;
  311. startChoose = false;
  312. Mouse.Capture(null);
  313. }
  314. catch
  315. {
  316. LineInset.Visibility = Visibility.Collapsed;
  317. ImgPicture.Visibility = Visibility.Collapsed;
  318. }
  319. }
  320. /// <summary>
  321. /// 判断是否开始框选、记录框选起始位置
  322. /// </summary>
  323. /// <param name="sender"></param>
  324. /// <param name="e"></param>
  325. private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  326. {
  327. var pos = e.GetPosition(ListPageEdit);
  328. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  329. if (result == null)
  330. {
  331. return;
  332. }
  333. //未选中item 并且不是点击滑轨时 开始框选
  334. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  335. var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
  336. if (listBoxItem == null)
  337. {
  338. if (scroller != null)
  339. {
  340. startChoose = false;
  341. return;
  342. }
  343. //点击空白处时开始框选
  344. startChoose = true;
  345. if (ListPageEdit.SelectedItems.Count > 0)
  346. {
  347. ListPageEdit.SelectedItems.Clear();
  348. }
  349. starPosition = e.GetPosition(ListPageEdit);
  350. starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
  351. Mouse.Capture(ListPageEdit);
  352. return;
  353. }
  354. //选中了item 时,不能框选
  355. startChoose = false;
  356. //更改系统的选中规则,选中状态下,鼠标松开后取消选中
  357. //方便实现多选拖拽功能
  358. if (listBoxItem.IsSelected == true && !Keyboard.IsKeyDown(Key.LeftCtrl))
  359. {
  360. e.Handled = true;
  361. }
  362. }
  363. private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  364. {
  365. var pos = e.GetPosition(ListPageEdit);
  366. HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  367. if (result == null)
  368. {
  369. return;
  370. }
  371. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  372. if (listBoxItem == null)
  373. {
  374. return;
  375. }
  376. //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
  377. //拖拽框选之后的抬起鼠标不进入处理
  378. if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
  379. {
  380. ListPageEdit.SelectedItems.Clear();
  381. ListPageEdit.SelectedItem = listBoxItem;
  382. listBoxItem.IsSelected = true;
  383. return;
  384. }
  385. Mouse.Capture(null);
  386. //结束鼠标框选
  387. startChoose = false;
  388. RectChoose.Visibility = Visibility.Collapsed;
  389. }
  390. /// <summary>
  391. /// 获取Listobox的Wrappanel容器
  392. /// </summary>
  393. /// <param name="listBox"></param>
  394. /// <returns></returns>
  395. public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
  396. {
  397. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  398. var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
  399. return panel;
  400. }
  401. /// <summary>
  402. ///根据鼠标拖选的框 选中矩形框里面的Item
  403. /// </summary>
  404. private void DoSelectItems()
  405. {
  406. var s = GetScrollHost(ListPageEdit);
  407. Point start = new Point();
  408. //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
  409. start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
  410. var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
  411. RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
  412. RectChoose.Width = rec.Width;
  413. RectChoose.Height = rec.Height;
  414. RectChoose.Visibility = Visibility.Visible;
  415. //检测遍历所有项,筛选在矩形框中的Item
  416. for (int i = 0; i < ListPageEdit.Items.Count; i++)
  417. {
  418. var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  419. //通过这一步来避免重复误选中
  420. var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
  421. if (parent == null)
  422. continue;
  423. var v = VisualTreeHelper.GetOffset(_item);
  424. if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
  425. {
  426. ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
  427. }
  428. else
  429. {
  430. ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
  431. }
  432. }
  433. return;
  434. }
  435. /// <summary>
  436. /// 退出拖拽模式
  437. /// </summary>
  438. private void ExitDraging()
  439. {
  440. LineInset.Visibility = Visibility.Collapsed;
  441. ImgPicture.Visibility = Visibility.Collapsed;
  442. isDraging = false;
  443. }
  444. /// <summary>
  445. /// 拖拽释放后的处理逻辑
  446. /// </summary>
  447. /// <param name="sender"></param>
  448. /// <param name="e"></param>
  449. private async void ListPageEdit_Drop(object sender, DragEventArgs e)
  450. {
  451. needScroll = false;
  452. if (!isDraging)
  453. {
  454. //未拖拽时隐藏插入标记和虚影
  455. LineInset.Visibility = Visibility.Collapsed;
  456. ImgPicture.Visibility = Visibility.Collapsed;
  457. return;
  458. }
  459. #region 功能付费锁
  460. // //if (!App.IsActive())
  461. // //{
  462. // // MidLane.Visibility = Visibility.Collapsed;
  463. // // IAPFunctionDialog dialog = new IAPFunctionDialog("PageEdit");
  464. // // dialog.ShowDialog();
  465. // // return;
  466. // //}
  467. #endregion
  468. #region 从外部拖拽插入文件
  469. if (isDragingEnter)
  470. {
  471. var files = (string[])e.Data.GetData(DataFormats.FileDrop);
  472. Array.Reverse(files);
  473. foreach(string file in files)
  474. {
  475. System.IO.FileInfo info = new System.IO.FileInfo(file);
  476. if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
  477. {
  478. int index = InsertIndex == -1 ? 0 : InsertIndex;
  479. viewModel.InsertFromFile(index, file);
  480. viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
  481. }
  482. }
  483. //其他文件 则新增一个页签打开
  484. // DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个 Load(TPDFDocument)的接口
  485. LineInset.Visibility = Visibility.Collapsed;
  486. ImgPicture.Visibility = Visibility.Collapsed;
  487. isDragingEnter = false;
  488. return;
  489. }
  490. #endregion
  491. var pos = e.GetPosition(ListPageEdit);
  492. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  493. //if (result == null)
  494. //{
  495. // //超出当前可控区域
  496. // ExitDraging();
  497. // return;
  498. //}
  499. ////查找元数据
  500. //var sourcePerson = e.Data.GetData(typeof(StackPanel)) as StackPanel;
  501. //if (sourcePerson == null)
  502. //{
  503. // ExitDraging();
  504. // return;
  505. //}
  506. //查找目标数据
  507. int targetindex = 0;//目标插入位置
  508. if (InsertIndex != -1)
  509. {
  510. //往前移动时 此index 不是准确的,需要处理++
  511. targetindex = InsertIndex;
  512. }
  513. //else//基本不会命中 仅作为保险措施
  514. //{
  515. // var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  516. // if (listBoxItem == null)
  517. // {
  518. // ////鼠标停留在两个item之间或其他无效区域 暂时不做处理(比较麻烦)
  519. // ExitDraging();
  520. // return;
  521. // }
  522. // var targetPerson = listBoxItem;
  523. // targetPerson.Opacity = 1;
  524. // sourcePerson.Opacity = 1;
  525. // if (ReferenceEquals(targetPerson, sourcePerson))
  526. // {
  527. // ExitDraging();
  528. // return;
  529. // }
  530. // targetindex = ListPageEdit.Items.IndexOf(targetPerson);
  531. //}
  532. List<ListBoxItem> list = new List<ListBoxItem>();
  533. List<int> sourceindex = new List<int>();//需要保存每个页面对应的位置
  534. //开始排序
  535. List<int> pages = new List<int>();
  536. //要先对所有选中项 根据页码排序
  537. for (int i = 0; i < ListPageEdit.SelectedItems.Count; i++)
  538. {
  539. var pageindex = ListPageEdit.Items.IndexOf(ListPageEdit.SelectedItems[i] as PageEditItem);
  540. pages.Add(pageindex);//存入的为页码索引值
  541. }
  542. if (pages.Count <= 0)
  543. {
  544. ExitDraging();
  545. return;
  546. }
  547. viewModel.DragToSort(targetindex,pages);
  548. ExitDraging();
  549. isDraging = false;
  550. }
  551. private void MidLane_Drop(object sender, DragEventArgs e)
  552. {
  553. ListPageEdit_Drop(sender, e);
  554. }
  555. private void ImgPicture_Drop(object sender, DragEventArgs e)
  556. {
  557. ListPageEdit_Drop(sender, e);
  558. }
  559. /// <summary>
  560. /// 拖拽事件写在外部的Grid里,会更加流畅
  561. /// </summary>
  562. /// <param name="sender"></param>
  563. /// <param name="e"></param>
  564. private void Grid_DragOver(object sender, DragEventArgs e)
  565. {
  566. try
  567. {
  568. //ctrl建按下 不显示插入标记和虚影 因为释放不会响应drop事件
  569. if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
  570. return;
  571. //滚动后有 位置不准确 要减去滚动偏移量
  572. //控制线的位置
  573. var pos = e.GetPosition(ListPageEdit);
  574. var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
  575. if (result == null)
  576. {
  577. //MidLane.Visibility = Visibility.Collapsed;
  578. //return;
  579. }
  580. //获取当前鼠标指针下的容器
  581. var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
  582. if (listBoxItem == null)
  583. {
  584. //MidLane.Visibility = Visibility.Collapsed;
  585. //return;
  586. }
  587. #region 计算虚影位置
  588. //xaml层 要设置 虚影控件为左上
  589. double xPos = 0;
  590. double yPos = 0;
  591. //内部拖动
  592. if (!isDragingEnter)
  593. {
  594. if(tempItem!=null)
  595. {
  596. ImgPicture.Width = tempItem.ItemSize.Width;
  597. ImgPicture.Height = tempItem.ItemSize.Height;
  598. ImgPicture.Source = tempItem.Image;
  599. xPos = e.GetPosition(ListPageEdit).X - item_x;
  600. yPos = e.GetPosition(ListPageEdit).Y - item_y;
  601. }
  602. }
  603. else
  604. {
  605. DragDropHelper.DragOver(this, e);
  606. //从外部拖入的逻辑
  607. //var pic = ToBitmapSource(dragingEnterPath);
  608. //ShadowPicture.Width = pic.Width;
  609. //ShadowPicture.Height = pic.Height;
  610. //ShadowPicture.Source = pic;
  611. //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
  612. //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
  613. }
  614. ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
  615. #endregion
  616. #region 计算插入标记位置
  617. var scroll = GetScrollHost(ListPageEdit);
  618. if (listBoxItem != null)
  619. {
  620. //虚拟化影响到该值计算
  621. var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
  622. LineInset.Visibility = Visibility.Visible;
  623. if(!isDragingEnter)
  624. {
  625. ImgPicture.Visibility = Visibility.Visible;
  626. }
  627. else
  628. {
  629. ImgPicture.Visibility = Visibility.Collapsed;
  630. }
  631. var panel = GetWrapPanel(ListPageEdit);
  632. //var item = panel.ItemSize.Width;
  633. //var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
  634. var item = listBoxItem.DesiredSize.Width;
  635. int count = (int)(panel.ViewportWidth / item);
  636. var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
  637. LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
  638. if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
  639. {
  640. LineInset.X2 = LineInset.X1 = p.X - gap / 2;
  641. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
  642. }
  643. else
  644. {
  645. InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
  646. }
  647. Console.WriteLine("InsertIndex:{0}\r\n",InsertIndex);
  648. //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
  649. LineInset.Y1 = p.Y;
  650. if (LineInset.Y1 < 0)//避免超出上边界
  651. {
  652. LineInset.Y1 = 0;
  653. }
  654. //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
  655. LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
  656. if (LineInset.Y2 < 0)
  657. {
  658. LineInset.Y2 = 0;
  659. }
  660. }
  661. #endregion
  662. //暂时处理 鼠标移出边框时,虚影的显示问题
  663. if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
  664. {
  665. LineInset.Visibility = Visibility.Collapsed;
  666. ImgPicture.Visibility = Visibility.Collapsed;
  667. needScroll = false;
  668. }
  669. if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
  670. {
  671. LineInset.Visibility = Visibility.Collapsed;
  672. ImgPicture.Visibility = Visibility.Collapsed;
  673. needScroll = false;
  674. }
  675. #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
  676. speed = 0;
  677. if (pos.Y >= ListPageEdit.ActualHeight - 30)
  678. {
  679. speed = 30 - (int)(ListPageEdit.ActualHeight - pos.Y);
  680. needScroll = true;
  681. }
  682. else if (pos.Y <= 30)
  683. {
  684. speed = (int)(pos.Y - 30);
  685. needScroll = true;
  686. }
  687. else
  688. needScroll = false;
  689. var v = scroll.VerticalOffset;
  690. scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
  691. #endregion
  692. }
  693. catch
  694. {
  695. }
  696. }
  697. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  698. {
  699. //e.Handled = true;
  700. }
  701. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  702. {
  703. if (e.ChangedButton == MouseButton.Left)
  704. {
  705. //双击回到PDFViewer界面
  706. var item = sender as ListBoxItem;
  707. if (item != null)
  708. {
  709. var index = ListPageEdit.ItemContainerGenerator.IndexFromContainer(item);
  710. //双击事件无法绑定Command 暂时采用这种方式调用VM里的方法
  711. viewModel.BackToPDFViewer(index);
  712. }
  713. }
  714. }
  715. private void PageEdit_SizeChanged(object sender, SizeChangedEventArgs e)
  716. {
  717. //BOTA缩略图模式下需要调整Item宽度
  718. if (GridBOTAHeader.Visibility == Visibility.Visible)
  719. {
  720. ItemSuitAcutalWidth(e.NewSize.Width);
  721. }
  722. }
  723. private void ItemSuitAcutalWidth(double width)
  724. {
  725. //缩略图模式下,保持一列或者两列显示
  726. if ((bool)TbnTwoLine.IsChecked)
  727. {
  728. var itemwidth = (width - 80) / 2;
  729. var itemHeight = 294 * itemwidth / 208.0;
  730. viewModel.ChangeItemSize(new Size(itemwidth, itemHeight));
  731. }
  732. else
  733. {
  734. var itemwidth = 0.6 * width;
  735. var itemheight = 294 * itemwidth / 208.0;
  736. viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
  737. }
  738. }
  739. private void TbnTwoLine_Click(object sender, RoutedEventArgs e)
  740. {
  741. ItemSuitAcutalWidth(this.ActualWidth);
  742. }
  743. /// <summary>
  744. /// 用来判断是否有外界拖入的事件
  745. /// </summary>
  746. /// <param name="sender"></param>
  747. /// <param name="e"></param>
  748. private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
  749. {
  750. DragDropHelper.DragEnter(this,e);
  751. var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  752. if (file == null)//为null 表示内部拖动 触发的
  753. {
  754. return;
  755. }
  756. isDragingEnter = false;
  757. foreach (string f in file)
  758. {
  759. System.IO.FileInfo info = new System.IO.FileInfo(f);
  760. //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
  761. if(System.IO.Path.GetExtension(f).ToLower()==".pdf" &&info.Length>0)
  762. {
  763. isDragingEnter = true;
  764. }
  765. }
  766. }
  767. private void Grid_DragLeave(object sender, DragEventArgs e)
  768. {
  769. DragDropHelper.DragLeave();
  770. }
  771. private void Grid_Drop(object sender, DragEventArgs e)
  772. {
  773. DragDropHelper.Drop(this,e);
  774. }
  775. private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
  776. {
  777. //增加辅助判断,防止误触发 拖动排序
  778. //较大幅度拖动才能触发排序
  779. isDraging = true;
  780. }
  781. }
  782. }