123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635 |
- using PDF_Office.CustomControl;
- using PDF_Office.Helper;
- using PDF_Office.Model.PDFTool;
- using PDF_Office.ViewModels.HomePanel;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace PDF_Office.Views.HomePanel.PDFTools
- {
- /// <summary>
- /// ToolbarPage.xaml 的交互逻辑
- /// </summary>
- public partial class PDFToolsContent : UserControl
- {
- private ListItemQuickTool sourceListItem = null;
- private ListItemQuickTool targetListItem = null;
- private ListItemQuickTool quickSourceListItem = null;
- private List<ToolItem> quickTools = null;
- private List<ToolItem> allTools = null;
- private List<ToolItem> moreTools = null;
- private string fileName = string.Empty;
- private BitmapSource bitmapSource = null;
- private bool isMoreCuts = false;
- private bool isMove = false;
- private Point wrpnlPoint;
- private Point mousePoint;
- public PDFToolsContent()
- {
- InitializeComponent();
- quickTools = new List<ToolItem>();
- allTools = new List<ToolItem>();
- #region ListBox
- SetListBox();
- #endregion ListBox
- }
- /// <summary>
- /// 获取与窗口相对的鼠标位置。
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OnRendering(object sender, EventArgs e)
- {
- mousePoint = Mouse.GetPosition(this);
- Console.WriteLine($"鼠标坐标:{mousePoint.X}:{mousePoint.Y}");
- }
- private void SetListBox()
- {
- string path = @"pack://application:,,,/Resources/PromotionIcon/Windows.png";
- List<string> all = new List<string>() { "PDF转Word", "PDF转Excel", "PDF转PPT", "转档PDF", "OCR", "拆分",
- "提取","合并","压缩","图片转PDF","安全", "水印", "页眉页脚","贝茨Bates码","批量处理","打印", "背景","插入","文件对比"};
- for (int i = 0; i < all.Count; i++)
- {
- ToolItem aToolModule = new ToolItem();
- aToolModule.Id = i + 1;
- aToolModule.Name = all[i];
- aToolModule.Image = path;
- allTools.Add(aToolModule);
- }
- //取前10个,如果List里面数据少于5个,则返回所有的
- quickTools = allTools.Take(8).ToList<ToolItem>();
- ListShortCuts.ItemsSource = quickTools;
- //对比两个集合,取差值
- moreTools = allTools.Except(quickTools).ToList();
- ListMoreCuts.ItemsSource = moreTools;
- }
- private void Page_Loaded(object sender, RoutedEventArgs e)
- {
- }
- private void ListBoxMoreCuts_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- var pos = e.GetPosition(ListMoreCuts);
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- ListMoreCuts.AllowDrop = true;
- HitTestResult result = VisualTreeHelper.HitTest(ListMoreCuts, pos);
- if (result == null)
- {
- sourceImage.Visibility = Visibility.Hidden;
- return;
- }
- sourceListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (sourceListItem == null || sourceListItem.Content != ListMoreCuts.SelectedItem)
- {
- return;
- }
- //获取控件相对于另一控件的坐标
- wrpnlPoint = sourceListItem.TranslatePoint(new Point(), this);
- System.Windows.DataObject dataObj = new System.Windows.DataObject(sourceListItem.Content as ToolItem);
- if (dataObj != null)
- {
- //控件截图
- IsMoreToolbars();
- if (string.IsNullOrEmpty(fileName))
- {
- sourceImage.Visibility = Visibility.Hidden;
- return;
- }
- //图片解码
- ImageDecoding();
- isMoreCuts = true;
- DragDrop.DoDragDrop(ListMoreCuts, dataObj, System.Windows.DragDropEffects.Move);
- }
- }
- else
- {
- moveImage.Visibility = Visibility.Hidden;
- sourceImage.Visibility = Visibility.Hidden;
- }
- }
- private void ImageDecoding()
- {
- Stream imageStreamSource = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
- PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
- bitmapSource = decoder.Frames[0];
- }
- private void IsMoreToolbars()
- {
- var source = sourceListItem.Content as ToolItem;
- if (moreTools == null && moreTools.Count != 0) return;
- List<ToolItem> toolModule = moreTools.FindAll(item => item.Name == source.Name);
- if (toolModule.Count > 0)
- {
- // 判断文件夹是否存在,不存在则创建
- DirectoryInfo folder = new DirectoryInfo(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Asource"));
- if (!folder.Exists)
- {
- folder.Create();
- }
- fileName = System.IO.Path.Combine(folder.FullName, $"{source.Name}.png");
- SaveToPng(sourceListItem, fileName);
- }
- }
- private void ListBoxShortCuts_Drop(object sender, DragEventArgs e)
- {
- var pos = e.GetPosition(ListShortCuts);
- var result = VisualTreeHelper.HitTest(ListShortCuts, pos);
- if (result == null)
- {
- WorkingWithAnimation(pos);
- return;
- }
- //查找目标数据
- targetListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (targetListItem == null)
- {
- WorkingWithAnimation(pos);
- return;
- }
- var targetAToolModule = targetListItem.Content as ToolItem;
- ToolItem target = quickTools.Find(c => c.Name == targetAToolModule.Name);
- //查找元数据
- var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
- if (sourceAToolModule == null)
- {
- sourceImage.Visibility = Visibility.Hidden;
- // target.SetValue(AToolModule.IsFilePickedProperty, false);
- return;
- }
- if (ReferenceEquals(targetAToolModule, sourceAToolModule))
- {
- sourceImage.Visibility = Visibility.Hidden;
- // target.SetValue(AToolModule.IsFilePickedProperty, false);
- return;
- }
- if (targetAToolModule == null && sourceAToolModule == null)
- {
- sourceImage.Visibility = Visibility.Hidden;
- // target.SetValue(AToolModule.IsFilePickedProperty, false);
- return;
- }
- ModuleExchange(sourceAToolModule, targetAToolModule);
- }
- private void ModuleExchange(ToolItem sourceAToolModule, ToolItem targetAToolModule)
- {
- if (moreTools == null) return;
- ToolItem source = moreTools.Find(c => c.Name == sourceAToolModule.Name);
- int targetIndex = quickTools.FindIndex(item => item.Name == targetAToolModule.Name);
- if (targetIndex < 0) return;
- if (!isMoreCuts)
- {
- ListMoreCuts.AllowDrop = false;
- sourceImage.Visibility = Visibility.Hidden;
- source = quickTools.Find(c => c.Name == sourceAToolModule.Name);
- if (source == null) return;
- int sourceIndex = quickTools.FindIndex(item => item.Name == sourceAToolModule.Name);
- if (sourceIndex < 0) return;
- var temp = quickTools[sourceIndex];
- quickTools[sourceIndex] = quickTools[targetIndex];
- quickTools[targetIndex] = temp;
- ListShortCuts.Items.Refresh();
- ListBoxCutsWidthChanged(ListShortCuts);
- }
- else
- {
- if (target == null) return;
- if (source == null) return;
- moreTools.Remove(source);
- moreTools.Insert(0, target);
- quickTools.Remove(target);
- quickTools.Insert(targetIndex, source);
- ListMoreCuts.Items.Refresh();
- ListShortCuts.Items.Refresh();
- sourceImage.Visibility = Visibility.Hidden;
- targetListItem.IsOverModular = false;
- ListBoxCutsWidthChanged(ListShortCuts);
- ListBoxCutsWidthChanged(ListMoreCuts);
- }
- }
- private void WorkingWithAnimation(Point pos)
- {
- sourceImage.Visibility = Visibility.Hidden;
- if (!isMoreCuts || !isMove)
- {
- moveImage.Visibility = Visibility.Hidden;
- }
- else
- {
- if (sourceListItem == null) return;
- moveImage.Visibility = Visibility.Visible;
- Point point = sourceListItem.TransformToAncestor(this).Transform(new Point(0, 0));
- ImageMoveTo(new Point(pos.X - 80, pos.Y), new Point(wrpnlPoint.X + 40, wrpnlPoint.Y), moveImage, 200);
- }
- }
- /// <summary>
- /// 图片移动动画
- /// </summary>
- /// <param name="curPoint">初始位置</param>
- /// <param name="deskPoint">控件要移动到的位置</param>
- /// <param name="ell">你要移动的空间</param>
- /// <param name="space">设置移动的时间片(关系到控件移动的速度)</param>
- private void ImageMoveTo(Point curPoint, Point deskPoint, Image ell, double space)
- {
- Storyboard storyboard = new Storyboard(); //创建Storyboard对象
- double lxspeed = space, lyspeed = space; //设置X方向 / Y方向 移动时间片
- //创建X轴方向动画
- DoubleAnimation dox = new DoubleAnimation(
- curPoint.X,
- deskPoint.X,
- new Duration(TimeSpan.FromMilliseconds(lxspeed))
- );
- //创建Y轴方向动画
- DoubleAnimation doy = new DoubleAnimation(
- curPoint.Y,
- deskPoint.Y,
- new Duration(TimeSpan.FromMilliseconds(lyspeed))
- );
- Storyboard.SetTarget(dox, ell);
- Storyboard.SetTarget(doy, ell);
- Storyboard.SetTargetProperty(dox, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
- Storyboard.SetTargetProperty(doy, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
- storyboard.Children.Add(dox);
- storyboard.Children.Add(doy);
- storyboard.Completed += Storyboard_Completed;
- //动画播放
- storyboard.Begin();
- }
- private void Storyboard_Completed(object sender, EventArgs e)
- {
- moveImage.Visibility = Visibility.Hidden;
- }
- /// <summary>
- /// 控件截图
- /// </summary>
- /// <param name="visual">控件</param>
- /// <param name="fileName">文件路径 C:\Users\oyxh\AppData\Local\Temp\</param>
- private void SaveToPng(FrameworkElement visual, string fileName)
- {
- DrawingVisual drawingVisual = new DrawingVisual();
- using (DrawingContext context = drawingVisual.RenderOpen())
- {
- VisualBrush brush = new VisualBrush(sourceListItem) { Stretch = Stretch.None };
- context.DrawRectangle(brush, null, new Rect(0, 0, sourceListItem.ActualWidth, sourceListItem.ActualHeight));
- context.Close();
- }
- RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)sourceListItem.ActualWidth, (int)sourceListItem.ActualHeight, 96d, 96d, PixelFormats.Default);
- targetBitmap.Render(drawingVisual);
- FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Interlace = PngInterlaceOption.On;
- encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
- encoder.Save(stream);
- stream.Close();
- }
- private ToolItem target;
- private void ListBoxShortCuts_DragOver(object sender, DragEventArgs e)
- {
- var pos = e.GetPosition(ListShortCuts);
- Console.WriteLine($"pos:{pos.X} {pos.Y} this.Grid.ActualHeight1 {this.Grid.ActualHeight}");
- if (pos.Y < 0)
- {
- isMove = false;
- sourceImage.Visibility = Visibility.Hidden;
- sourceListItem = null;
- return;
- }
- if (pos.Y >= this.Grid.ActualHeight - 50)
- {
- isMove = false;
- sourceImage.Visibility = Visibility.Hidden;
- sourceListItem = null;
- return;
- }
- if (pos.X < 10)
- {
- isMove = false;
- sourceListItem = null;
- sourceImage.Visibility = Visibility.Hidden;
- return;
- }
- if (pos.X >= this.Grid.ActualWidth - 10)
- {
- isMove = false;
- sourceListItem = null;
- sourceImage.Visibility = Visibility.Hidden;
- return;
- }
- if (!isMoreCuts)
- {
- var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
- if (sourceAToolModule == null) return;
- int sourceIndex = quickTools.FindIndex(item => item.Name == sourceAToolModule.Name);
- //根据index找listbox对应的item
- sourceListItem = (ListItemQuickTool)(ListShortCuts.ItemContainerGenerator.ContainerFromIndex(sourceIndex) as FrameworkElement);
- if (sourceListItem == null)
- {
- return;
- }
- ListMoreCuts.AllowDrop = false;
- sourceImage.Visibility = Visibility.Hidden;
- }
- else
- {
- var sourceAToolModule = e.Data.GetData(typeof(ToolItem)) as ToolItem;
- if (sourceAToolModule == null) return;
- int sourceIndex = moreTools.FindIndex(item => item.Name == sourceAToolModule.Name);
- //根据index找listbox对应的item
- sourceListItem = (ListItemQuickTool)(ListMoreCuts.ItemContainerGenerator.ContainerFromIndex(sourceIndex) as FrameworkElement);
- if (sourceListItem == null)
- {
- return;
- }
- isMove = true;
- //图片绑定
- ImageBinding(pos);
- }
- var result = VisualTreeHelper.HitTest(ListShortCuts, pos);
- if (result == null)
- {
- return;
- }
- //查找目标数据
- targetListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (targetListItem == null)
- {
- //sourceImage.Visibility = Visibility.Hidden;
- return;
- }
- var targetAToolModule = targetListItem.Content as ToolItem;
- if (targetAToolModule == null) return;
- target = quickTools.Find(c => c.Name == targetAToolModule.Name);
- }
- /// <summary>
- ///图片绑定
- /// </summary>
- private void ImageBinding(System.Windows.Point pos)
- {
- sourceImage.Visibility = Visibility.Visible;
- sourceImage.Source = bitmapSource;
- sourceImage.Margin = new Thickness(pos.X - 80, pos.Y, 0, 0);
- moveImage.Source = sourceImage.Source;
- }
- private void ListBoxShortCuts_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- ListMoreCuts.AllowDrop = false;
- var pos = e.GetPosition(ListShortCuts);
- HitTestResult result = VisualTreeHelper.HitTest(ListShortCuts, pos);
- if (result == null)
- {
- return;
- }
- quickSourceListItem = (ListItemQuickTool)CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
- if (quickSourceListItem == null || quickSourceListItem.Content != ListShortCuts.SelectedItem)
- {
- return;
- }
- System.Windows.DataObject dataObj = new System.Windows.DataObject(quickSourceListItem.Content as ToolItem);
- if (dataObj != null)
- {
- isMoreCuts = false;
- DragDrop.DoDragDrop(ListShortCuts, dataObj, System.Windows.DragDropEffects.Move);
- }
- }
- else
- {
- sourceImage.Visibility = Visibility.Hidden;
- //moveImage.Visibility = Visibility.Hidden;
- }
- }
- private void sourceImage_Drop(object sender, DragEventArgs e)
- {
- ListBoxShortCuts_Drop(sender, e);
- }
- private void sourceImage_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- ListBoxMoreCuts_PreviewMouseMove(sender, e);
- }
- private void sourceImage_DragOver(object sender, DragEventArgs e)
- {
- ListBoxShortCuts_DragOver(sender, e);
- }
- private void Label_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- ListBoxMoreCuts_PreviewMouseMove(sender, e);
- }
- private void ListBoxMoreCuts_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- //获取当前控件元素相对鼠标点击的位置(只有点击在当前A控件,会触发事件并返回相对A控件的鼠标位置)。
- Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
- sourceImage.Visibility = Visibility.Hidden;
- if (pp.X < 0 || pp.Y < 0)
- {
- moveImage.Visibility = Visibility.Hidden;
- }
- }
- /// <summary>
- /// 扩展收缩UI
- /// </summary>
- /// <param name="IsShowConciseContent">是否收缩</param>
- private void ShowToolsUI(ListBoxEx list,bool IsShowConciseContent)
- {
- foreach (var item in list.Items)
- {
- var listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
- if (listBoxItem != null)
- {
- var viewItem = listBoxItem.ContentTemplate;
- var myContentPresenter = CommonHelper.FindVisualChild<ContentPresenter>(listBoxItem);
- var obj = viewItem.FindName("data", myContentPresenter);
- var checkNum = obj as PDFToolItem;
- if (checkNum != null)
- {
- checkNum.IsShowConciseContent = IsShowConciseContent;
- }
- }
- }
- }
- private void ListBoxShortCuts_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- ListBoxCutsWidthChanged(ListShortCuts);
- }
- private void ListBoxMoreCuts_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- ListBoxCutsWidthChanged(ListMoreCuts);
- }
- private void ListBoxCutsWidthChanged(ListBoxEx list)
- {
- //3x + 4y = containerWidth;
- //3x = y;
- //x为间距,y为item;
- double containerWidth = list.ActualWidth - 20;
- int widthItem = (int)containerWidth / 5;
- double margin = widthItem / 3;
- int i = 0;
- foreach (var item in list.Items)
- {
- i++;
- var listBoxItem = list.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
- if (listBoxItem != null)
- {
- if (i % 4 != 0)
- {
- listBoxItem.Margin = new Thickness(0, 0, margin, 0);
- }
- else
- {
- listBoxItem.Margin = new Thickness(0, 0, 0, 0);
- }
- listBoxItem.Width = widthItem;
- }
- }
- }
- private void BtnTools_Click(object sender, RoutedEventArgs e)
- {
- }
- bool isExtend = false;
- private void BtnMore_Click(object sender, RoutedEventArgs e)
- {
- if(isExtend)
- {
- isExtend = false;
- ShowToolsUI(ListShortCuts, false);
- ShowToolsUI(ListMoreCuts, false);
- }
- else
- {
- isExtend = true;
- ShowToolsUI(ListShortCuts, true);
- ShowToolsUI(ListMoreCuts, true);
- }
-
-
- }
- //自适应流式布局,以防改需求备用
- //private void ListBoxMoreCutsWidthChanged()
- //{
- // double containerWidth = ListBoxShortCuts.ActualWidth;
- // int Rowcount = (int)containerWidth / 240;
- // double EndRow = containerWidth % 240.0;
- // Rowcount = EndRow >= 140 ? ++Rowcount : Rowcount;
- // int i = 0;
- // foreach (var item in ListBoxMoreCuts.Items)
- // {
- // i++;
- // var listBoxItem = ListBoxMoreCuts.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
- // if (listBoxItem != null)
- // {
- // if (i % Rowcount != 0)
- // {
- // listBoxItem.Margin = new Thickness(0, 0, 100, 0);
- // }
- // else
- // {
- // listBoxItem.Margin = new Thickness(0, 0, 0, 0);
- // }
- // }
- // }
- //}
- }
- }
|