Bläddra i källkod

页面编辑-晚上页面编辑操作,修复自定义页面bug

ZhouJieSheng 2 år sedan
förälder
incheckning
bc5ca068d8

+ 1 - 1
PDF Office/Helper/ToolMethod.cs

@@ -163,7 +163,7 @@ namespace PDF_Office.Helper
             }
             if(tempdoc!=null)
             {
-                tempdoc.Release();
+                tempdoc?.Release();
             }
         }
 

+ 70 - 4
PDF Office/ViewModels/PageEdit/PageEditContentViewModel.cs

@@ -27,6 +27,7 @@ using ComPDFKit.PDFDocument;
 using PDF_Office.CustomControl;
 using PDF_Office.Model.HomePageToolsDialogs;
 using System.IO;
+using System.Text.RegularExpressions;
 
 namespace PDF_Office.ViewModels.PageEdit
 {
@@ -360,6 +361,12 @@ namespace PDF_Office.ViewModels.PageEdit
         /// </summary>
         public DelegateCommand<object> KeyDown { get; set; }
 
+        /// <summary>
+        ///文本框失去焦点事件
+        /// </summary>
+        public DelegateCommand<object> LostFocusCommand { get; set; }
+
+
         /// <summary>
         /// 粘贴
         /// </summary>
@@ -434,6 +441,8 @@ namespace PDF_Office.ViewModels.PageEdit
         /// 显示页面大小
         /// </summary>
         public DelegateCommand ShowPageSizeCommand { get; set; }
+
+        public DelegateCommand ClearSelectedCommand { get; set; }
         #endregion
 
         public PageEditContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
@@ -449,6 +458,7 @@ namespace PDF_Office.ViewModels.PageEdit
             SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
             PreviewKeyDown = new DelegateCommand<object>(PreviewKeyDownEvent);
             KeyDown = new DelegateCommand<object>(KeyDownEvent);
+            LostFocusCommand = new DelegateCommand<object>(lostfocus);
 
             SplitCommand = new DelegateCommand(SplitCommandEvent);
             InsertCommand = new DelegateCommand<object>(InsertCommandEvent);
@@ -465,6 +475,7 @@ namespace PDF_Office.ViewModels.PageEdit
             PasteCommand = new DelegateCommand(paste,CanPasteExcute).ObservesProperty(() => CanPaste);
             CutCommand = new DelegateCommand(cut);
             PrintCommand = new DelegateCommand(print);
+            ClearSelectedCommand = new DelegateCommand(clearSelected);
 
             //订阅页面刷新事件
             eventAggregator.GetEvent<PageEditRefreshEvent>().Subscribe(OnPageEditRefreshEvent, e => e.Unicode == unicode);
@@ -475,7 +486,12 @@ namespace PDF_Office.ViewModels.PageEdit
             InitString();
         }
 
+
+
         #region 事件
+
+
+
         private void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
         {
             if(e.PropertyName=="CopyDoc")
@@ -487,7 +503,6 @@ namespace PDF_Office.ViewModels.PageEdit
             }
         }
 
-
         /// <summary>
         /// 替换功能的逻辑
         /// </summary>
@@ -993,7 +1008,7 @@ namespace PDF_Office.ViewModels.PageEdit
             {
                 List<int> TargetPageIndexs = new List<int>();
                 var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
-                if (!result)
+                if (!result&&ComboxSelectIndex==5)
                 {
                     AlertsMessage alertsMessage = new AlertsMessage();
                     alertsMessage.ShowDialog("","请输入有效范围","OK");
@@ -1014,10 +1029,60 @@ namespace PDF_Office.ViewModels.PageEdit
             }
         }
 
