Pārlūkot izejas kodu

ComPDFKit.Tool(win)-补充设置选中矩形可以拖至页面区域外逻辑

liyijie 7 mēneši atpakaļ
vecāks
revīzija
10ded71552

+ 11 - 0
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.SelectedRect.cs

@@ -19,6 +19,7 @@ namespace ComPDFKit.Tool
     {
     {
         bool isDrawSelectRect = false;
         bool isDrawSelectRect = false;
         int selectedRectViewTag = -1;
         int selectedRectViewTag = -1;
+        private bool isOutSideScaling = false;
         public event EventHandler<SelectedAnnotData> SelectedDataChanging;
         public event EventHandler<SelectedAnnotData> SelectedDataChanging;
         public event EventHandler<SelectedAnnotData> SelectedDataChanged;
         public event EventHandler<SelectedAnnotData> SelectedDataChanged;
 
 
@@ -35,6 +36,15 @@ namespace ComPDFKit.Tool
             selectedRectViewTag= customizeLayer.GetResTag();
             selectedRectViewTag= customizeLayer.GetResTag();
         }
         }
 
 
+        /// <summary>
+        /// Set whether the border can be moved outside the border
+        /// </summary>
+        /// <param name="IsOutSideScaling"></param>
+        public void SetOutSideScaling(bool IsOutSideScaling)
+        {
+            isOutSideScaling = IsOutSideScaling;
+        }
+
         private void SelectedRect_DataChanging(object sender, SelectedAnnotData e)
         private void SelectedRect_DataChanging(object sender, SelectedAnnotData e)
         {
         {
             SelectedDataChanging?.Invoke(this, e);
             SelectedDataChanging?.Invoke(this, e);
@@ -87,6 +97,7 @@ namespace ComPDFKit.Tool
             SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
             SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
             if (selectedRect != null)
             if (selectedRect != null)
             {
             {
+                selectedRect.SetOutSideScaling(isOutSideScaling);
                 selectedRect.OnMouseMove(point, out DrawTag,PDFViewer.ActualWidth,PDFViewer.ActualHeight);
                 selectedRect.OnMouseMove(point, out DrawTag,PDFViewer.ActualWidth,PDFViewer.ActualHeight);
                 selectedRect.Draw();
                 selectedRect.Draw();
             }
             }

+ 1 - 0
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.TextEdit.cs

@@ -525,6 +525,7 @@ namespace ComPDFKit.Tool
                 }
                 }
                 else
                 else
                 {
                 {
+                    selectedRect.SetOutSideScaling(isOutSideScaling);
                     selectedRect.OnMouseMove(point, out bool Tag, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
                     selectedRect.OnMouseMove(point, out bool Tag, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
                     if (selectedEditAreaIndex == cropIndex)
                     if (selectedEditAreaIndex == cropIndex)
                     {
                     {

+ 0 - 2
Demo/Examples/ComPDFKit.Tool/DrawTool/SelectedRect.cs

@@ -453,8 +453,6 @@ namespace ComPDFKit.Tool.DrawTool
             {
             {
                 this.editHoverPen = editHoverPen;
                 this.editHoverPen = editHoverPen;
             }
             }
-
-
         }
         }
 
 
         public virtual void ClearDraw()
         public virtual void ClearDraw()

+ 337 - 8
Demo/Examples/ComPDFKit.Tool/DrawTool/SelectedRect.protected.cs

@@ -12,8 +12,11 @@ namespace ComPDFKit.Tool.DrawTool
         /// <summary>
         /// <summary>
         /// Current control point drawing style.
         /// Current control point drawing style.
         /// </summary>
         /// </summary>
-        protected DrawPointType currentDrawPointType { get; 
+        protected DrawPointType currentDrawPointType
-            set; }
+        {
+            get;
+            set;
+        }
 
 
 
 
         /// <summary>
         /// <summary>
@@ -99,12 +102,14 @@ namespace ComPDFKit.Tool.DrawTool
         /// <summary>
         /// <summary>
         /// Current drawing rectangle (calculated during operation).
         /// Current drawing rectangle (calculated during operation).
         /// </summary>
         /// </summary>
-        protected  Thickness clipThickness = new Thickness(0, 0, 0, 0);
+        protected Thickness clipThickness = new Thickness(0, 0, 0, 0);
 
 
         private Pen editPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
         private Pen editPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
 
 
         private Pen editHoverPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
         private Pen editHoverPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
 
 
+        protected bool isOutSideScaling = false;
+
         /// <summary>
         /// <summary>
         /// Current actual display width and height of PDFVIewer.
         /// Current actual display width and height of PDFVIewer.
         /// </summary>
         /// </summary>
@@ -147,7 +152,8 @@ namespace ComPDFKit.Tool.DrawTool
         }
         }
 
 
         protected List<Point> GetControlPoint(Rect currentRect)
         protected List<Point> GetControlPoint(Rect currentRect)
-        { List<Point> controlCurrentPoints = new List<Point>();
+        {
+            List<Point> controlCurrentPoints = new List<Point>();
             controlCurrentPoints.Clear();
             controlCurrentPoints.Clear();
             int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
             int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
             int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
             int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
@@ -226,9 +232,22 @@ namespace ComPDFKit.Tool.DrawTool
             {
             {
                 return false;
                 return false;
             }
             }
-            return NormalScaling(mousePoint);
+            if (!isOutSideScaling)
+            {
+                return NormalScaling(mousePoint);
+            }
+            else
+            {
+                return OutSideScaling(mousePoint);
+            }
         }
         }
 
 
+        public void SetOutSideScaling(bool IsOutSideScaling)
+        {
+            isOutSideScaling = IsOutSideScaling;
+        }
+
+
         private Size GetProportionalScalingSize(double width, double height)
         private Size GetProportionalScalingSize(double width, double height)
         {
         {
             double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
             double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
@@ -542,6 +561,316 @@ namespace ComPDFKit.Tool.DrawTool
             return false;
             return false;
         }
         }
 
 
+        /// <summary>
+        /// Provisional logic, to be further improved, not yet used: Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
+        /// </summary>
+        /// <param name="mousePoint">Current mouse position.</param>
+        /// <returns></returns>
+        protected bool OutSideScaling(Point mousePoint)
+        {
+            try
+            {
+                double left = 0, right = 0, top = 0, bottom = 0;
+                double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
+                double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
+
+                Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
+                Point moveVector = (Point)(mousePoint - centerPoint);
+                moveVector = ProportionalScalingOffsetPos(moveVector);
+
+                switch (hitControlType)
+                {
+                    case PointControlType.LeftTop:
+                        {
+                            left = centerPoint.X + moveVector.X;
+                            right = cacheRect.Right;
+                            top = centerPoint.Y + moveVector.Y;
+                            bottom = cacheRect.Bottom;
+                            if (isProportionalScaling)
+                            {
+                                Size size = GetProportionalScalingSize(right - left, bottom - top);
+                                left = right - size.Width;
+                                top = bottom - size.Height;
+                                if (left < maxRect.Left)
+                                {
+                                    double tmpWidth = right - left;
+                                    left = maxRect.Left;
+                                    double width = right - left;
+                                    double height = (bottom - top) * width / tmpWidth;
+                                    top = bottom - height;
+                                }
+
+                                if (top < maxRect.Top)
+                                {
+                                    double tmpHeight = bottom - top;
+                                    top = maxRect.Top;
+                                    double height = bottom - top;
+                                    double width = (right - left) * height / tmpHeight;
+                                    left = right - width;
+                                }
+                            }
+                            else
+                            {
+                                if (left + minWidth > right)
+                                {
+                                    left = right - minWidth;
+                                }
+
+                                if (top + minHeight > bottom)
+                                {
+                                    top = bottom - minHeight;
+                                }
+                            }
+                        }
+                        break;
+
+                    case PointControlType.LeftMiddle:
+                        {
+                            left = centerPoint.X + moveVector.X;
+                            right = cacheRect.Right;
+                            top = cacheRect.Top;
+                            bottom = cacheRect.Bottom;
+                            if (left + minWidth > right)
+                            {
+                                left = right - minWidth;
+                            }
+                        }
+                        break;
+
+                    case PointControlType.LeftBottom:
+                        {
+                            left = centerPoint.X + moveVector.X;
+                            right = cacheRect.Right;
+                            top = cacheRect.Top;
+                            bottom = centerPoint.Y + moveVector.Y;
+                            if (isProportionalScaling)
+                            {
+                                Size size = GetProportionalScalingSize(right - left, bottom - top);
+                                left = right - size.Width;
+                                bottom = top + size.Height;
+                                if (left < maxRect.Left)
+                                {
+                                    double tmpWidth = right - left;
+                                    left = maxRect.Left;
+                                    double width = right - left;
+                                    double height = (bottom - top) * width / tmpWidth;
+                                    bottom = top + height;
+                                }
+
+                                if (bottom > maxRect.Bottom)
+                                {
+                                    double tmpHeight = bottom - top;
+                                    bottom = maxRect.Bottom;
+                                    double height = bottom - top;
+                                    double width = (right - left) * height / tmpHeight;
+                                    left = right - width;
+                                }
+                            }
+                            else
+                            {
+                                if (left + minWidth > right)
+                                {
+                                    left = right - minWidth;
+                                }
+
+                                if (top + minHeight > bottom)
+                                {
+                                    bottom = top + minHeight;
+                                }
+                            }
+                        }
+                        break;
+
+                    case PointControlType.MiddlBottom:
+                        {
+                            left = cacheRect.Left;
+                            right = cacheRect.Right;
+                            top = cacheRect.Top;
+                            bottom = centerPoint.Y + moveVector.Y;
+                            if (top + minHeight > bottom)
+                            {
+                                bottom = top + minHeight;
+                            }
+                        }
+                        break;
+
+                    case PointControlType.RightBottom:
+                        {
+                            left = cacheRect.Left;
+                            right = centerPoint.X + moveVector.X;
+                            top = cacheRect.Top;
+                            bottom = centerPoint.Y + moveVector.Y;
+                            if (isProportionalScaling)
+                            {
+                                Size size = GetProportionalScalingSize(right - left, bottom - top);
+                                right = left + size.Width;
+                                bottom = top + size.Height;
+                                if (right > maxRect.Right)
+                                {
+                                    double tmpWidth = right - left;
+                                    right = maxRect.Right;
+                                    double width = right - left;
+                                    double height = (bottom - top) * width / tmpWidth;
+                                    bottom = top + height;
+                                }
+
+                                if (bottom > maxRect.Bottom)
+                                {
+                                    double tmpHeight = bottom - top;
+                                    bottom = maxRect.Bottom;
+                                    double height = bottom - top;
+                                    double width = (right - left) * height / tmpHeight;
+                                    right = left + width;
+                                }
+                            }
+                            else
+                            {
+                                if (left + minWidth > right)
+                                {
+                                    right = left + minWidth;
+                                }
+
+                                if (top + minHeight > bottom)
+                                {
+                                    bottom = top + minHeight;
+                                }
+                            }
+                        }
+                        break;
+
+                    case PointControlType.RightMiddle:
+                        {
+                            left = cacheRect.Left;
+                            right = centerPoint.X + moveVector.X;
+                            top = cacheRect.Top;
+                            bottom = cacheRect.Bottom;
+                            if (left + minWidth > right)
+                            {
+                                right = left + minWidth;
+                            }
+                        }
+                        break;
+
+                    case PointControlType.RightTop:
+                        {
+                            left = cacheRect.Left;
+                            right = centerPoint.X + moveVector.X;
+                            top = centerPoint.Y + moveVector.Y;
+                            bottom = cacheRect.Bottom;
+                            if (isProportionalScaling)
+                            {
+                                Size size = GetProportionalScalingSize(right - left, bottom - top);
+                                right = left + size.Width;
+                                top = bottom - size.Height;
+                                if (right > maxRect.Right)
+                                {
+                                    double tmpWidth = right - left;
+                                    right = maxRect.Right;
+                                    double width = right - left;
+                                    double height = (bottom - top) * width / tmpWidth;
+                                    top = bottom - height;
+                                }
+
+                                if (top < maxRect.Top)
+                                {
+                                    double tmpHeight = bottom - top;
+                                    top = maxRect.Top;
+                                    double height = bottom - top;
+                                    double width = (right - left) * height / tmpHeight;
+                                    right = left + width;
+                                }
+                            }
+                            else
+                            {
+                                if (left + minWidth > right)
+                                {
+                                    right = left + minWidth;
+                                }
+
+                                if (top + minHeight > bottom)
+                                {
+                                    top = bottom - minHeight;
+                                }
+                            }
+                        }
+                        break;
+
+                    case PointControlType.MiddleTop:
+                        {
+                            left = cacheRect.Left;
+                            right = cacheRect.Right;
+                            top = centerPoint.Y + moveVector.Y;
+                            bottom = cacheRect.Bottom;
+                            if (top + minHeight > bottom)
+                            {
+                                top = bottom - minHeight;
+                            }
+                        }
+                        break;
+
+                    case PointControlType.Body:
+                    case PointControlType.Line:
+                        {
+                            double newleft = maxRect.Left - SetDrawRect.Width + 10;
+                            double newright = maxRect.Right + SetDrawRect.Width - 10;
+                            double newtop = maxRect.Top - SetDrawRect.Height + 10;
+                            double newbottom = maxRect.Bottom + SetDrawRect.Height - 10;
+                            if (newleft < 0)
+                            {
+                                newleft = 0;
+                            }
+                            Rect newMaxRect = new Rect(newleft, newtop, newright - newleft, newbottom - newtop);
+
+                            Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), newMaxRect);
+                            left = cacheRect.Left + OffsetPos.X;
+                            right = cacheRect.Right + OffsetPos.X;
+                            top = cacheRect.Top + OffsetPos.Y;
+                            bottom = cacheRect.Bottom + OffsetPos.Y;
+                        }
+                        break;
+
+                    default:
+                        break;
+                }
+
+                //if (left < maxRect.Left)
+                //{
+                //    left = maxRect.Left;
+                //}
+
+                //if (top < maxRect.Top)
+                //{
+                //    top = maxRect.Top;
+                //}
+
+                if (right > maxRect.Right + SetDrawRect.Width - 10)
+                {
+                    if (left > maxRect.Right - 10)
+                    {
+                        left = maxRect.Right - 10;
+                    }
+                    right = maxRect.Right + SetDrawRect.Width - 10;
+                }
+
+                if (bottom > maxRect.Bottom + SetDrawRect.Height - 10)
+                {
+                    if (top > maxRect.Bottom - 10)
+                    {
+                        top = maxRect.Bottom - 10;
+                    }
+                    bottom = maxRect.Bottom + SetDrawRect.Height - 10;
+                }
+
+                drawRect = new Rect(left, top, right - left, bottom - top);
+                moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
+                return true;
+            }
+            catch (Exception ex)
+            {
+            }
+            return false;
+        }
+
         /// <summary>
         /// <summary>
         /// Proportional scaling offset calibration
         /// Proportional scaling offset calibration
         /// </summary>
         /// </summary>
