123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media;
- namespace PDF_Master.CustomControl.ScanViewControl
- {
- public class DrawRect : DrawingVisual
- {
- private DrawingContext DrawDC { get; set; }
- /// <summary>
- /// 当前点的类型
- /// </summary>
- public enum PointControlType
- {
- None = -1,
- LeftTop,
- LeftMiddle,
- LeftBottom,
- MiddlBottom,
- RightBottom,
- RightMiddle,
- RightTop,
- MiddleTop,
- Rotate,
- Body
- }
- /// <summary>
- /// 当前点击命中的控制点
- /// </summary>
- public PointControlType HitControlType { get; private set; }
- /// <summary>
- /// 矩形框周围可拖拽点的坐标集合
- /// </summary>
- private List<Point> ControlPoints { get; set; } = new List<Point>();
- /// <summary>
- /// 当前内部缓存的矩形
- /// </summary>
- private Rect ClientRect { get; set; } = new Rect(0, 0, 0, 0);
- private Rect MaxRect { get; set; } = new Rect(0, 0, 0, 0);
- /// <summary>
- /// 按下时缓存的矩形
- /// </summary>
- private Rect SnapClientRect { get; set; }
- /// <summary>
- /// 覆盖填充色
- /// </summary>
- public SolidColorBrush FillBrush { get; set; }
- /// <summary>
- /// 边框线画刷
- /// </summary>
- public SolidColorBrush BorderBrush { get; set; }
- /// <summary>
- /// 边框线画笔
- /// </summary>
- public Pen LinePen { get; set; }
- /// <summary>
- /// 控制点画笔
- /// </summary>
- public Pen PointPen { get; set; }
- /// <summary>
- /// 控制点大小
- /// </summary>
- public int PointSize { get; set; } = 6;
- /// <summary>
- /// 绘制线段长度(点)
- /// </summary>
- public int ClipLine { get; set; } = 8;
- private double RatioRate { get; set; } = 1;
- /// <summary>
- /// 鼠标按下时的坐标
- /// </summary>
- public Point MouseDownPoint { get; set; }
- /// <summary>
- /// 移动偏移量
- /// </summary>
- private Point MoveOffset { get; set; } = new Point(0, 0);
- public DrawRect()
- {
- FillBrush = new SolidColorBrush(Color.FromArgb(0x01, 0x00, 0x00, 0x00));
- PointPen = new Pen(new SolidColorBrush(Color.FromRgb(0xC0, 0x4C, 0xF8)), 4);
- LinePen = new Pen(Brushes.White, 2);
- LinePen.DashStyle = DashStyles.Dash;
- }
- public void SetMaxRect(Rect rect)
- {
- MaxRect = rect;
- }
- /// <summary>
- /// 开始绘制
- /// </summary>
- public void OpenDraw()
- {
- if (DrawDC == null)
- {
- DrawDC = RenderOpen();
- }
- }
- /// <summary>
- /// 关闭绘制
- /// </summary>
- private void CloseDraw()
- {
- DrawDC?.Close();
- DrawDC = null;
- }
- /// <summary>
- /// 清除绘制内容
- /// </summary>
- public void ClearDraw()
- {
- OpenDraw();
- CloseDraw();
- }
- /// <summary>
- /// 绘制所需的内容
- /// </summary>
- public void Draw()
- {
- Dispatcher.Invoke(() =>
- {
- CalcControlPoint();
- OpenDraw();
- DrawDC?.DrawRectangle(FillBrush, LinePen, ClientRect);
- DrawClipMode();
- CloseDraw();
- });
- }
- /// <summary>
- /// 鼠标按下时调用,存储鼠标状态
- /// </summary>
- public bool MouseLeftButtonDown(Point clickPoint, CustomPanel custom)
- {
- HitControlType = PointControlType.None;
- HitTestResult hitResult = VisualTreeHelper.HitTest(custom, clickPoint);
- if (hitResult != null)
- {
- MouseDownPoint = clickPoint;
- MoveOffset = new Point(0, 0);
- HitControlType = GetHitControlIndex(clickPoint);
- if (HitControlType == PointControlType.None)
- {
- return false;
- }
- SnapClientRect = ClientRect;
- RatioRate = ClientRect.Width > 0 ? ClientRect.Height / ClientRect.Width : 1;
- return true;
- }
- return false;
- }
- public void test(double x)
- {
- ClientRect = new Rect(ClientRect.X , ClientRect.Y , ClientRect.Width * x, ClientRect.Height * x);
- }
- /// <summary>
- /// 鼠标松开时调用,存储鼠标状态
- /// </summary>
- public bool MouseLeftButtonUp(Point clickPoint)
- {
- if (HitControlType != PointControlType.None)
- {
- Draw();
- MoveOffset = new Point(0, 0);
- return true;
- }
- MoveOffset = new Point(0, 0);
- return false;
- }
- /// <summary>
- /// 鼠标移动时调用,根据按下时的状态,更新内部矩形
- /// </summary>
- public bool MouseMove(Point clickPoint)
- {
- if (HitControlType != PointControlType.None)
- {
- if (CalcHitPointMove(clickPoint))
- {
- Draw();
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 设置矩形用于绘制
- /// </summary>
- public void SetRect(Rect newRect)
- {
- ClientRect = newRect;
- }
- public Rect GetClientRect()
- {
- return ClientRect;
- }
- /// <summary>
- /// 获取当前命中状态
- /// </summary>
- private PointControlType GetHitControlIndex(Point clickPoint)
- {
- for (int i = 0; i < ControlPoints.Count; i++)
- {
- Point checkPoint = ControlPoints[i];
- Rect checkRect = new Rect(checkPoint.X - PointSize, checkPoint.Y - PointSize, PointSize * 2, PointSize * 2);
- if (checkRect.Contains(clickPoint))
- {
- return (PointControlType)i;
- }
- }
- if (ClientRect.Contains(clickPoint))
- {
- return PointControlType.Body;
- }
- return PointControlType.None;
- }
- /// <summary>
- /// 绘制框上的可选点
- /// </summary>
- private void DrawClipMode()
- {
- GeometryGroup geometryGroup = new GeometryGroup();
- //四个角的可选点样式绘制
- for (int i = 0; i <= 6; i += 2)
- {
- PathGeometry pathGeometry = GetCornerGeometry(ControlPoints[i], (PointControlType)i);
- if (pathGeometry != null)
- {
- geometryGroup.Children.Add(pathGeometry);
- }
- }
- //四条边的可选点样式绘制
- for (int i = 1; i <= 7; i += 2)
- {
- LineGeometry lineGeometry = GetMiddleGeometry(ControlPoints[i], (PointControlType)i);
- if (lineGeometry != null)
- {
- geometryGroup.Children.Add(lineGeometry);
- }
- }
- if (geometryGroup.Children.Count > 0)
- {
- DrawDC?.DrawGeometry(null, PointPen, geometryGroup);
- }
- }
- /// <summary>
- /// 计算所有点的坐标
- /// </summary>
- private void CalcControlPoint()
- {
- ControlPoints.Clear();
- int centerX = (int)(ClientRect.Left + ClientRect.Right) / 2;
- int centerY = (int)(ClientRect.Top + ClientRect.Bottom) / 2;
- ControlPoints.Add(new Point(ClientRect.Left, ClientRect.Top));
- ControlPoints.Add(new Point(ClientRect.Left, centerY));
- ControlPoints.Add(new Point(ClientRect.Left, ClientRect.Bottom));
- ControlPoints.Add(new Point(centerX, ClientRect.Bottom));
- ControlPoints.Add(new Point(ClientRect.Right, ClientRect.Bottom));
- ControlPoints.Add(new Point(ClientRect.Right, centerY));
- ControlPoints.Add(new Point(ClientRect.Right, ClientRect.Top));
- ControlPoints.Add(new Point(centerX, ClientRect.Top));
- }
- /// <summary>
- /// 根据类型生成对应四个角的Path路径
- /// </summary>
- private PathGeometry GetCornerGeometry(Point cornerPoint, PointControlType pointType)
- {
- PathGeometry pathGeometry = new PathGeometry();
- PathFigure figure = new PathFigure();
- PolyLineSegment polyLine = new PolyLineSegment();
- figure.Segments.Add(polyLine);
- pathGeometry.Figures.Add(figure);
- Point startPoint = new Point(0, 0), middlePoint = new Point(0, 0), endPoint = new Point(0, 0);
- int MinLine = (int)Math.Min(Math.Min(ClipLine, ClientRect.Width), ClientRect.Height);
- switch (pointType)
- {
- case PointControlType.LeftTop:
- {
- startPoint = new Point(cornerPoint.X, cornerPoint.Y + MinLine);
- middlePoint = cornerPoint;
- endPoint = new Point(cornerPoint.X + MinLine, cornerPoint.Y);
- }
- break;
- case PointControlType.LeftBottom:
- {
- startPoint = new Point(cornerPoint.X, cornerPoint.Y - MinLine);
- middlePoint = cornerPoint;
- endPoint = new Point(cornerPoint.X + MinLine, cornerPoint.Y);
- }
- break;
- case PointControlType.RightBottom:
- {
- startPoint = new Point(cornerPoint.X - MinLine, cornerPoint.Y);
- middlePoint = cornerPoint;
- endPoint = new Point(cornerPoint.X, cornerPoint.Y - MinLine);
- }
- break;
- case PointControlType.RightTop:
- {
- startPoint = new Point(cornerPoint.X - MinLine, cornerPoint.Y);
- middlePoint = cornerPoint;
- endPoint = new Point(cornerPoint.X, cornerPoint.Y + MinLine);
- }
- break;
- default:
- return null;
- }
- figure.StartPoint = startPoint;
- polyLine.Points.Add(middlePoint);
- polyLine.Points.Add(endPoint);
- return pathGeometry;
- }
- /// <summary>
- /// 根据类型生成对应四条边的Path路径
- /// </summary>
- private LineGeometry GetMiddleGeometry(Point middlePoint, PointControlType pointType)
- {
- LineGeometry lineGeometry = new LineGeometry();
- Point startPoint = new Point(0, 0), endPoint = new Point(0, 0);
- int MinLine = (int)Math.Min(Math.Min(ClipLine * 2, ClientRect.Width), ClientRect.Height) / 2;
- switch (pointType)
- {
- case PointControlType.LeftMiddle:
- case PointControlType.RightMiddle:
- {
- startPoint.X = middlePoint.X;
- startPoint.Y = middlePoint.Y - MinLine;
- endPoint.X = middlePoint.X;
- endPoint.Y = middlePoint.Y + MinLine;
- }
- break;
- case PointControlType.MiddleTop:
- case PointControlType.MiddlBottom:
- {
- startPoint.X = middlePoint.X - MinLine;
- startPoint.Y = middlePoint.Y;
- endPoint.X = middlePoint.X + MinLine;
- endPoint.Y = middlePoint.Y;
- }
- break;
- default:
- return null;
- }
- lineGeometry.StartPoint = startPoint;
- lineGeometry.EndPoint = endPoint;
- return lineGeometry;
- }
- /// <summary>
- /// 计算当前命中点是否需要绘制。为True则更新内部矩形
- /// </summary>
- private bool CalcHitPointMove(Point mousePoint)
- {
- if (HitControlType == PointControlType.None)
- {
- return false;
- }
- Point centerPoint = new Point((SnapClientRect.Right + SnapClientRect.Left) / 2, (SnapClientRect.Bottom + SnapClientRect.Top) / 2);
- Vector moveVector = mousePoint - centerPoint;
- int xMove = -(int)moveVector.X;
- int yMove = -(int)moveVector.Y;
- switch (HitControlType)
- {
- case PointControlType.LeftTop:
- {
- double X = centerPoint.X - xMove;
- double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X + xMove;
- if (Width < 10)
- {
- Width = 10;
- X = SnapClientRect.X + SnapClientRect.Width - 10;
- }
- double Y = centerPoint.Y - yMove;
- double Height = SnapClientRect.Height + SnapClientRect.Y - centerPoint.Y + yMove;
- if (Height < 10)
- {
- Height = 10;
- Y = SnapClientRect.Y + SnapClientRect.Height - 10;
- }
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.LeftMiddle:
- {
- double X = centerPoint.X - xMove;
- double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X + xMove;
- if (Width < 10)
- {
- Width = 10;
- X = SnapClientRect.X + SnapClientRect.Width - 10;
- }
- double Y = SnapClientRect.Top;
- double Height = SnapClientRect.Height;
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.LeftBottom:
- {
- double X = centerPoint.X - xMove;
- double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X + xMove;
- if (Width < 10)
- {
- Width = 10;
- X = SnapClientRect.X + SnapClientRect.Width - 10;
- }
- double Y = SnapClientRect.Y;
- double Height = SnapClientRect.Height + SnapClientRect.Y - centerPoint.Y - yMove;
- if (Height < 10)
- {
- Height = 10;
- Y = SnapClientRect.Y;
- }
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.MiddlBottom:
- {
- double X = SnapClientRect.Left;
- double Width = SnapClientRect.Width;
- double Y = SnapClientRect.Y;
- double Height = SnapClientRect.Height + SnapClientRect.Y - centerPoint.Y - yMove;
- if (Height < 10)
- {
- Height = 10;
- Y = SnapClientRect.Y;
- }
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.RightBottom:
- {
- double X = SnapClientRect.Left;
- double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X - xMove;
- if (Width < 10)
- {
- Width = 10;
- }
- double Y = SnapClientRect.Y;
- double Height = SnapClientRect.Height + SnapClientRect.Y - centerPoint.Y - yMove;
- if (Height < 10)
- {
- Height = 10;
- }
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.RightMiddle:
- {
- double X = SnapClientRect.Left;
- double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X - xMove;
- if (Width < 10)
- {
- Width = 10;
- }
- double Y = SnapClientRect.Top;
- double Height = SnapClientRect.Height;
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.RightTop:
- {
- double X = SnapClientRect.X;
- double Width = SnapClientRect.Width + SnapClientRect.X - centerPoint.X - xMove;
- if (Width < 10)
- {
- Width = 10;
- X = SnapClientRect.X;
- }
- double Y = centerPoint.Y - yMove;
- double Height = SnapClientRect.Height + SnapClientRect.Y - centerPoint.Y + yMove;
- if (Height < 10)
- {
- Height = 10;
- Y = SnapClientRect.Y + SnapClientRect.Height - 10;
- }
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.MiddleTop:
- {
- double X = SnapClientRect.X;
- double Width = SnapClientRect.Width;
- double Y = centerPoint.Y - yMove;
- double Height = SnapClientRect.Height + SnapClientRect.Y - centerPoint.Y + yMove;
- if (Height < 10)
- {
- Height = 10;
- Y = SnapClientRect.Y + SnapClientRect.Height - 10;
- }
- ClientRect = new Rect(new Point(X, Y),
- new Size(Width, Height));
- }
- break;
- case PointControlType.Rotate:
- break;
- case PointControlType.Body:
- {
- moveVector = mousePoint - MouseDownPoint;
- ClientRect = new Rect(SnapClientRect.Left - xMove, SnapClientRect.Top - yMove, SnapClientRect.Width, SnapClientRect.Height);
- }
- break;
- default:
- return false;
- }
- if (ClientRect.X < MaxRect.X)
- {
- ClientRect = new Rect
- (MaxRect.X,
- ClientRect.Y,
- ClientRect.Width,
- ClientRect.Height);
- }
- if (ClientRect.Y < MaxRect.Y)
- {
- ClientRect = new Rect
- (ClientRect.X,
- MaxRect.Y,
- ClientRect.Width,
- ClientRect.Height);
- }
- if (ClientRect.Right > MaxRect.Right)
- {
- ClientRect = new Rect
- (MaxRect.Right - ClientRect.Width,
- ClientRect.Y,
- ClientRect.Width,
- ClientRect.Height);
- }
- if (ClientRect.Bottom > MaxRect.Bottom)
- {
- ClientRect = new Rect
- (ClientRect.X,
- MaxRect.Bottom - ClientRect.Height,
- ClientRect.Width,
- ClientRect.Height);
- }
- Console.WriteLine(ClientRect.ToString());
- MoveOffset = new Point(ClientRect.X - SnapClientRect.X, ClientRect.Y - SnapClientRect.Y);
- return true;
- }
- }
- }
|