+        /// <summary>
+        /// 失去焦点后选中页面
+        /// </summary>
+        /// <param name="obj"></param>
+        private void lostfocus(object obj)
+        {
+            var args = obj as RoutedEventArgs;
+            if (args == null||(args.OriginalSource as TextBox)==null)
+            {
+                return;
+            }
+            List<int> TargetPageIndexs = new List<int>();
+
+            if(PageRangeItems.Contains((args.OriginalSource as TextBox).Text)&&ComboxSelectIndex!=5)
+            {
+                //选中其他项,失去焦点时不报错
+                return;
+            }
+
+            var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
+            if (!result)
+            {
+                AlertsMessage alertsMessage = new AlertsMessage();
+                alertsMessage.ShowDialog("", "请输入有效范围", "OK");
+                return;
+            }
+
+            //选中输入的页面范围
+            List<int> pageRange = new List<int>();
+            for (int i = 0; i < PageEditItems.Count; i++)
+            {
+                if (TargetPageIndexs.Contains(PageEditItems[i].PageNumber - 1))
+                {
+                    pageRange.Add(i + 1);
+                }
+            }
+
+            NotifyUIToRefresh(pageRange);
+        }
+
         #endregion
 
         #region 方法
+        /// <summary>
+        /// Esc退出多选
+        /// </summary>
+        private void clearSelected()
+        {
+            ListSelectedIndex = -1;
+        }
 
+        /// <summary>
+        /// 调用打印机
+        /// </summary>
         private void print()
         {
             PDFViewer.GoToPage(listSelecedIndex);
@@ -2018,8 +2083,9 @@ namespace PDF_Office.ViewModels.PageEdit
                     }
                     break;
                 case 5://自定义
-                    //取消所有选中
-                    TxtPageInputVisibility = Visibility.Visible;
+
+                    //await Task.Delay(20);
+                    //TbPageRange = "1,3";
                     break;
                 default:
                     break;

+ 34 - 13
PDF Office/Views/MainWindow.xaml.cs

@@ -154,19 +154,40 @@ namespace PDF_Office.Views
         private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
             //通过转移焦点来使文本框触发失去焦点事件
-            //var uiElement = FocusManager.GetFocusedElement(this);
-            //if (uiElement != null)
-            //{
-            //    var textBox = uiElement as TextBox;
-            //    //如果是下拉框里的文本框则不失去焦点
-            //    var parent = CommonHelper.FindVisualParent<ComboBox>(textBox);
-            //    var result = VisualTreeHelper.HitTest(parent,e.GetPosition());
-            //    if (textBox != null&&parent==null)
-            //    {
-            //        GridRoot.Focusable = true;
-            //        GridRoot.Focus();
-            //    }
-            //}
+            var uiElement = FocusManager.GetFocusedElement(this);
+            if (uiElement != null)
+            {
+                var textBox = uiElement as TextBox;
+                
+                //如果是下拉框本身的可输入文本框,则点击下拉框或者下拉选项时不失去焦点
+                var parent = CommonHelper.FindVisualParent<ComboBox>(textBox);
+                if (parent != null)
+                {
+                    var visual = VisualTreeHelper.HitTest(parent, e.GetPosition(parent));
+                    if (visual != null)
+                    {
+                        //点击下拉框展开按钮时 不失去焦点
+                        var item = CommonHelper.FindVisualParent<ComboBox>(visual.VisualHit);
+                        if (item != null)
+                        {
+                            return;
+                        }
+                    }
+
+                    //选择下拉选项时,返回,不失去焦点,否则下拉框的其他选项无法选中
+                    if(parent.IsDropDownOpen)
+                    {
+                        return;
+                    }
+                }
+
+                //点击其他非文本框区域时,失去焦点
+                if (textBox != null)
+                {
+                    GridRoot.Focusable = true;
+                    GridRoot.Focus();
+                }
+            }
         }
     }
 }

+ 5 - 3
PDF Office/Views/PageEdit/PageEditContent.xaml

