PageSelectedRect.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. using ComPDFKit.Tool.SettingParam;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. namespace ComPDFKit.Tool.DrawTool
  8. {
  9. public class PageSelectedRect : DrawingVisual
  10. {
  11. /// <summary>
  12. /// A method that repositions child elements
  13. /// </summary>
  14. public void Arrange()
  15. {
  16. foreach (Visual child in Children)
  17. {
  18. if (!(child is UIElement))
  19. {
  20. continue;
  21. }
  22. UIElement checkChild = child as UIElement;
  23. try
  24. {
  25. double left = Canvas.GetLeft(checkChild);
  26. double top = Canvas.GetTop(checkChild);
  27. double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
  28. double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
  29. checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  30. checkChild.Arrange(new Rect(
  31. double.IsNaN(left) ? 0 : left,
  32. double.IsNaN(top) ? 0 : top,
  33. double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
  34. double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
  35. }
  36. catch (Exception ex)
  37. {
  38. }
  39. }
  40. }
  41. protected DefaultDrawParam DrawParam = new DefaultDrawParam();
  42. protected DrawingContext drawDC { get; set; }
  43. /// <summary>
  44. /// Data changing event
  45. /// </summary>
  46. public event EventHandler<Rect> DataChanging;
  47. /// <summary>
  48. /// Data changed event
  49. /// </summary>
  50. public event EventHandler<Rect> DataChanged;
  51. /// <summary>
  52. /// Minimum width of the rectangle
  53. /// </summary>
  54. protected int rectMinWidth { get; set; } = 10;
  55. /// <summary>
  56. /// Minimum height of the rectangle
  57. /// </summary>
  58. protected int rectMinHeight { get; set; } = 10;
  59. /// <summary>
  60. /// Identifies whether the mouse is pressed
  61. /// </summary>
  62. protected bool isMouseDown { get; set; }
  63. /// <summary>
  64. /// Current click hit control point
  65. /// </summary>
  66. protected PointControlType hitControlType { get; set; }
  67. /// <summary>
  68. /// Location information recorded when the mouse is pressed
  69. /// </summary>
  70. protected Point mouseDownPoint { get; set; }
  71. /// <summary>
  72. /// Current set ignore point
  73. /// </summary>
  74. protected List<PointControlType> ignorePoints { get; set; } = new List<PointControlType>();
  75. /// <summary>
  76. /// Current control point coordinates
  77. /// </summary>
  78. protected List<Point> controlPoints { get; set; } = new List<Point>();
  79. /// <summary>
  80. /// Move offset during movement
  81. /// </summary>
  82. protected Point moveOffset { get; set; } = new Point(0, 0);
  83. /// <summary>
  84. /// Current PDFVIewer actual display width
  85. /// </summary>
  86. protected double PDFViewerActualWidth { get; set; } = 0;
  87. /// <summary>
  88. /// Current PDFVIewer actual display height
  89. /// </summary>
  90. protected double PDFViewerActualHeight { get; set; } = 0;
  91. /// <summary>
  92. /// Current control point drawing style
  93. /// </summary>
  94. protected DrawPointType currentDrawPointType { get; set; }
  95. /// <summary>
  96. /// Current drag drawing style
  97. /// </summary>
  98. protected DrawMoveType currentDrawMoveType { get; set; }
  99. /// <summary>
  100. /// Control point size
  101. /// </summary>
  102. protected int pointSize { get; set; } = 4;
  103. /// <summary>
  104. /// Current drawing rectangle (calculated during operation)
  105. /// </summary>
  106. protected Rect drawRect { get; set; } = new Rect(0, 0, 0, 0);
  107. /// <summary>
  108. /// Draw border and content internal padding
  109. /// </summary>
  110. protected double rectPadding = 5;
  111. /// <summary>
  112. /// Cache the rectangle when the mouse is pressed
  113. /// </summary>
  114. protected Rect cacheRect { get; set; } = new Rect(0, 0, 0, 0);
  115. /// <summary>
  116. /// Current set drawing rectangle (original data)
  117. /// </summary>
  118. protected Rect setDrawRect { get; set; } = new Rect(0, 0, 0, 0);
  119. /// <summary>
  120. /// Maximum drawable range
  121. /// </summary>
  122. protected Rect maxRect { get; set; } = new Rect(0, 0, 0, 0);
  123. /// <summary>
  124. /// Mouse start drawing point
  125. /// </summary>
  126. protected Point mouseStartPoint { get; set; }
  127. /// <summary>
  128. /// Mouse end drawing point
  129. /// </summary>
  130. protected Point mouseEndPoint { get; set; }
  131. /// <summary>
  132. /// Identifies whether proportional scaling is required
  133. /// </summary>
  134. protected bool isProportionalScaling { get; set; } = false;
  135. /// <summary>
  136. /// Identifies whether to draw a hover state
  137. /// </summary>
  138. protected bool isHover = false;
  139. /// <summary>
  140. /// Identifies whether to draw a selected state
  141. /// </summary>
  142. protected bool isSelected = false;
  143. protected bool isDrawCreateSelected = false;
  144. public PageSelectedRect(DefaultDrawParam defaultDrawParam)
  145. {
  146. DrawParam = defaultDrawParam;
  147. currentDrawPointType = DrawPointType.Square;
  148. }
  149. public void SetIsHover(bool hover)
  150. {
  151. isHover = hover;
  152. }
  153. public bool GetIsHover()
  154. {
  155. return isHover;
  156. }
  157. public void SetIsSelected(bool selected)
  158. {
  159. isSelected = selected;
  160. }
  161. public bool GetIsSelected()
  162. {
  163. return isSelected;
  164. }
  165. public DefaultDrawParam GetDefaultDrawParam()
  166. {
  167. return DrawParam;
  168. }
  169. /// <summary>
  170. /// Get the original set Rect, not the calculated filled
  171. /// </summary>
  172. /// <param name="newRect">
  173. /// The new rectangle to be set
  174. /// </param>
  175. public Rect GetRect()
  176. {
  177. Rect rect = new Rect(drawRect.X + rectPadding, drawRect.Y + rectPadding, Math.Max(0, drawRect.Width - 2 * rectPadding), Math.Max(0, drawRect.Height - 2 * rectPadding));
  178. return rect;
  179. }
  180. public void SetRect(Rect newRect)
  181. {
  182. newRect = new Rect(newRect.X - rectPadding, newRect.Y - rectPadding, newRect.Width + 2 * rectPadding, newRect.Height + 2 * rectPadding);
  183. setDrawRect = drawRect = newRect;
  184. }
  185. public void SetMaxRect(Rect rect)
  186. {
  187. maxRect = rect;
  188. }
  189. public Rect GetMaxRect()
  190. {
  191. return maxRect;
  192. }
  193. public void SetDrawMoveType(DrawMoveType drawType)
  194. {
  195. currentDrawMoveType = drawType;
  196. }
  197. public DrawMoveType GetDrawMoveType()
  198. {
  199. return currentDrawMoveType;
  200. }
  201. public void SetPDFViewerActualSize(double width, double height)
  202. {
  203. PDFViewerActualWidth = width;
  204. PDFViewerActualHeight = height;
  205. }
  206. public virtual void OnMouseLeftButtonDown(Point downPoint)
  207. {
  208. isMouseDown = true;
  209. hitControlType = PointControlType.None;
  210. mouseDownPoint = downPoint;
  211. moveOffset = new Point(0, 0);
  212. HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
  213. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  214. {
  215. hitControlType = GetHitControlIndex(downPoint);
  216. if (hitControlType != PointControlType.None)
  217. {
  218. cacheRect = drawRect;
  219. }
  220. }
  221. }
  222. public virtual void OnMouseLeftButtonUp(Point upPoint)
  223. {
  224. if (isDrawCreateSelected)
  225. {
  226. Draw();
  227. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  228. {
  229. InvokeDataChangEvent(true);
  230. }
  231. }
  232. else
  233. {
  234. if (isMouseDown && hitControlType != PointControlType.None)
  235. {
  236. isMouseDown = false;
  237. cacheRect = setDrawRect = drawRect;
  238. Draw();
  239. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  240. {
  241. InvokeDataChangEvent(true);
  242. }
  243. }
  244. moveOffset = new Point(0, 0);
  245. }
  246. isDrawCreateSelected = false;
  247. }
  248. public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
  249. {
  250. Tag = false;
  251. Tag = isMouseDown;
  252. SetPDFViewerActualSize(width, height);
  253. if (isDrawCreateSelected)
  254. {
  255. mouseEndPoint = mousePoint;
  256. Draw();
  257. }
  258. else
  259. {
  260. if (isMouseDown && hitControlType != PointControlType.None)
  261. {
  262. if (CalcHitPointMove(mousePoint))
  263. {
  264. Draw();
  265. if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
  266. {
  267. InvokeDataChangEvent(false);
  268. }
  269. }
  270. }
  271. }
  272. }
  273. public void CreateRect(Point downPoint, Point cropPoint, Rect maxRect, double width, double height)
  274. {
  275. mouseEndPoint = mouseStartPoint = downPoint;
  276. this.maxRect = maxRect;
  277. PDFViewerActualWidth = width;
  278. PDFViewerActualHeight = height;
  279. isDrawCreateSelected = true;
  280. }
  281. public void Draw()
  282. {
  283. if (isDrawCreateSelected)
  284. {
  285. CreateRect();
  286. }
  287. else
  288. {
  289. EditRect();
  290. }
  291. }
  292. private void CreateRect()
  293. {
  294. Dispatcher.Invoke(() =>
  295. {
  296. drawDC = RenderOpen();
  297. SolidColorBrush bgsolidColorBrush = DrawParam.PageSelectedBgBrush;
  298. Pen bgpen = DrawParam.PageSelectedRectLinePen;
  299. CombinedGeometry clipGeometry = new CombinedGeometry();
  300. clipGeometry.Geometry1 = new RectangleGeometry() { Rect = maxRect };
  301. clipGeometry.Geometry2 = new RectangleGeometry() { Rect = drawRect };
  302. clipGeometry.GeometryCombineMode = GeometryCombineMode.Exclude;
  303. drawDC?.DrawGeometry(bgsolidColorBrush, bgpen, clipGeometry);
  304. SolidColorBrush solidColorBrush = DrawParam.PageSelectedRectFillBrush;
  305. Pen pen = DrawParam.PageSelectedRectLinePen;
  306. if (isProportionalScaling)
  307. {
  308. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  309. if (mouseOffset.X < 0)
  310. {
  311. if (mouseOffset.Y > 0)
  312. {
  313. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  314. }
  315. else
  316. {
  317. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  318. }
  319. }
  320. else
  321. {
  322. if (mouseOffset.Y > 0)
  323. {
  324. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  325. }
  326. else
  327. {
  328. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  329. }
  330. }
  331. }
  332. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  333. double mLeft = rect.Left;
  334. double mRight = rect.Right;
  335. double mUp = rect.Top;
  336. double mDown = rect.Bottom;
  337. if (rect.Left < maxRect.Left)
  338. {
  339. mLeft = maxRect.Left;
  340. }
  341. if (rect.Right > maxRect.Right)
  342. {
  343. mRight = maxRect.Right;
  344. }
  345. if (rect.Top < maxRect.Top)
  346. {
  347. mUp = maxRect.Top;
  348. }
  349. if (rect.Bottom > maxRect.Bottom)
  350. {
  351. mDown = maxRect.Bottom;
  352. }
  353. Rect DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  354. int halfPenWidth = (int)Math.Ceiling(pen.Thickness / 2);
  355. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  356. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  357. if (drawWidth > 0 && drawHeight > 0)
  358. {
  359. drawRect = new Rect(
  360. (int)DPIRect.Left + halfPenWidth,
  361. (int)DPIRect.Top + halfPenWidth,
  362. (int)DPIRect.Width - halfPenWidth * 2,
  363. (int)DPIRect.Height - halfPenWidth * 2);
  364. drawDC?.DrawRectangle(solidColorBrush, pen, drawRect);
  365. CalcControlPoint(drawRect);
  366. SolidColorBrush PointBrush = DrawParam.PageSelectedPointBorderBrush;
  367. Pen PointPen = DrawParam.PageSelectedPointPen;
  368. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  369. switch (currentDrawPointType)
  370. {
  371. case DrawPointType.Circle:
  372. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  373. break;
  374. case DrawPointType.Square:
  375. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  376. break;
  377. }
  378. }
  379. drawDC?.Close();
  380. drawDC = null;
  381. });
  382. }
  383. private void EditRect()
  384. {
  385. Dispatcher.Invoke(() =>
  386. {
  387. drawDC = RenderOpen();
  388. SolidColorBrush bgsolidColorBrush = DrawParam.PageSelectedBgBrush;
  389. Pen bgpen = DrawParam.PageSelectedRectLinePen;
  390. CombinedGeometry clipGeometry = new CombinedGeometry();
  391. clipGeometry.Geometry1 = new RectangleGeometry() { Rect = maxRect };
  392. clipGeometry.Geometry2 = new RectangleGeometry() { Rect = drawRect };
  393. clipGeometry.GeometryCombineMode = GeometryCombineMode.Exclude;
  394. drawDC?.DrawGeometry(bgsolidColorBrush, bgpen, clipGeometry);
  395. CalcControlPoint(drawRect);
  396. SolidColorBrush solidColorBrush = DrawParam.PageSelectedRectFillBrush;
  397. Pen pen = DrawParam.PageSelectedRectLinePen;
  398. SolidColorBrush PointBrush = DrawParam.PageSelectedPointBorderBrush;
  399. Pen PointPen = DrawParam.PageSelectedPointPen;
  400. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  401. switch (currentDrawMoveType)
  402. {
  403. case DrawMoveType.kDefault:
  404. break;
  405. case DrawMoveType.kReferenceLine:
  406. if (isMouseDown == true)
  407. {
  408. SolidColorBrush moveBrush = DrawParam.PDFEditMultiMoveBrush;
  409. Pen movepen = DrawParam.PDFEditMultiMovePen;
  410. GetMoveBrushAndPen(ref moveBrush, ref movepen);
  411. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  412. }
  413. drawDC?.DrawRectangle(solidColorBrush, pen, drawRect);
  414. break;
  415. default:
  416. break;
  417. }
  418. switch (currentDrawPointType)
  419. {
  420. case DrawPointType.Circle:
  421. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  422. break;
  423. case DrawPointType.Square:
  424. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  425. break;
  426. }
  427. drawDC?.Close();
  428. drawDC = null;
  429. });
  430. }
  431. public virtual void ClearDraw()
  432. {
  433. setDrawRect = drawRect = new Rect();
  434. drawDC = RenderOpen();
  435. drawDC?.Close();
  436. drawDC = null;
  437. }
  438. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  439. {
  440. colorBrush = DrawParam.PageSelectedMoveBrush;
  441. pen = DrawParam.PageSelectedMovePen;
  442. }
  443. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  444. {
  445. if (isHover)
  446. {
  447. colorBrush = DrawParam.PageSelectedRectFillHoverBrush;
  448. pen = DrawParam.PageSelectedRectLineHoverPen;
  449. }
  450. else
  451. {
  452. if (isSelected)
  453. {
  454. colorBrush = DrawParam.SPageSelectedRectFillBrush;
  455. pen = DrawParam.SPageSelectedRectLinePen;
  456. }
  457. else
  458. {
  459. colorBrush = DrawParam.PageSelectedRectFillBrush;
  460. pen = DrawParam.PageSelectedRectLinePen;
  461. }
  462. }
  463. }
  464. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  465. {
  466. if (isHover)
  467. {
  468. colorBrush = DrawParam.PageSelectedRectFillHoverBrush;
  469. pen = DrawParam.PageSelectedRectLineHoverPen;
  470. }
  471. else
  472. {
  473. if (isSelected)
  474. {
  475. colorBrush = DrawParam.SPageSelectedPointBorderBrush;
  476. pen = DrawParam.SPageSelectedPointPen;
  477. }
  478. else
  479. {
  480. colorBrush = DrawParam.PageSelectedPointBorderBrush;
  481. pen = DrawParam.PageSelectedPointHoverPen;
  482. }
  483. }
  484. }
  485. /// <summary>
  486. /// Inner drawing circle point
  487. /// </summary>
  488. /// <param name="drawingContext">
  489. /// Drawing context
  490. /// </param>
  491. /// <param name="ControlPoints">
  492. /// Dataset of ignored points. Collection of positions where points need to be drawn.
  493. /// </param>
  494. /// <param name="PointSize">
  495. /// Size of the point
  496. /// </param>
  497. /// <param name="PointPen">
  498. /// Point brush
  499. /// </param>
  500. /// <param name="BorderBrush">
  501. /// Border brush for drawing points
  502. /// </param>
  503. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  504. {
  505. GeometryGroup controlGroup = new GeometryGroup();
  506. controlGroup.FillRule = FillRule.Nonzero;
  507. List<Point> ignorePointsList = new List<Point>();
  508. // Get specific points
  509. foreach (PointControlType type in ignoreList)
  510. {
  511. if ((int)type < controlPoints.Count)
  512. {
  513. ignorePointsList.Add(controlPoints[(int)type]);
  514. }
  515. }
  516. for (int i = 0; i < controlPoints.Count; i++)
  517. {
  518. Point controlPoint = controlPoints[i];
  519. if (ignorePointsList.Contains(controlPoint))
  520. {
  521. continue;
  522. }
  523. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  524. controlGroup.Children.Add(circlPoint);
  525. }
  526. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  527. }
  528. /// <summary>
  529. /// Inner drawing square
  530. /// </summary>
  531. /// <param name="drawingContext">
  532. /// Drawing context
  533. /// </param>
  534. /// <param name="ControlPoints">
  535. /// Collection of positions where points need to be drawn
  536. /// </param>
  537. /// <param name="PointSize">
  538. /// Collection of positions where points need to be drawn
  539. /// </param>
  540. /// <param name="PointPen">
  541. /// The brush for drawing points
  542. /// </param>
  543. /// <param name="BorderBrush">
  544. /// The brush for drawing the border of points
  545. /// </param>
  546. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  547. {
  548. GeometryGroup controlGroup = new GeometryGroup();
  549. controlGroup.FillRule = FillRule.Nonzero;
  550. List<Point> ignorePointsList = new List<Point>();
  551. // Get specific points
  552. foreach (PointControlType type in ignoreList)
  553. {
  554. if ((int)type < controlPoints.Count)
  555. {
  556. ignorePointsList.Add(controlPoints[(int)type]);
  557. }
  558. }
  559. for (int i = 0; i < controlPoints.Count; i++)
  560. {
  561. Point controlPoint = controlPoints[i];
  562. if (ignorePointsList.Contains(controlPoint))
  563. {
  564. continue;
  565. }
  566. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  567. PointSize * 2, PointSize * 2), 1, 1);
  568. controlGroup.Children.Add(rectPoint);
  569. }
  570. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  571. }
  572. /// <summary>
  573. /// Draw the reference line in the moving state
  574. /// </summary>
  575. /// <param name="drawDc">
  576. /// Drawing context handle
  577. /// </param>
  578. /// <param name="controltype">
  579. /// Current selected control point type
  580. /// </param>
  581. /// <param name="activePen">
  582. /// Line brush
  583. /// </param>
  584. /// <param name="moveBrush">
  585. /// Rectangle brush
  586. /// </param>
  587. /// <param name="moveRect">
  588. /// Current rectangle to be drawn
  589. /// </param>
  590. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect)
  591. {
  592. switch (controltype)
  593. {
  594. case PointControlType.LeftTop:
  595. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  596. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  597. break;
  598. case PointControlType.LeftMiddle:
  599. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  600. break;
  601. case PointControlType.LeftBottom:
  602. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  603. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  604. break;
  605. case PointControlType.MiddleBottom:
  606. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  607. break;
  608. case PointControlType.RightBottom:
  609. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  610. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  611. break;
  612. case PointControlType.RightMiddle:
  613. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  614. break;
  615. case PointControlType.RightTop:
  616. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  617. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  618. break;
  619. case PointControlType.MiddleTop:
  620. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  621. break;
  622. case PointControlType.Rotate:
  623. break;
  624. case PointControlType.Body:
  625. case PointControlType.Line:
  626. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  627. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  628. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  629. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  630. break;
  631. default:
  632. break;
  633. }
  634. drawDc?.DrawRectangle(moveBrush, null, moveRect);
  635. }
  636. /// <summary>
  637. /// Calculate the current control point
  638. /// </summary>
  639. /// <param name="currentRect">
  640. /// Control point target rectangle
  641. /// </param>
  642. protected void CalcControlPoint(Rect currentRect)
  643. {
  644. controlPoints.Clear();
  645. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  646. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  647. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  648. controlPoints.Add(new Point(currentRect.Left, centerY));
  649. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  650. controlPoints.Add(new Point(centerX, currentRect.Bottom));
  651. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  652. controlPoints.Add(new Point(currentRect.Right, centerY));
  653. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  654. controlPoints.Add(new Point(centerX, currentRect.Top));
  655. }
  656. /// <summary>
  657. /// Calculate the movement of the hit point
  658. /// </summary>
  659. /// <param name="mousePoint">
  660. /// Current mouse position
  661. /// </param>
  662. /// <returns>
  663. /// Return true if the calculation is successful
  664. /// </returns>
  665. protected bool CalcHitPointMove(Point mousePoint)
  666. {
  667. if (isMouseDown == false || hitControlType == PointControlType.None)
  668. {
  669. return false;
  670. }
  671. return NormalScaling(mousePoint);
  672. }
  673. /// <summary>
  674. /// Draw the algorithm of the normal scaling form (drag a point, only scale in one direction)
  675. /// </summary>
  676. /// <param name="mousePoint">
  677. /// Current mouse position
  678. /// </param>
  679. /// <returns>
  680. /// Return true if the calculation is successful
  681. /// </returns>
  682. protected bool NormalScaling(Point mousePoint)
  683. {
  684. double mLeft = cacheRect.Left;
  685. double mRight = cacheRect.Right;
  686. double mUp = cacheRect.Top;
  687. double mDown = cacheRect.Bottom;
  688. double TmpLeft = mLeft, TmpRight = mRight, TmpUp = mUp, TmpDown = mDown;
  689. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  690. Point moveVector = (Point)(mousePoint - centerPoint);
  691. moveVector = ProportionalScalingOffsetPos(moveVector);
  692. switch (hitControlType)
  693. {
  694. case PointControlType.LeftTop:
  695. TmpLeft = centerPoint.X + moveVector.X;
  696. TmpRight = cacheRect.Right;
  697. if (TmpLeft + rectMinWidth > TmpRight)
  698. {
  699. TmpLeft = TmpRight - rectMinWidth;
  700. }
  701. TmpUp = centerPoint.Y + moveVector.Y;
  702. TmpDown = cacheRect.Bottom;
  703. if (TmpUp + rectMinHeight > TmpDown)
  704. {
  705. TmpUp = TmpDown - rectMinHeight;
  706. }
  707. break;
  708. case PointControlType.LeftMiddle:
  709. TmpLeft = centerPoint.X + moveVector.X;
  710. TmpRight = cacheRect.Right;
  711. if (TmpLeft + rectMinWidth > TmpRight)
  712. {
  713. TmpLeft = TmpRight - rectMinWidth;
  714. }
  715. TmpUp = cacheRect.Top;
  716. TmpDown = cacheRect.Bottom;
  717. break;
  718. case PointControlType.LeftBottom:
  719. TmpLeft = centerPoint.X + moveVector.X;
  720. TmpRight = cacheRect.Right;
  721. if (TmpLeft + rectMinWidth > TmpRight)
  722. {
  723. TmpLeft = TmpRight - rectMinWidth;
  724. }
  725. TmpUp = cacheRect.Top;
  726. TmpDown = centerPoint.Y + moveVector.Y;
  727. if (TmpUp + rectMinHeight > TmpDown)
  728. {
  729. TmpDown = TmpUp + rectMinHeight;
  730. }
  731. break;
  732. case PointControlType.MiddleBottom:
  733. TmpLeft = cacheRect.Left;
  734. TmpRight = cacheRect.Right;
  735. TmpUp = cacheRect.Top;
  736. TmpDown = centerPoint.Y + moveVector.Y;
  737. if (TmpUp + rectMinHeight > TmpDown)
  738. {
  739. TmpDown = TmpUp + rectMinHeight;
  740. }
  741. break;
  742. case PointControlType.RightBottom:
  743. TmpLeft = cacheRect.Left;
  744. TmpRight = centerPoint.X + moveVector.X;
  745. if (TmpLeft + rectMinWidth > TmpRight)
  746. {
  747. TmpRight = TmpLeft + rectMinWidth;
  748. }
  749. TmpUp = cacheRect.Top;
  750. TmpDown = centerPoint.Y + moveVector.Y;
  751. if (TmpUp + rectMinHeight > TmpDown)
  752. {
  753. TmpDown = TmpUp + rectMinHeight;
  754. }
  755. break;
  756. case PointControlType.RightMiddle:
  757. TmpLeft = cacheRect.Left;
  758. TmpRight = centerPoint.X + moveVector.X;
  759. if (TmpLeft + rectMinWidth > TmpRight)
  760. {
  761. TmpRight = TmpLeft + rectMinWidth;
  762. }
  763. TmpUp = cacheRect.Top;
  764. TmpDown = cacheRect.Bottom;
  765. break;
  766. case PointControlType.RightTop:
  767. TmpLeft = cacheRect.Left;
  768. TmpRight = centerPoint.X + moveVector.X;
  769. if (TmpLeft + rectMinWidth > TmpRight)
  770. {
  771. TmpRight = TmpLeft + rectMinWidth;
  772. }
  773. TmpUp = centerPoint.Y + moveVector.Y;
  774. TmpDown = cacheRect.Bottom;
  775. if (TmpUp + rectMinHeight > TmpDown)
  776. {
  777. TmpUp = TmpDown - rectMinHeight;
  778. }
  779. break;
  780. case PointControlType.MiddleTop:
  781. TmpLeft = cacheRect.Left;
  782. TmpRight = cacheRect.Right;
  783. TmpUp = centerPoint.Y + moveVector.Y;
  784. TmpDown = cacheRect.Bottom;
  785. if (TmpUp + rectMinHeight > TmpDown)
  786. {
  787. TmpUp = TmpDown - rectMinHeight;
  788. }
  789. break;
  790. case PointControlType.Body:
  791. case PointControlType.Line:
  792. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  793. TmpLeft = cacheRect.Left + OffsetPos.X;
  794. TmpRight = cacheRect.Right + OffsetPos.X;
  795. TmpUp = cacheRect.Top + OffsetPos.Y;
  796. TmpDown = cacheRect.Bottom + OffsetPos.Y;
  797. break;
  798. default:
  799. break;
  800. }
  801. if (TmpLeft < maxRect.Left)
  802. {
  803. TmpLeft = maxRect.Left;
  804. }
  805. if (TmpUp < maxRect.Top)
  806. {
  807. TmpUp = maxRect.Top;
  808. }
  809. if (TmpRight > maxRect.Right)
  810. {
  811. TmpRight = maxRect.Right;
  812. }
  813. if (TmpDown > maxRect.Bottom)
  814. {
  815. TmpDown = maxRect.Bottom;
  816. }
  817. if (TmpRight - TmpLeft < 0.0 || TmpDown - TmpUp < 0.0)
  818. {
  819. return false;
  820. }
  821. drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  822. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  823. return true;
  824. }
  825. /// <summary>
  826. /// Proportional scaling offset calibration.
  827. /// </summary>
  828. /// <param name="movePoint">
  829. /// Current mouse position
  830. /// </param>
  831. /// <returns>
  832. /// Return the calibrated offset value
  833. /// </returns>
  834. protected Point ProportionalScalingOffsetPos(Point movePoint)
  835. {
  836. if (isProportionalScaling)
  837. {
  838. Point offsetPos = movePoint;
  839. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  840. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  841. switch (hitControlType)
  842. {
  843. case PointControlType.LeftTop:
  844. case PointControlType.RightBottom:
  845. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  846. break;
  847. case PointControlType.LeftBottom:
  848. case PointControlType.RightTop:
  849. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  850. break;
  851. case PointControlType.LeftMiddle:
  852. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  853. break;
  854. case PointControlType.RightMiddle:
  855. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  856. break;
  857. case PointControlType.MiddleBottom:
  858. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  859. break;
  860. case PointControlType.MiddleTop:
  861. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  862. break;
  863. default:
  864. break;
  865. }
  866. return offsetPos;
  867. }
  868. else
  869. {
  870. return movePoint;
  871. }
  872. }
  873. /// <summary>
  874. /// Calc the offset of the current rectangle in the maximum rectangle range
  875. /// </summary>
  876. /// <param name="currentRect">
  877. /// The rectangle cached when pressed.
  878. /// </param>
  879. /// <param name="offsetPoint">
  880. /// Equivalent to the offset value when pressed.
  881. /// </param>
  882. /// <param name="maxRect">
  883. /// The maximum rectangle range.
  884. /// </param>
  885. /// <returns>
  886. /// Return the offset value after the calculation.
  887. /// </returns>
  888. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  889. {
  890. double cLeft = currentRect.Left;
  891. double cRight = currentRect.Right;
  892. double cUp = currentRect.Top;
  893. double cDown = currentRect.Bottom;
  894. double TmpLeft = cLeft + offsetPoint.X;
  895. double TmpRight = cRight + offsetPoint.X;
  896. double TmpUp = cUp + offsetPoint.Y;
  897. double TmpDown = cDown + offsetPoint.Y;
  898. if (TmpLeft < maxRect.Left)
  899. {
  900. TmpRight = (cRight - cLeft) + maxRect.Left;
  901. TmpLeft = maxRect.Left;
  902. }
  903. if (TmpUp < maxRect.Top)
  904. {
  905. TmpDown = (cDown - cUp) + maxRect.Top;
  906. TmpUp = maxRect.Top;
  907. }
  908. if (TmpRight > maxRect.Right)
  909. {
  910. TmpLeft = maxRect.Right - (cRight - cLeft);
  911. TmpRight = maxRect.Right;
  912. }
  913. if (TmpDown > maxRect.Bottom)
  914. {
  915. TmpUp = maxRect.Bottom - (cDown - cUp);
  916. TmpDown = maxRect.Bottom;
  917. }
  918. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  919. return offsetPoint;
  920. }
  921. /// <summary>
  922. /// Get which control point the coordinate is on
  923. /// </summary>
  924. /// <param name="clickPoint">
  925. /// The coordinate of the click
  926. /// </param>
  927. /// <returns>
  928. /// Return the control point type
  929. /// </returns>
  930. public PointControlType GetHitControlIndex(Point point)
  931. {
  932. try
  933. {
  934. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  935. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  936. {
  937. List<PointControlType> ignoreList = GetIgnorePoints();
  938. List<Point> IgnorePointsList = new List<Point>();
  939. foreach (PointControlType type in ignoreList)
  940. {
  941. if ((int)type < controlPoints.Count)
  942. {
  943. IgnorePointsList.Add(controlPoints[(int)type]);
  944. }
  945. }
  946. for (int i = 0; i < controlPoints.Count; i++)
  947. {
  948. Point checkPoint = controlPoints[i];
  949. if (IgnorePointsList.Contains(checkPoint))
  950. {
  951. continue;
  952. }
  953. switch (currentDrawPointType)
  954. {
  955. case DrawPointType.Circle:
  956. Vector checkVector = checkPoint - point;
  957. if (checkVector.Length < pointSize)
  958. {
  959. return (PointControlType)i;
  960. }
  961. break;
  962. case DrawPointType.Square:
  963. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  964. if (checkRect.Contains(point))
  965. {
  966. return (PointControlType)i;
  967. }
  968. break;
  969. case DrawPointType.Crop:
  970. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  971. if (cropRect.Contains(point))
  972. {
  973. return (PointControlType)i;
  974. }
  975. break;
  976. default:
  977. break;
  978. }
  979. }
  980. if (drawRect.Contains(point))
  981. {
  982. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), drawRect.Width - 2 * rectPadding, drawRect.Height - 2 * rectPadding);
  983. if (rect.Contains(point))
  984. {
  985. if (!ignoreList.Contains(PointControlType.Body))
  986. {
  987. return PointControlType.Body;
  988. }
  989. }
  990. if (!ignoreList.Contains(PointControlType.Body))
  991. {
  992. return PointControlType.Line;
  993. }
  994. }
  995. }
  996. }
  997. catch (Exception ex)
  998. {
  999. }
  1000. return PointControlType.None;
  1001. }
  1002. /// <summary>
  1003. /// Get the current set of ignored points
  1004. /// </summary>
  1005. /// The dataset of ignored points
  1006. /// </returns>
  1007. private List<PointControlType> GetIgnorePoints()
  1008. {
  1009. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  1010. foreach (PointControlType type in ignorePoints)
  1011. {
  1012. IgnorePointsList.Add(type);
  1013. }
  1014. return IgnorePointsList;
  1015. }
  1016. /// <summary>
  1017. /// Notify events during/after drawing data
  1018. /// </summary>
  1019. /// <param name="isFinish">
  1020. /// Identifies whether the data has changed
  1021. /// </param>
  1022. protected void InvokeDataChangEvent(bool isFinish)
  1023. {
  1024. if (isFinish)
  1025. {
  1026. DataChanged?.Invoke(this, drawRect);
  1027. }
  1028. else
  1029. {
  1030. DataChanging?.Invoke(this, drawRect);
  1031. }
  1032. }
  1033. }
  1034. }