Browse Source

页面编辑-补充拖拽插入文件功能(底层库重复拿图崩溃问题待确认)

ZhouJieSheng 2 năm trước cách đây
mục cha
commit
a0853cc0aa

+ 104 - 0
PDF Office/Helper/DragDropHelper.cs

@@ -0,0 +1,104 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Interop;
+
+namespace PDF_Office.Helper
+{
+    /// <summary>
+    /// 显示拖拽时图片的辅助类
+    /// </summary>
+    public static class DragDropHelper
+    {
+        private static IDropTargetHelper helper;
+
+        /// <summary>
+        /// 拖拽相关操作的实例
+        /// </summary>
+        public static IDropTargetHelper Helper
+        {
+            get
+            {
+                if (helper == null)
+                {
+                    helper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
+                }
+                return helper;
+            }
+            set { helper = value; }
+        }
+
+
+        public static void DragEnter(FrameworkElement element, DragEventArgs e)
+        {
+            try
+            {
+                Point iconPoint = e.GetPosition(element);
+                Helper?.DragEnter(new WindowInteropHelper(Window.GetWindow(element)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
+            }
+            catch { }
+        }
+
+        public static void DragOver(FrameworkElement element, DragEventArgs e)
+        {
+            try
+            {
+                Point iconPoint = e.GetPosition(element);
+                Helper?.DragOver(ref iconPoint, e.Effects);
+            }
+            catch { }
+        }
+
+        public static void DragLeave()
+        {
+            try
+            {
+                Helper?.DragLeave();
+            }
+            catch { }
+        }
+
+        public static void Drop(FrameworkElement element, DragEventArgs e)
+        {
+            try
+            {
+                Point iconPoint = e.GetPosition(element);
+                Helper?.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
+            }
+            catch { }
+        }
+    }
+
+    /// <summary>
+    /// 系统设置拖拽时图片的接口
+    /// </summary>
+    [ComImport]
+    [GuidAttribute("4657278B-411B-11d2-839A-00C04FD918D0")]
+    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
+    public interface IDropTargetHelper
+    {
+        // Notifies the drag-image manager that the drop target's IDropTarget::DragEnter method has been called  
+        [PreserveSig]
+        Int32 DragEnter(IntPtr hwndTarget, System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref Point ppt, DragDropEffects dwEffect);
+
+        // Notifies the drag-image manager that the drop target's IDropTarget::DragLeave method has been called  
+        [PreserveSig]
+        Int32 DragLeave();
+
+        // Notifies the drag-image manager that the drop target's IDropTarget::DragOver method has been called  
+        [PreserveSig]
+        Int32 DragOver(ref Point ppt, DragDropEffects dwEffect);
+
+        // Notifies the drag-image manager that the drop target's IDropTarget::Drop method has been called  
+        [PreserveSig]
+        Int32 Drop(System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref Point ppt, DragDropEffects dwEffect);
+
+        // Notifies the drag-image manager to show or hide the drag image  
+        [PreserveSig]
+        Int32 Show(bool fShow);
+    }
+}

+ 1 - 0
PDF Office/PDF Office.csproj

@@ -232,6 +232,7 @@
     <Compile Include="Helper\ConverterHelper.cs" />
     <Compile Include="Helper\AdvancedInvokeCommandAction.cs" />
     <Compile Include="Helper\ArrowHelper.cs" />
+    <Compile Include="Helper\DragDropHelper.cs" />
     <Compile Include="Helper\HomePageEditHelper.cs" />
     <Compile Include="Helper\PasswordBoxHelper.cs" />
     <Compile Include="Helper\SDKLisenceHelper.cs" />

+ 5 - 6
PDF Office/ViewModels/PageEdit/PageEditContentViewModel.cs

@@ -527,7 +527,7 @@ namespace PDF_Office.ViewModels.PageEdit
                 if ((bool)dialog.ShowDialog())
                 {
                     //插入文件
-                   var result = await InsertFromFile(insertIndex,dialog.FileName);
+                    var result = await InsertFromFile(insertIndex,dialog.FileName);
                     if(!result)
                     {
                         IsLoading = Visibility.Collapsed;
@@ -911,7 +911,7 @@ namespace PDF_Office.ViewModels.PageEdit
         /// <summary>
         /// 页面编辑执行Redo操作的方法
         /// </summary>
-        private void Redo()
+        private async void Redo()
         {
             var history = RedoStack.Pop();
             PageEditHistory undoHistory = new PageEditHistory();
@@ -932,9 +932,8 @@ namespace PDF_Office.ViewModels.PageEdit
                     undoHistory.Type = ActionType.RightRotate;
                     break;
                 case ActionType.InsertFromFile:
-                    InsertFromFile(history.InsertIndex,history.InsertFilePath,history.InsertFilePassWord,true);
-                    //刷新图片
-                    ReloadAfterOption(true, true, Tuple.Create(history.InsertIndex - 1, history.InsertIndex+history.InsertPageCount));
+                    await InsertFromFile(history.InsertIndex,history.InsertFilePath,history.InsertFilePassWord,true);
+                    ReloadAfterOption(true, true, Tuple.Create(history.InsertIndex-1, history.InsertIndex+history.InsertPageCount));
                     undoHistory = history;
                     break;
                 case ActionType.InsertCustomPages:
@@ -1470,7 +1469,7 @@ namespace PDF_Office.ViewModels.PageEdit
         /// <param name="needRefreshPageNum">是否需要刷新页码</param>
         /// <param name="needClearUndoHistory">是否需要清空注释Undo栈</param>
         /// <param name="pageRange">需要刷新的页面范围图片</param>
-        private void ReloadAfterOption(bool needRefreshPageNum = false, bool needClearUndoHistory = true, Tuple<int, int> pageRange = null)
+        public void ReloadAfterOption(bool needRefreshPageNum = false, bool needClearUndoHistory = true, Tuple<int, int> pageRange = null)
         {
             if (needRefreshPageNum)
             {

+ 7 - 2
PDF Office/Views/PageEdit/PageEditContent.xaml

@@ -28,7 +28,7 @@
             <convert:BoolToVisible x:Key="BoolToVisibleConvert" />
             <convert:UnVisivleConvert x:Key="UnVisibleConvert" />
             <DataTemplate x:Key="PageEditListBoxItemTemplate" DataType="{x:Type pageedit:PageEditItem}">
-                <StackPanel>
+                <StackPanel AllowDrop="True">
                     <Border
                         Name="BdBorder"
                         VerticalAlignment="Top"
@@ -140,7 +140,11 @@
     </UserControl.Resources>
 
     <Border BorderBrush="#F2F2F2" BorderThickness="0,1,0,0">
-        <Grid DragOver="Grid_DragOver">
+        <Grid
+            DragLeave="Grid_DragLeave"
+            DragOver="Grid_DragOver"
+            Drop="Grid_Drop"
+            PreviewDragEnter="Grid_PreviewDragEnter">
             <Grid.RowDefinitions>
                 <RowDefinition Height="auto" />
                 <RowDefinition />
@@ -342,6 +346,7 @@
                         <Setter Property="HorizontalContentAlignment" Value="Center" />
                         <Setter Property="VerticalContentAlignment" Value="Center" />
                         <Setter Property="ContextMenu" Value="{StaticResource ListBoxItemMenu}" />
+                        <EventSetter Event="DragLeave" Handler="ListBoxItem_DragLeave"/>
                         <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown" />
                         <EventSetter Event="PreviewMouseDoubleClick" Handler="ListBoxItem_PreviewMouseDoubleClick" />
                     </Style>

+ 120 - 234
PDF Office/Views/PageEdit/PageEditContent.xaml.cs

@@ -75,18 +75,13 @@ namespace PDF_Office.Views.PageEdit
         //拖动的Item
         private PageEditItem tempItem;
 
-        ///鼠标是否停留在item前半部
-        ///显示在前半部时,获取的index为实际索引值
-        ///显示在后半部时,获取的index需要+1
-        private bool isFrontHalf = false;
-
         /// <summary>
         /// 是否正在拖拽排序中,通过该变量避免单击触发拖动
         /// </summary>
         private bool isDraging = false;
 
         //是否正在从外部拖入文件
-        private bool isDragingEnter = false;
+        public bool isDragingEnter { get; set; } = false;
 
         /// <summary>
         /// 是否需要自动滚动
@@ -105,7 +100,7 @@ namespace PDF_Office.Views.PageEdit
         }
 
 
-        public PageEditContent(IEventAggregator eventAggregator) :this()
+        public PageEditContent(IEventAggregator eventAggregator) : this()
         {
             eventor = eventAggregator;
             unicode = App.mainWindowViewModel.SelectedItem.Unicode;
@@ -153,15 +148,15 @@ namespace PDF_Office.Views.PageEdit
         }
 
 
-            #region UI事件
-            /// <summary>
-            /// 每次显示的时候就触发事件,刷新所有图片
-            /// </summary>
-            /// <param name="sender"></param>
-            /// <param name="e"></param>
-            private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
+        #region UI事件
+        /// <summary>
+        /// 每次显示的时候就触发事件,刷新所有图片
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
         {
-            if((bool)e.NewValue)
+            if ((bool)e.NewValue)
             {
                 //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
                 PulishEvent();
@@ -287,7 +282,7 @@ namespace PDF_Office.Views.PageEdit
         /// <param name="e"></param>
         private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
         {
-            if((bool)e.NewValue)
+            if ((bool)e.NewValue)
             {
                 (sender as Control).Focus();
             }
@@ -320,6 +315,7 @@ namespace PDF_Office.Views.PageEdit
                     if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
                     {
                         LineInset.Visibility = Visibility.Collapsed;
+                        ImgPicture.Visibility = Visibility.Collapsed;
                         return;
                     }
                     HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
@@ -359,7 +355,8 @@ namespace PDF_Office.Views.PageEdit
             }
             catch
             {
-               LineInset.Visibility = Visibility.Collapsed;
+                LineInset.Visibility = Visibility.Collapsed;
+                ImgPicture.Visibility = Visibility.Collapsed;
             }
         }
 
@@ -425,7 +422,7 @@ namespace PDF_Office.Views.PageEdit
 
             //更改系统默认的选中规则,多选后,鼠标单击抬起后再选中单个
             //拖拽框选之后的抬起鼠标不进入处理
-            if (!startChoose&&!Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
+            if (!startChoose && !Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.LeftShift))
             {
                 ListPageEdit.SelectedItems.Clear();
                 ListPageEdit.SelectedItem = listBoxItem;
@@ -488,152 +485,13 @@ namespace PDF_Office.Views.PageEdit
             return;
         }
 
-        /// <summary>
-        /// 计算插入标记线和虚影显示
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private void ListPageEdit_DragOver(object sender, DragEventArgs e)
-        {
-            try
-            {
-                //ctrl建按下 不显示插入标记和虚影  因为释放不会响应drop事件
-                if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
-                    return;
-
-                //滚动后有 位置不准确 要减去滚动偏移量
-                //控制线的位置
-                var pos = e.GetPosition(ListPageEdit);
-                var result = VisualTreeHelper.HitTest(ListPageEdit, pos);
-                if (result == null)
-                {
-                    //MidLane.Visibility = Visibility.Collapsed;
-                    //return;
-                }
-
-                //获取当前鼠标指针下的容器
-                var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
-                if (listBoxItem == null)
-                {
-                    //MidLane.Visibility = Visibility.Collapsed;
-                    //return;
-                }
-
-                #region  计算虚影位置
-                //xaml层 要设置 虚影控件为左上
-                double xPos = 0;
-                double yPos = 0;
-                //内部拖动
-                if (!isDragingEnter)
-                {
-                    //Viewbox viewBox = (tempItem.Content as StackPanel).Children[0] as Viewbox;//获取item宽度
-
-                    ImgPicture.Width = tempItem.ItemSize.Width;
-                    ImgPicture.Height = tempItem.ItemSize.Height;
-                    ImgPicture.Source = tempItem.Image;
-                    xPos = e.GetPosition(ListPageEdit).X - item_x;
-                    yPos = e.GetPosition(ListPageEdit).Y - item_y;
-                }
-                //else
-                //{
-                //    //从外部拖入的逻辑
-                //    //var pic = ToBitmapSource(dragingEnterPath);
-                //    //ShadowPicture.Width = pic.Width;
-                //    //ShadowPicture.Height = pic.Height;
-                //    //ShadowPicture.Source = pic;
-                //    //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
-                //    //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
-                //}
-
-                ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
-                #endregion
-
-                #region 计算插入标记位置
-                var scroll = GetScrollHost(ListPageEdit);
-                if (listBoxItem != null)
-                {
-                    //虚拟化影响到该值计算
-                    var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
-                    LineInset.Visibility = Visibility.Visible;
-
-                    var panel = GetWrapPanel(ListPageEdit);
-                    var item = (ListPageEdit.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).DesiredSize.Width;
-
-                    int count = (int)(panel.ViewportWidth / item);
-                    var gap = (panel.ViewportWidth - count * item) / (count + 1) * 1.0;
-
-                    LineInset.X2 = LineInset.X1 = p.X + gap / 2 + listBoxItem.DesiredSize.Width;
-
-                    if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
-                    {
-                        isFrontHalf = true;//前半部 线条出现在位置前 
-                        LineInset.X2 = LineInset.X1 = p.X - gap / 2;
-                        InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem);
-                    }
-                    else
-                    {
-                        isFrontHalf = false;
-                        InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem) + 1;
-                    }
-                    //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
-                    LineInset.Y1 = p.Y;
-                    if (LineInset.Y1 < 0)//避免超出上边界
-                    {
-                        LineInset.Y1 = 0;
-                    }
-                    //MidLane.Y2 = p.Y + listBoxItem.ActualHeight - scroll.VerticalOffset;//仿智能滚动后可能会导致 垂直滚动偏量不准确
-                    LineInset.Y2 = p.Y + listBoxItem.ActualHeight;
-                    if (LineInset.Y2 < 0)
-                    {
-                        LineInset.Y2 = 0;
-                    }
-                }
-                #endregion
-
-                //暂时处理  鼠标移出边框时,虚影的显示问题
-                if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
-                {
-                    LineInset.Visibility = Visibility.Collapsed;
-                    needScroll = false;
-                }
-
-                if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
-                {
-                    LineInset.Visibility = Visibility.Collapsed;
-                    needScroll = false;
-                }
-
-                #region 靠近上下边界时,自动滚动,离边界越近,滚动速度越快
-                //speed = 0;
-                //if (pos.Y >= PageEditListBox.ActualHeight - 30)
-                //{
-                //    speed = 30 - (int)(PageEditListBox.ActualHeight - pos.Y);
-                //    needScroll = true;
-                //}
-                //else if (pos.Y <= 30)
-                //{
-                //    speed = (int)(pos.Y - 30);
-                //    needScroll = true;
-                //}
-                //else
-                //    needScroll = false;
-
-                //var v = scroll.VerticalOffset;
-                //scroll.ScrollToVerticalOffset(v + speed);//触发连续滚动
-                #endregion
-            }
-            catch (Exception ex)
-            {
-
-            }
-        }
-
         /// <summary>
         /// 退出拖拽模式
         /// </summary>
         private void ExitDraging()
         {
             LineInset.Visibility = Visibility.Collapsed;
+            ImgPicture.Visibility = Visibility.Collapsed;
             isDraging = false;
         }
 
@@ -642,13 +500,14 @@ namespace PDF_Office.Views.PageEdit
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
-        private void ListPageEdit_Drop(object sender, DragEventArgs e)
+        private async void ListPageEdit_Drop(object sender, DragEventArgs e)
         {
             needScroll = false;
             if (!isDraging)
             {
                 //未拖拽时隐藏插入标记和虚影
                 LineInset.Visibility = Visibility.Collapsed;
+                ImgPicture.Visibility = Visibility.Collapsed;
                 return;
             }
             #region 功能付费锁
@@ -662,56 +521,27 @@ namespace PDF_Office.Views.PageEdit
             #endregion
 
             #region  从外部拖拽插入文件
-            //if (isDragingEnter)
-            //{
-
-
-            //    //在当前位置插入整个pdf
-            //    CPDFDocument dragDoc = CPDFDocument.InitWithFilePath(dragingEnterPath);
-            //    if (dragDoc.IsLocked)
-            //    {
-            //        VerifyPasswordDialog dialog = new VerifyPasswordDialog(dragDoc);
-            //        dialog.ShowDialog();
-            //        if (dragDoc.IsLocked)
-            //            return;
-            //    }
-            //    if (dragingEnterPath.Substring(dragingEnterPath.LastIndexOf(".")).ToLower() == ".pdf")
-            //    {
-            //        if (dragDoc != null)
-            //        {
-            //            int index = InsertIndex == -1 ? 0 : InsertIndex;
-            //            pdfViewer.Document.ImportPagesAtIndex(dragDoc, "1-" + dragDoc.PageCount, index);
-            //            PopulateThumbnailList();
-            //            ItemsInViewHitTest();
-
-            //            pdfViewer.UndoManager.ClearHistory();
-            //            pdfViewer.UndoManager.CanSave = true;
-            //            pdfViewer.ReloadDocument();
-            //            PageEditListBox.ScrollIntoView(PageEditListBox.SelectedItem as ListBoxItem);
-            //            PageMoved.Invoke(this, new RoutedEventArgs());
-            //            PDFViewerCtrl viewerCtrl = ParentPage?.GetCurrentViewer();
-            //            if (viewerCtrl != null)
-            //            {
-            //                viewerCtrl.IsPageEdit = true;
-            //            }
-            //            dragDoc.Release();
-            //        }
-            //        else
-            //        {
-            //            MessageBoxEx.Show(App.MainPageLoader.GetString("Merge_FileCannotOpenedWarningd"));
-            //        }
-            //        //提示文档损坏无法打开
-            //    }
-            //    else if (!string.IsNullOrEmpty(dragingEnterPath))
-            //    {
-            //        //其他文件  则新增一个页签打开
-            //        //    DragAddTab.Invoke(dragingEnterPath, new RoutedEventArgs());//底层库需要加一个  Load(TPDFDocument)的接口
-            //    }
-            //    MidLane.Visibility = Visibility.Collapsed;
-            //    isDragingEnter = false;
-            //    dragingEnterPath = null;
-            //    return;
-            //}
+            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;
+                return;
+            }
             #endregion
 
             var pos = e.GetPosition(ListPageEdit);
@@ -851,6 +681,8 @@ namespace PDF_Office.Views.PageEdit
                 if (e.KeyStates == (DragDropKeyStates.ControlKey | DragDropKeyStates.LeftMouseButton) || e.KeyStates == (DragDropKeyStates.ShiftKey | DragDropKeyStates.LeftMouseButton | DragDropKeyStates.ControlKey))
                     return;
 
+
+
                 //滚动后有 位置不准确 要减去滚动偏移量
                 //控制线的位置
                 var pos = e.GetPosition(ListPageEdit);
@@ -876,24 +708,26 @@ namespace PDF_Office.Views.PageEdit
                 //内部拖动
                 if (!isDragingEnter)
                 {
-                    //Viewbox viewBox = (tempItem.Content as StackPanel).Children[0] as Viewbox;//获取item宽度
-
-                    ImgPicture.Width = tempItem.ItemSize.Width;
-                    ImgPicture.Height = tempItem.ItemSize.Height;
-                    ImgPicture.Source = tempItem.Image;
-                    xPos = e.GetPosition(ListPageEdit).X - item_x;
-                    yPos = e.GetPosition(ListPageEdit).Y - item_y;
+                    if(tempItem!=null)
+                    {
+                        ImgPicture.Width = tempItem.ItemSize.Width;
+                        ImgPicture.Height = tempItem.ItemSize.Height;
+                        ImgPicture.Source = tempItem.Image;
+                        xPos = e.GetPosition(ListPageEdit).X - item_x;
+                        yPos = e.GetPosition(ListPageEdit).Y - item_y;
+                    }
+                }
+                else
+                {
+                    DragDropHelper.DragOver(this, e);
+                    //从外部拖入的逻辑
+                    //var pic = ToBitmapSource(dragingEnterPath);
+                    //ShadowPicture.Width = pic.Width;
+                    //ShadowPicture.Height = pic.Height;
+                    //ShadowPicture.Source = pic;
+                    //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
+                    //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
                 }
-                //else
-                //{
-                //    //从外部拖入的逻辑
-                //    //var pic = ToBitmapSource(dragingEnterPath);
-                //    //ShadowPicture.Width = pic.Width;
-                //    //ShadowPicture.Height = pic.Height;
-                //    //ShadowPicture.Source = pic;
-                //    //xPos = e.GetPosition(ListPageEdit).X - pic.Width / 2;
-                //    //yPos = e.GetPosition(ListPageEdit).Y - pic.Height / 2;
-                //}
 
                 ImgPicture.Margin = new Thickness(xPos, yPos, 0, 0);
                 #endregion
@@ -905,6 +739,14 @@ namespace PDF_Office.Views.PageEdit
                     //虚拟化影响到该值计算
                     var p = VisualTreeHelper.GetOffset(listBoxItem);//计算控件在容器中的偏移(位置)
                     LineInset.Visibility = Visibility.Visible;
+                    if(!isDragingEnter)
+                    {
+                        ImgPicture.Visibility = Visibility.Visible;
+                    }
+                    else
+                    {
+                        ImgPicture.Visibility = Visibility.Collapsed;
+                    }
 
                     var panel = GetWrapPanel(ListPageEdit);
 
@@ -919,15 +761,14 @@ namespace PDF_Office.Views.PageEdit
 
                     if (pos.X < p.X + gap / 2 + listBoxItem.ActualWidth / 2)
                     {
-                        isFrontHalf = true;//前半部 线条出现在位置前 
                         LineInset.X2 = LineInset.X1 = p.X - gap / 2;
-                        InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem);
+                        InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem);
                     }
                     else
                     {
-                        isFrontHalf = false;
-                        InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem) + 1;
+                        InsertIndex = ListPageEdit.Items.IndexOf(listBoxItem.DataContext as PageEditItem) + 1;
                     }
+                    Console.WriteLine("InsertIndex:{0}\r\n",InsertIndex);
                     //MidLane.Y1 = p.Y - scroll.VerticalOffset;//向下滑动后要减去滑动值
                     LineInset.Y1 = p.Y;
                     if (LineInset.Y1 < 0)//避免超出上边界
@@ -947,12 +788,14 @@ namespace PDF_Office.Views.PageEdit
                 if (pos.Y <= 30 || pos.Y >= ListPageEdit.ActualHeight - 10)
                 {
                     LineInset.Visibility = Visibility.Collapsed;
+                    ImgPicture.Visibility = Visibility.Collapsed;
                     needScroll = false;
                 }
 
                 if (pos.X <= 40 || pos.X >= scroll.ViewportWidth - 50)
                 {
                     LineInset.Visibility = Visibility.Collapsed;
+                    ImgPicture.Visibility = Visibility.Collapsed;
                     needScroll = false;
                 }
 
@@ -983,14 +826,14 @@ namespace PDF_Office.Views.PageEdit
 
         private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
-            
+
             //e.Handled = true;
         }
 
         private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
         {
-            if(e.ChangedButton== MouseButton.Left)
-            {         
+            if (e.ChangedButton == MouseButton.Left)
+            {
                 //双击回到PDFViewer界面
                 var item = sender as ListBoxItem;
                 if (item != null)
@@ -1024,7 +867,7 @@ namespace PDF_Office.Views.PageEdit
             {
                 var itemwidth = 0.6 * width;
                 var itemheight = 294 * itemwidth / 208.0;
-                viewModel.ChangeItemSize(new Size(itemwidth,itemheight));
+                viewModel.ChangeItemSize(new Size(itemwidth, itemheight));
             }
         }
 
@@ -1032,5 +875,48 @@ namespace PDF_Office.Views.PageEdit
         {
             ItemSuitAcutalWidth(this.ActualWidth);
         }
+
+        /// <summary>
+        /// 用来判断是否有外界拖入的事件
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <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 表示内部拖动 触发的
+            { 
+                return; 
+            }
+            isDragingEnter = false;
+            foreach (string f in file)
+            {
+                System.IO.FileInfo info = new System.IO.FileInfo(f);
+                //只要拖拽进来的文件里包含有pdf格式文件,就允许拖入
+                if(System.IO.Path.GetExtension(f).ToLower()==".pdf" &&info.Length>0)
+                {
+                    isDragingEnter = true;
+                }
+            }
+        }
+
+        private void Grid_DragLeave(object sender, DragEventArgs e)
+        {
+            DragDropHelper.DragLeave();
+        }
+
+        private void Grid_Drop(object sender, DragEventArgs e)
+        {
+            DragDropHelper.Drop(this,e);
+        }
+
+        private void ListBoxItem_DragLeave(object sender, DragEventArgs e)
+        {
+            //增加辅助判断,防止误触发 拖动排序
+            //较大幅度拖动才能触发排序
+            isDraging = true;
+        }
     }
 }