@@ -145,6 +145,7 @@
         <KeyBinding Command="{Binding RightRotateCommand}" Gesture="Ctrl+R" />
         <KeyBinding Command="{Binding LeftRotateCommand}" Gesture="Ctrl+L" />
         <KeyBinding Command="{Binding DeleteCommand}" Gesture="Delete" />
+        <KeyBinding Command="{Binding ClearSelectedCommand}" Key="Esc"/>
         <KeyBinding
             Key="Add"
             Command="{Binding ZoomInCommand}"
@@ -153,9 +154,10 @@
             Key="Subtract"
             Command="{Binding ZoomOutCommand}"
             Modifiers="Ctrl" />
+        
     </UserControl.InputBindings>
 
-    <Border BorderBrush="#F2F2F2" BorderThickness="0,1,0,0">
+    <Border AllowDrop="True" BorderBrush="#F2F2F2" BorderThickness="0,1,0,0" Drop="Border_Drop">
         <Grid
             DragLeave="Grid_DragLeave"
             DragOver="Grid_DragOver"
@@ -186,7 +188,7 @@
                                     <i:InvokeCommandAction Command="{Binding KeyDown}" PassEventArgsToCommand="True" />
                                 </i:EventTrigger>
                                 <i:EventTrigger EventName="LostFocus">
-                                    <i:InvokeCommandAction Command="{Binding KeyDown}" PassEventArgsToCommand="True" />
+                                    <i:InvokeCommandAction Command="{Binding LostFocusCommand}" PassEventArgsToCommand="True" />
                                 </i:EventTrigger>
                             </i:Interaction.Triggers>
                         </ComboBox>
@@ -323,7 +325,7 @@
                 Name="ListPageEdit"
                 Grid.Row="1"
                 Padding="0"
-                AllowDrop="False"
+                AllowDrop="True"
                 BorderThickness="0"
                 Drop="ListPageEdit_Drop"
                 FocusVisualStyle="{x:Null}"

+ 46 - 21
PDF Office/Views/PageEdit/PageEditContent.xaml.cs