@@ -687,10 +1016,10 @@ namespace ComPDFKit.Tool.DrawTool
             //GeometryGroup controlGroup = new GeometryGroup();
             //GeometryGroup controlGroup = new GeometryGroup();
             //controlGroup.FillRule = FillRule.Nonzero;
             //controlGroup.FillRule = FillRule.Nonzero;
             clipThickness.Left = SetDrawRect.Left - drawRect.Left;
             clipThickness.Left = SetDrawRect.Left - drawRect.Left;
-            clipThickness.Top= SetDrawRect.Top - drawRect.Top;
+            clipThickness.Top = SetDrawRect.Top - drawRect.Top;
             clipThickness.Right = SetDrawRect.Right - drawRect.Right;
             clipThickness.Right = SetDrawRect.Right - drawRect.Right;
             clipThickness.Bottom = SetDrawRect.Bottom - drawRect.Bottom;
             clipThickness.Bottom = SetDrawRect.Bottom - drawRect.Bottom;
-            List<Point> controlCurrentPoints=GetControlPoint(drawRect);
+            List<Point> controlCurrentPoints = GetControlPoint(drawRect);
             CombinedGeometry controlGroup = new CombinedGeometry();
             CombinedGeometry controlGroup = new CombinedGeometry();
             RectangleGeometry paintGeometry = new RectangleGeometry();
             RectangleGeometry paintGeometry = new RectangleGeometry();
             paintGeometry.Rect = SetDrawRect;
             paintGeometry.Rect = SetDrawRect;
@@ -776,7 +1105,7 @@ namespace ComPDFKit.Tool.DrawTool
         /// <param name="moveRect">
         /// <param name="moveRect">
         /// Current rectangle to draw
         /// Current rectangle to draw
         /// </param>
         /// </param>
-        protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect,Pen RectPen=null)
+        protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect, Pen RectPen = null)
         {
         {
             switch (controltype)
             switch (controltype)
             {
             {