@@ -313,8 +313,9 @@ namespace PDF_Office.Views.PageEdit
         {
             try
             {
-                if (e.LeftButton == MouseButtonState.Pressed)
+                if (e.LeftButton == MouseButtonState.Pressed&&!isDragingEnter)
                 {
+                   
                     //鼠标框选逻辑
                     if (startChoose)
                     {
@@ -331,6 +332,7 @@ namespace PDF_Office.Views.PageEdit
                         DoSelectItems();
                         return;
                     }
+
                     //拖拽排序的逻辑
                     var pos = e.GetPosition(ListPageEdit);
                     if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
@@ -365,7 +367,9 @@ namespace PDF_Office.Views.PageEdit
                     System.IO.File.Create(tempPath);
                     string[] files = new string[1];
                     files[0] = tempPath;
-                    DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
+                    //禁用从窗体拖拽到桌面的方法
+                    //DataObject dataObj = new DataObject(DataFormats.FileDrop, files);
+                    DataObject dataObj = new DataObject(listBoxItem);
                     DragDrop.DoDragDrop(ListPageEdit, dataObj, DragDropEffects.Copy);
                     Mouse.Capture(ListPageEdit);
                     return;
@@ -531,6 +535,13 @@ namespace PDF_Office.Views.PageEdit
                 ImgPicture.Visibility = Visibility.Collapsed;
                 return;
             }
+
+            //拖入非法格式释放时 返回
+            string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
+            if (file != null)
+            {
+                return;
+            }
             #region 功能付费锁
             //    //if (!App.IsActive())
             //    //{
@@ -544,23 +555,24 @@ namespace PDF_Office.Views.PageEdit
             #region  从外部拖拽插入文件
             if (isDragingEnter)
             {
-                var files = (string[])e.Data.GetData(DataFormats.FileDrop);
-                Array.Reverse(files);
-                foreach(string file in files)
-                {
-                    System.IO.FileInfo info = new System.IO.FileInfo(file);
-                    if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
-                    {
-                        int index = InsertIndex == -1 ? 0 : InsertIndex;
-                        viewModel.InsertFromFile(index, file);
-                        viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
-                    }
-                }
-                //其他文件  则新增一个页签打开
-                //    DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个  Load(TPDFDocument)的接口
-                LineInset.Visibility = Visibility.Collapsed;
-                ImgPicture.Visibility = Visibility.Collapsed;
-                isDragingEnter = false;
+                //注释从外部拖拽插入文件的功能
+                //var files = (string[])e.Data.GetData(DataFormats.FileDrop);
+                //Array.Reverse(files);
+                //foreach(string file in files)
+                //{
+                //    System.IO.FileInfo info = new System.IO.FileInfo(file);
+                //    if(System.IO.Path.GetExtension(file).ToLower()==".pdf"&&info.Length>0)
+                //    {
+                //        int index = InsertIndex == -1 ? 0 : InsertIndex;
+                //        viewModel.InsertFromFile(index, file);
+                //        viewModel.ReloadAfterOption(true,true,new Tuple<int, int>(0,ListPageEdit.Items.Count));
+                //    }
+                //}
+                ////其他文件  则新增一个页签打开
+                ////    DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个  Load(TPDFDocument)的接口
+                //LineInset.Visibility = Visibility.Collapsed;
+                //ImgPicture.Visibility = Visibility.Collapsed;
+                //isDragingEnter = false;
                 return;
             }
             #endregion
@@ -652,6 +664,12 @@ namespace PDF_Office.Views.PageEdit
                 if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
                     return;
 
+                //从外部拖入文件时 返回
+                string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
+                if(file!=null)
+                {
+                    return;
+                }
 
 
                 //滚动后有 位置不准确 要减去滚动偏移量
@@ -660,6 +678,7 @@ namespace PDF_Office.Views.PageEdit
                 var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
                 if (result == null)
                 {
+                    return;
                     //MidLane.Visibility = Visibility.Collapsed;
                     //return;
                 }
@@ -859,7 +878,6 @@ namespace PDF_Office.Views.PageEdit
         /// <param name="e"></param>
         private void Grid_PreviewDragEnter(object sender, DragEventArgs e)
         {
-            DragDropHelper.DragEnter(this,e);
 
             var file = (System.Array)e.Data.GetData(DataFormats.FileDrop);
             if (file == null)//为null 表示内部拖动 触发的
@@ -871,11 +889,13 @@ namespace PDF_Office.Views.PageEdit
             {
                 System.IO.FileInfo info = new System.IO.FileInfo(f);
                 //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
-                if(System.IO.Path.GetExtension(f).ToLower()==".pdf" &&info.Length>0)
+                if (System.IO.Path.GetExtension(f).ToLower() == ".pdf" && info.Length > 0)
                 {
                     isDragingEnter = true;
                 }
             }
+
+            DragDropHelper.DragEnter(this, e);
         }
 
         private void Grid_DragLeave(object sender, DragEventArgs e)
@@ -894,5 +914,10 @@ namespace PDF_Office.Views.PageEdit
             //较大幅度拖动才能触发排序
             isDraging = true;
         }
+
+        private void Border_Drop(object sender, DragEventArgs e)
+        {
+
+        }
     }
 }

+ 2 - 1
PDF Office/Views/ViewContent.xaml.cs

@@ -242,7 +242,8 @@ namespace PDF_Office.Views
                 DragDropHelper.Drop(this, e);
 
                 string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
-                if (file.Length > 0)
+                //页面编辑模式下 不响应拖拽打开文件
+                if (file!=null  &&file.Length > 0&&!(this.DataContext as ViewContentViewModel).isInPageEdit)
                 {
                     App.mainWindowViewModel.AddTab.Execute();
                     (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).homeContentViewModel.AddFileFromDrag(file.ToList());