MultiSelectedRect.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. using ComPDFKit.Tool.SettingParam;
  2. using ComPDFKitViewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. using static ComPDFKit.Tool.Help.ImportWin32;
  13. namespace ComPDFKit.Tool.DrawTool
  14. {
  15. public enum MulitiDrawMoveType
  16. {
  17. Default,
  18. Alone
  19. }
  20. public class MultiSelectedRect : DrawingVisual
  21. {
  22. /// <summary>
  23. /// Re-layout child elements
  24. /// </summary>
  25. public void Arrange()
  26. {
  27. foreach (Visual child in Children)
  28. {
  29. if (!(child is UIElement))
  30. {
  31. continue;
  32. }
  33. UIElement checkChild = child as UIElement;
  34. try
  35. {
  36. double left = Canvas.GetLeft(checkChild);
  37. double top = Canvas.GetTop(checkChild);
  38. double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
  39. double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
  40. checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  41. checkChild.Arrange(new Rect(
  42. double.IsNaN(left) ? 0 : left,
  43. double.IsNaN(top) ? 0 : top,
  44. double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
  45. double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
  46. }
  47. catch (Exception ex)
  48. {
  49. }
  50. }
  51. }
  52. protected DefaultDrawParam drawParam = new DefaultDrawParam();
  53. protected DrawingContext drawDC { get; set; }
  54. /// <summary>
  55. /// DataChanging event
  56. /// </summary>
  57. public event EventHandler<Point> DataChanging;
  58. /// <summary>
  59. /// DataChanged event
  60. /// </summary>
  61. public event EventHandler<Point> DataChanged;
  62. protected SelectedType selectedType = SelectedType.None;
  63. /// <summary>
  64. /// Minimum width of the rectangle
  65. /// </summary>
  66. protected int rectMinWidth { get; set; } = 10;
  67. /// <summary>
  68. /// Minimum height of the rectangle
  69. /// </summary>
  70. protected int rectMinHeight { get; set; } = 10;
  71. /// <summary>
  72. /// Identify whether the mouse is pressed
  73. /// </summary>
  74. protected bool isMouseDown { get; set; }
  75. /// <summary>
  76. /// Current hit control point
  77. /// </summary>
  78. protected PointControlType hitControlType { get; set; }
  79. /// <summary>
  80. /// Location information recorded when the mouse is pressed
  81. /// </summary>
  82. protected Point mouseDownPoint { get; set; }
  83. /// <summary>
  84. /// Current set ignore points
  85. /// </summary>
  86. protected List<PointControlType> ignorePoints { get; set; } = new List<PointControlType>();
  87. /// <summary>
  88. /// Current control point coordinates
  89. /// </summary>
  90. protected List<Point> controlPoints { get; set; } = new List<Point>();
  91. /// <summary>
  92. /// Move offset
  93. /// </summary>
  94. protected Point moveOffset { get; set; } = new Point(0, 0);
  95. /// <summary>
  96. /// Current PDFVIewer's actual display width
  97. /// </summary>
  98. protected double PDFViewerActualWidth { get; set; } = 0;
  99. /// <summary>
  100. /// Current PDFVIewer's actual display height
  101. /// </summary>
  102. protected double PDFViewerActualHeight { get; set; } = 0;
  103. /// <summary>
  104. /// Current control point drawing style
  105. /// </summary>
  106. protected DrawPointType currentDrawPointType { get; set; }
  107. /// <summary>
  108. /// Current drag drawing style
  109. /// </summary>
  110. protected DrawMoveType currentDrawMoveType { get; set; }
  111. /// <summary>
  112. /// Current multi-select drawing style
  113. /// </summary>
  114. protected MulitiDrawMoveType currentDrawType { get; set; } = MulitiDrawMoveType.Default;
  115. /// <summary>
  116. /// Control point size
  117. /// </summary>
  118. protected int pointSize { get; set; } = 4;
  119. /// <summary>
  120. /// Current drawing rectangle (calculated during operation)
  121. /// </summary>
  122. protected Rect drawRect { get; set; } = new Rect(0, 0, 0, 0);
  123. /// <summary>
  124. /// Default outermost rectangle of the drawing style (calculated during operation)
  125. /// </summary>
  126. protected Rect drawDefaultRect { get; set; } = new Rect(0, 0, 0, 0);
  127. /// <summary>
  128. /// Padding between the border and the content
  129. /// </summary>
  130. protected double rectPadding = 5;
  131. /// <summary>
  132. /// Mouse down cache rectangle
  133. /// </summary>
  134. protected Rect cacheRect { get; set; } = new Rect(0, 0, 0, 0);
  135. /// <summary>
  136. /// Current set drawing rectangle (original data)
  137. /// </summary>
  138. protected Rect setDrawRect { get; set; } = new Rect(0, 0, 0, 0);
  139. /// <summary>
  140. /// Maximum drawable range
  141. /// </summary>
  142. protected Rect maxRect { get; set; } = new Rect(0, 0, 0, 0);
  143. /// <summary>
  144. /// Identify whether the mouse is pressed
  145. /// </summary>
  146. protected bool isProportionalScaling { get; set; } = false;
  147. /// <summary>
  148. /// Array passed from outside for multiple selection
  149. /// </summary>
  150. protected List<SelectedRect> selectedRects = new List<SelectedRect>();
  151. protected bool isHover = false;
  152. protected bool isSelected = false;
  153. public void SetIsHover(bool hover)
  154. {
  155. isHover = hover;
  156. }
  157. public bool GetIsHover()
  158. {
  159. return isHover;
  160. }
  161. public void SetIsSelected(bool selected)
  162. {
  163. isSelected = selected;
  164. }
  165. public bool GetIsSelected()
  166. {
  167. return isSelected;
  168. }
  169. public MultiSelectedRect(DefaultDrawParam defaultDrawParam, SelectedType type) : base()
  170. {
  171. drawParam = defaultDrawParam;
  172. currentDrawPointType = DrawPointType.Circle;
  173. selectedType = type;
  174. }
  175. public void SetMulitSelectedRect(SelectedRect selectedobject)
  176. {
  177. selectedRects.Add(selectedobject);
  178. }
  179. /// <summary>
  180. /// delete
  181. /// </summary>
  182. /// <param name="selectedobject"></param>
  183. public void DelMulitSelectedRect(SelectedRect selectedobject)
  184. {
  185. selectedRects.Remove(selectedobject);
  186. }
  187. /// <summary>
  188. /// get selectedRects Index
  189. /// </summary>
  190. /// <param name="selectedobject"></param>
  191. /// <returns></returns>
  192. public int GetMulitSelectedRectIndex(SelectedRect selectedobject)
  193. {
  194. return selectedRects.IndexOf(selectedobject);
  195. }
  196. public SelectedType GetSelectedType()
  197. {
  198. return selectedType;
  199. }
  200. public void SetSelectedType(SelectedType type)
  201. {
  202. if (selectedType != type)
  203. {
  204. selectedRects.Clear();
  205. }
  206. selectedType = type;
  207. }
  208. public void CleanMulitSelectedRect()
  209. {
  210. selectedRects.Clear();
  211. }
  212. public virtual void OnMouseLeftButtonDown(Point downPoint)
  213. {
  214. isMouseDown = true;
  215. hitControlType = PointControlType.None;
  216. mouseDownPoint = downPoint;
  217. moveOffset = new Point(0, 0);
  218. HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
  219. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  220. {
  221. hitControlType = GetHitControlIndex(downPoint);
  222. if (hitControlType != PointControlType.None)
  223. {
  224. cacheRect = drawRect;
  225. }
  226. }
  227. }
  228. public virtual void OnMouseLeftButtonUp(Point upPoint)
  229. {
  230. if (isMouseDown && hitControlType != PointControlType.None)
  231. {
  232. isMouseDown = false;
  233. cacheRect = setDrawRect = drawRect;
  234. Draw();
  235. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  236. {
  237. InvokeDataChangEvent(true);
  238. }
  239. }
  240. moveOffset = new Point(0, 0);
  241. }
  242. public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
  243. {
  244. PDFViewerActualWidth = width;
  245. PDFViewerActualHeight = height;
  246. Tag = false;
  247. if (isMouseDown && hitControlType != PointControlType.None)
  248. {
  249. Tag = isMouseDown;
  250. if (CalcHitPointMove(mousePoint))
  251. {
  252. Draw();
  253. if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
  254. {
  255. InvokeDataChangEvent(false);
  256. }
  257. }
  258. }
  259. }
  260. public float GetZoomX()
  261. {
  262. return (float)(drawRect.Width / drawDefaultRect.Width);
  263. }
  264. public float GetZoomY()
  265. {
  266. return (float)(drawRect.Height / drawDefaultRect.Height);
  267. }
  268. public void Draw()
  269. {
  270. switch (currentDrawType)
  271. {
  272. case MulitiDrawMoveType.Default:
  273. Dispatcher.Invoke(() =>
  274. {
  275. if (drawDefaultRect.IsEmpty == false && drawDefaultRect.Width > 0 && drawDefaultRect.Height > 0)
  276. {
  277. drawDC = RenderOpen();
  278. CalcControlPoint(drawDefaultRect);
  279. SolidColorBrush solidColorBrush = drawParam.SPDFEditMultiRectFillBrush;
  280. Pen pen = drawParam.SPDFEditMultiRectLinePen;
  281. GetBrushAndPen(ref solidColorBrush, ref pen);
  282. if (selectedRects.Count >= 1)
  283. {
  284. foreach (SelectedRect item in selectedRects)
  285. {
  286. Rect rect = item.GetRect();
  287. rect.X -= rectPadding;
  288. rect.Y -= rectPadding;
  289. rect.Width += rectPadding;
  290. rect.Height += rectPadding;
  291. drawDC?.DrawRectangle(solidColorBrush, pen, rect);
  292. }
  293. }
  294. SolidColorBrush PointBrush = drawParam.PDFEditMultiPointBorderBrush;
  295. Pen PointPen = drawParam.PDFEditMultiPointPen;
  296. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  297. switch (currentDrawMoveType)
  298. {
  299. case DrawMoveType.kDefault:
  300. break;
  301. case DrawMoveType.kReferenceLine:
  302. if (isMouseDown == true)
  303. {
  304. SolidColorBrush moveBrush = drawParam.PDFEditMultiMoveBrush;
  305. Pen movepen = drawParam.PDFEditMultiMovePen;
  306. GetMoveBrushAndPen(ref moveBrush, ref movepen);
  307. if (selectedType == SelectedType.PDFEdit)
  308. {
  309. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect, drawParam.PDFEditMultiMoveRectPen);
  310. }
  311. else
  312. {
  313. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  314. }
  315. }
  316. drawDC?.DrawRectangle(solidColorBrush, pen, drawDefaultRect);
  317. break;
  318. default:
  319. break;
  320. }
  321. switch (currentDrawPointType)
  322. {
  323. case DrawPointType.Circle:
  324. if (selectedType == SelectedType.PDFEdit)
  325. {
  326. //Edit Settings Frame
  327. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, new SolidColorBrush(Color.FromRgb(71, 126, 222)));
  328. }
  329. else
  330. {
  331. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  332. }
  333. break;
  334. case DrawPointType.Square:
  335. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  336. break;
  337. }
  338. drawDC?.Close();
  339. drawDC = null;
  340. }
  341. });
  342. break;
  343. case MulitiDrawMoveType.Alone:
  344. CalcControlPoint(drawDefaultRect);
  345. foreach (SelectedRect selectRect in selectedRects)
  346. {
  347. selectRect.Draw();
  348. }
  349. break;
  350. default:
  351. break;
  352. }
  353. }
  354. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  355. {
  356. switch (selectedType)
  357. {
  358. case SelectedType.None:
  359. break;
  360. case SelectedType.Annot:
  361. //colorBrush = DrawParam.AnnotMoveBrush;
  362. //pen = DrawParam.AnnotMovePen;
  363. break;
  364. case SelectedType.PDFEdit:
  365. colorBrush = drawParam.PDFEditMultiMoveBrush;
  366. pen = drawParam.PDFEditMultiMovePen;
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  373. {
  374. switch (selectedType)
  375. {
  376. case SelectedType.None:
  377. break;
  378. case SelectedType.Annot:
  379. //if (isHover)
  380. //{
  381. // colorBrush = DrawParam.AnnotRectFillBrush;
  382. // pen = DrawParam.AnnotRectHoverPen;
  383. //}
  384. //else
  385. //{
  386. // colorBrush = DrawParam.AnnotRectFillBrush;
  387. // pen = DrawParam.AnnotRectLinePen;
  388. //}
  389. break;
  390. case SelectedType.PDFEdit:
  391. if (isHover)
  392. {
  393. colorBrush = drawParam.PDFEditMultiRectFillHoverBrush;
  394. pen = drawParam.PDFEditMultiRectLineHoverPen;
  395. }
  396. else
  397. {
  398. if (isSelected)
  399. {
  400. colorBrush = drawParam.SPDFEditMultiRectFillBrush;
  401. pen = drawParam.SPDFEditMultiRectLinePen;
  402. }
  403. else
  404. {
  405. colorBrush = drawParam.PDFEditMultiRectFillBrush;
  406. pen = drawParam.PDFEditMultiRectLinePen;
  407. }
  408. }
  409. break;
  410. default:
  411. break;
  412. }
  413. }
  414. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  415. {
  416. switch (selectedType)
  417. {
  418. case SelectedType.None:
  419. break;
  420. case SelectedType.Annot:
  421. //colorBrush = DrawParam.AnnotPointBorderBrush;
  422. //pen = DrawParam.AnnotPointPen;
  423. break;
  424. case SelectedType.PDFEdit:
  425. if (isHover)
  426. {
  427. colorBrush = drawParam.PDFEditMultiRectFillHoverBrush;
  428. pen = drawParam.PDFEditMultiPointHoverPen;
  429. }
  430. else
  431. {
  432. if (isSelected)
  433. {
  434. colorBrush = drawParam.SPDFEditMultiPointBorderBrush;
  435. pen = drawParam.SPDFEditMultiPointPen;
  436. }
  437. else
  438. {
  439. colorBrush = drawParam.PDFEditMultiPointBorderBrush;
  440. pen = drawParam.PDFEditMultiPointPen;
  441. }
  442. }
  443. break;
  444. default:
  445. break;
  446. }
  447. }
  448. /// <summary>
  449. /// Internal drawing circle point
  450. /// </summary>
  451. /// <param name="drawingContext">
  452. /// Drawing context
  453. /// </param>
  454. /// <param name="ignoreList">
  455. ///Collection of positions where points need to be drawn
  456. /// </param>
  457. /// <param name="PointSize">
  458. /// Point size
  459. /// </param>
  460. /// <param name="PointPen">
  461. /// Drawing point brush
  462. /// </param>
  463. /// <param name="BorderBrush">
  464. /// Brush for drawing point border
  465. /// </param>
  466. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  467. {
  468. GeometryGroup controlGroup = new GeometryGroup();
  469. controlGroup.FillRule = FillRule.Nonzero;
  470. List<Point> ignorePointsList = new List<Point>();
  471. // Get specific points
  472. foreach (PointControlType type in ignoreList)
  473. {
  474. if ((int)type < controlPoints.Count)
  475. {
  476. ignorePointsList.Add(controlPoints[(int)type]);
  477. }
  478. }
  479. for (int i = 0; i < controlPoints.Count; i++)
  480. {
  481. Point controlPoint = controlPoints[i];
  482. if (ignorePointsList.Contains(controlPoint))
  483. {
  484. continue;
  485. }
  486. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  487. controlGroup.Children.Add(circlPoint);
  488. }
  489. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  490. }
  491. /// <summary>
  492. /// Internal drawing square
  493. /// </summary>
  494. /// <param name="drawingContext">
  495. /// Drawing context
  496. /// </param>
  497. /// <param name="ControlPoints">
  498. /// Collection of positions where points need to be drawn
  499. /// </param>
  500. /// <param name="PointSize">
  501. /// Size of the point
  502. /// </param>
  503. /// <param name="PointPen">
  504. /// Brush for drawing point
  505. /// </param>
  506. /// <param name="BorderBrush">
  507. /// Border brush for drawing points
  508. /// </param>
  509. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  510. {
  511. GeometryGroup controlGroup = new GeometryGroup();
  512. controlGroup.FillRule = FillRule.Nonzero;
  513. List<Point> ignorePointsList = new List<Point>();
  514. // Get specific points
  515. foreach (PointControlType type in ignoreList)
  516. {
  517. if ((int)type < controlPoints.Count)
  518. {
  519. ignorePointsList.Add(controlPoints[(int)type]);
  520. }
  521. }
  522. for (int i = 0; i < controlPoints.Count; i++)
  523. {
  524. Point controlPoint = controlPoints[i];
  525. if (ignorePointsList.Contains(controlPoint))
  526. {
  527. continue;
  528. }
  529. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  530. PointSize * 2, PointSize * 2), 1, 1);
  531. controlGroup.Children.Add(rectPoint);
  532. }
  533. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  534. }
  535. /// <summary>
  536. /// Draw the reference line in the moving state
  537. /// </summary>
  538. /// <param name="drawDc">
  539. /// Drawing context
  540. /// </param>
  541. /// <param name="controltype">
  542. /// Drawing context
  543. /// </param>
  544. /// <param name="activePen">
  545. /// Drawing context
  546. /// </param>
  547. /// <param name="moveBrush">
  548. /// Move brush
  549. /// </param>
  550. /// <param name="moveRect">
  551. /// Move rectangle
  552. /// </param>
  553. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect, Pen RectPen = null)
  554. {
  555. switch (controltype)
  556. {
  557. case PointControlType.LeftTop:
  558. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  559. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  560. break;
  561. case PointControlType.LeftMiddle:
  562. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  563. break;
  564. case PointControlType.LeftBottom:
  565. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  566. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  567. break;
  568. case PointControlType.MiddlBottom:
  569. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  570. break;
  571. case PointControlType.RightBottom:
  572. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  573. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  574. break;
  575. case PointControlType.RightMiddle:
  576. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  577. break;
  578. case PointControlType.RightTop:
  579. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  580. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  581. break;
  582. case PointControlType.MiddleTop:
  583. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  584. break;
  585. case PointControlType.Rotate:
  586. break;
  587. case PointControlType.Body:
  588. case PointControlType.Line:
  589. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(moveRect.Left, moveRect.Top));
  590. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  591. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Top), new Point(moveRect.Left, 0));
  592. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(moveRect.Right, 0));
  593. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(moveRect.Left, moveRect.Bottom));
  594. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  595. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Bottom), new Point(moveRect.Left, PDFViewerActualHeight));
  596. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(moveRect.Right, PDFViewerActualHeight));
  597. break;
  598. default:
  599. break;
  600. }
  601. drawDc?.DrawRectangle(moveBrush, RectPen, moveRect);
  602. }
  603. public virtual void ClearDraw()
  604. {
  605. drawDefaultRect = setDrawRect = drawRect = new Rect();
  606. drawDC = RenderOpen();
  607. drawDC?.Close();
  608. drawDC = null;
  609. }
  610. /// <summary>
  611. /// Calculate the current control point
  612. /// </summary>
  613. /// <param name="currentRect">
  614. /// Target rectangle where the control point is located
  615. /// </param>
  616. protected void CalcControlPoint(Rect currentRect)
  617. {
  618. controlPoints.Clear();
  619. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  620. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  621. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  622. controlPoints.Add(new Point(currentRect.Left, centerY));
  623. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  624. controlPoints.Add(new Point(centerX, currentRect.Bottom));
  625. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  626. controlPoints.Add(new Point(currentRect.Right, centerY));
  627. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  628. controlPoints.Add(new Point(centerX, currentRect.Top));
  629. }
  630. /// <summary>
  631. /// Get the original set Rect, not the one that has been calculated for padding
  632. /// </summary>
  633. public Rect GetRect()
  634. {
  635. Rect rect = new Rect(drawRect.X + rectPadding, drawRect.Y + rectPadding, Math.Max(0, drawRect.Width - 2 * rectPadding), Math.Max(0, drawRect.Height - 2 * rectPadding));
  636. return rect;
  637. }
  638. public void SetRect(Rect newRect)
  639. {
  640. newRect = new Rect(newRect.X - rectPadding, newRect.Y - rectPadding, newRect.Width + 2 * rectPadding, newRect.Height + 2 * rectPadding);
  641. if (drawDefaultRect != new Rect())
  642. {
  643. newRect.Union(drawDefaultRect);
  644. newRect.Intersect(maxRect);
  645. }
  646. drawDefaultRect = newRect;
  647. setDrawRect = drawRect = newRect;
  648. }
  649. public void SetRectPadding(double rectPadding)
  650. {
  651. this.rectPadding = rectPadding;
  652. }
  653. public double GetRectPadding()
  654. {
  655. return rectPadding;
  656. }
  657. public Rect GetDrawRect()
  658. {
  659. return drawRect;
  660. }
  661. public void SetMaxRect(Rect rect)
  662. {
  663. maxRect = rect;
  664. }
  665. public Rect GetMaxRect()
  666. {
  667. return maxRect;
  668. }
  669. public void SetIsProportionalScaling(bool isProportionalScaling)
  670. {
  671. this.isProportionalScaling = isProportionalScaling;
  672. }
  673. public void SetDrawType(DrawPointType drawType)
  674. {
  675. currentDrawPointType = drawType;
  676. }
  677. public void SetDrawMoveType(DrawMoveType drawType)
  678. {
  679. currentDrawMoveType = drawType;
  680. }
  681. /// <summary>
  682. /// Set the type that needs to be ignored
  683. /// </summary>
  684. /// <param name="types">
  685. /// Collection of point types that need to be shielded
  686. /// </param>
  687. public void SetIgnorePoints(List<PointControlType> types)
  688. {
  689. ignorePoints.Clear();
  690. foreach (PointControlType type in types)
  691. {
  692. ignorePoints.Add(type);
  693. }
  694. }
  695. /// <summary>
  696. /// Set all points to be ignored
  697. /// </summary>
  698. public void SetIgnorePointsAll()
  699. {
  700. ignorePoints.Clear();
  701. ignorePoints.Add(PointControlType.LeftTop);
  702. ignorePoints.Add(PointControlType.LeftMiddle);
  703. ignorePoints.Add(PointControlType.LeftBottom);
  704. ignorePoints.Add(PointControlType.MiddlBottom);
  705. ignorePoints.Add(PointControlType.RightBottom);
  706. ignorePoints.Add(PointControlType.RightMiddle);
  707. ignorePoints.Add(PointControlType.RightTop);
  708. ignorePoints.Add(PointControlType.MiddleTop);
  709. }
  710. /// <summary>
  711. /// Disable all functions
  712. /// </summary>
  713. public void DisableAll()
  714. {
  715. ignorePoints.Clear();
  716. ignorePoints.Add(PointControlType.LeftTop);
  717. ignorePoints.Add(PointControlType.LeftMiddle);
  718. ignorePoints.Add(PointControlType.LeftBottom);
  719. ignorePoints.Add(PointControlType.MiddlBottom);
  720. ignorePoints.Add(PointControlType.RightBottom);
  721. ignorePoints.Add(PointControlType.RightMiddle);
  722. ignorePoints.Add(PointControlType.RightTop);
  723. ignorePoints.Add(PointControlType.MiddleTop);
  724. ignorePoints.Add(PointControlType.Rotate);
  725. ignorePoints.Add(PointControlType.Body);
  726. ignorePoints.Add(PointControlType.Line);
  727. }
  728. /// <summary>
  729. /// Calculate the movement of the hit point
  730. /// </summary>
  731. /// <param name="mousePoint">
  732. /// Current mouse position
  733. /// </param>
  734. /// <returns></returns>
  735. protected bool CalcHitPointMove(Point mousePoint)
  736. {
  737. if (isMouseDown == false || hitControlType == PointControlType.None)
  738. {
  739. return false;
  740. }
  741. return NormalScaling(mousePoint);
  742. }
  743. /// <summary>
  744. /// Draw the algorithm of the normal scaling form (drag a point, only scale in one direction)
  745. /// </summary>
  746. /// <param name="mousePoint">
  747. /// Current mouse position
  748. /// </param>
  749. /// <returns></returns>
  750. protected bool NormalScaling(Point mousePoint)
  751. {
  752. double mLeft = cacheRect.Left;
  753. double mRight = cacheRect.Right;
  754. double mUp = cacheRect.Top;
  755. double mDown = cacheRect.Bottom;
  756. double TmpLeft = mLeft, TmpRight = mRight, TmpUp = mUp, TmpDown = mDown;
  757. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  758. Point moveVector = (Point)(mousePoint - centerPoint);
  759. moveVector = ProportionalScalingOffsetPos(moveVector);
  760. switch (hitControlType)
  761. {
  762. case PointControlType.LeftTop:
  763. TmpLeft = centerPoint.X + moveVector.X;
  764. TmpRight = cacheRect.Right;
  765. if (TmpLeft + rectMinWidth > TmpRight)
  766. {
  767. TmpLeft = TmpRight - rectMinWidth;
  768. }
  769. TmpUp = centerPoint.Y + moveVector.Y;
  770. TmpDown = cacheRect.Bottom;
  771. if (TmpUp + rectMinHeight > TmpDown)
  772. {
  773. TmpUp = TmpDown - rectMinHeight;
  774. }
  775. break;
  776. case PointControlType.LeftMiddle:
  777. TmpLeft = centerPoint.X + moveVector.X;
  778. TmpRight = cacheRect.Right;
  779. if (TmpLeft + rectMinWidth > TmpRight)
  780. {
  781. TmpLeft = TmpRight - rectMinWidth;
  782. }
  783. TmpUp = cacheRect.Top;
  784. TmpDown = cacheRect.Bottom;
  785. break;
  786. case PointControlType.LeftBottom:
  787. TmpLeft = centerPoint.X + moveVector.X;
  788. TmpRight = cacheRect.Right;
  789. if (TmpLeft + rectMinWidth > TmpRight)
  790. {
  791. TmpLeft = TmpRight - rectMinWidth;
  792. }
  793. TmpUp = cacheRect.Top;
  794. TmpDown = centerPoint.Y + moveVector.Y;
  795. if (TmpUp + rectMinHeight > TmpDown)
  796. {
  797. TmpDown = TmpUp + rectMinHeight;
  798. }
  799. break;
  800. case PointControlType.MiddlBottom:
  801. TmpLeft = cacheRect.Left;
  802. TmpRight = cacheRect.Right;
  803. TmpUp = cacheRect.Top;
  804. TmpDown = centerPoint.Y + moveVector.Y;
  805. if (TmpUp + rectMinHeight > TmpDown)
  806. {
  807. TmpDown = TmpUp + rectMinHeight;
  808. }
  809. break;
  810. case PointControlType.RightBottom:
  811. TmpLeft = cacheRect.Left;
  812. TmpRight = centerPoint.X + moveVector.X;
  813. if (TmpLeft + rectMinWidth > TmpRight)
  814. {
  815. TmpRight = TmpLeft + rectMinWidth;
  816. }
  817. TmpUp = cacheRect.Top;
  818. TmpDown = centerPoint.Y + moveVector.Y;
  819. if (TmpUp + rectMinHeight > TmpDown)
  820. {
  821. TmpDown = TmpUp + rectMinHeight;
  822. }
  823. break;
  824. case PointControlType.RightMiddle:
  825. TmpLeft = cacheRect.Left;
  826. TmpRight = centerPoint.X + moveVector.X;
  827. if (TmpLeft + rectMinWidth > TmpRight)
  828. {
  829. TmpRight = TmpLeft + rectMinWidth;
  830. }
  831. TmpUp = cacheRect.Top;
  832. TmpDown = cacheRect.Bottom;
  833. break;
  834. case PointControlType.RightTop:
  835. TmpLeft = cacheRect.Left;
  836. TmpRight = centerPoint.X + moveVector.X;
  837. if (TmpLeft + rectMinWidth > TmpRight)
  838. {
  839. TmpRight = TmpLeft + rectMinWidth;
  840. }
  841. TmpUp = centerPoint.Y + moveVector.Y;
  842. TmpDown = cacheRect.Bottom;
  843. if (TmpUp + rectMinHeight > TmpDown)
  844. {
  845. TmpUp = TmpDown - rectMinHeight;
  846. }
  847. break;
  848. case PointControlType.MiddleTop:
  849. TmpLeft = cacheRect.Left;
  850. TmpRight = cacheRect.Right;
  851. TmpUp = centerPoint.Y + moveVector.Y;
  852. TmpDown = cacheRect.Bottom;
  853. if (TmpUp + rectMinHeight > TmpDown)
  854. {
  855. TmpUp = TmpDown - rectMinHeight;
  856. }
  857. break;
  858. case PointControlType.Body:
  859. case PointControlType.Line:
  860. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  861. TmpLeft = cacheRect.Left + OffsetPos.X;
  862. TmpRight = cacheRect.Right + OffsetPos.X;
  863. TmpUp = cacheRect.Top + OffsetPos.Y;
  864. TmpDown = cacheRect.Bottom + OffsetPos.Y;
  865. break;
  866. default:
  867. break;
  868. }
  869. if (TmpLeft < maxRect.Left)
  870. {
  871. TmpLeft = maxRect.Left;
  872. }
  873. if (TmpUp < maxRect.Top)
  874. {
  875. TmpUp = maxRect.Top;
  876. }
  877. if (TmpRight > maxRect.Right)
  878. {
  879. TmpRight = maxRect.Right;
  880. }
  881. if (TmpDown > maxRect.Bottom)
  882. {
  883. TmpDown = maxRect.Bottom;
  884. }
  885. if (TmpRight - TmpLeft < 0.0 || TmpDown - TmpUp < 0.0)
  886. {
  887. return false;
  888. }
  889. drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  890. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  891. return true;
  892. }
  893. /// <summary>
  894. /// Proportional scaling offset calibration
  895. /// </summary>
  896. /// <param name="movePoint">
  897. /// Offset value
  898. /// </param>
  899. /// <returns>
  900. /// Offset value after calibration
  901. /// </returns>
  902. protected Point ProportionalScalingOffsetPos(Point movePoint)
  903. {
  904. if (isProportionalScaling)
  905. {
  906. Point offsetPos = movePoint;
  907. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  908. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  909. switch (hitControlType)
  910. {
  911. case PointControlType.LeftTop:
  912. case PointControlType.RightBottom:
  913. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  914. break;
  915. case PointControlType.LeftBottom:
  916. case PointControlType.RightTop:
  917. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  918. break;
  919. case PointControlType.LeftMiddle:
  920. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  921. break;
  922. case PointControlType.RightMiddle:
  923. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  924. break;
  925. case PointControlType.MiddlBottom:
  926. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  927. break;
  928. case PointControlType.MiddleTop:
  929. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  930. break;
  931. default:
  932. break;
  933. }
  934. return offsetPos;
  935. }
  936. else
  937. {
  938. return movePoint;
  939. }
  940. }
  941. /// <summary>
  942. /// Set left alignment within the set maximum rectangle
  943. /// </summary>
  944. public virtual void SetAlignLeftForMaxRect()
  945. {
  946. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  947. }
  948. /// <summary>
  949. /// Set horizontal center alignment within the set maximum rectangle
  950. /// </summary>
  951. public virtual void SetAlignHorizonCenterForMaxRect()
  952. {
  953. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  954. }
  955. /// <summary>
  956. /// Set right alignment within the set maximum rectangle
  957. /// </summary>
  958. public virtual void SetAlignRightForMaxRect()
  959. {
  960. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  961. }
  962. /// <summary>
  963. /// Set top alignment within the set maximum rectangle
  964. /// </summary>
  965. public virtual void SetAlignTopForMaxRect()
  966. {
  967. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  968. }
  969. /// <summary>
  970. /// Set vertical center alignment within the set maximum rectangle
  971. /// </summary>
  972. public virtual void SetAlignVerticalCenterForMaxRect()
  973. {
  974. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  975. }
  976. /// <summary>
  977. /// Set Align center within the set maximum rectangle
  978. /// </summary>
  979. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  980. {
  981. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  982. }
  983. /// <summary>
  984. /// Set bottom alignment within the set maximum rectangle
  985. /// </summary>
  986. public virtual void SetAlignBottomForMaxRect()
  987. {
  988. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  989. }
  990. /// <summary>
  991. /// Draw the rectangle of the alignment function
  992. /// </summary>
  993. /// <param name="RectMovePoint">
  994. /// Move distance required for the rectangle obtained by the alignment algorithm
  995. /// </param>
  996. private void DrawAlignRect(Point RectMovePoint)
  997. {
  998. double TmpLeft, TmpRight, TmpUp, TmpDown;
  999. Point OffsetPos = CalcMoveBound(drawRect, RectMovePoint, maxRect);
  1000. TmpLeft = drawRect.Left + OffsetPos.X;
  1001. TmpRight = drawRect.Right + OffsetPos.X;
  1002. TmpUp = drawRect.Top + OffsetPos.Y;
  1003. TmpDown = drawRect.Bottom + OffsetPos.Y;
  1004. setDrawRect = drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  1005. Draw();
  1006. }
  1007. /// <summary>
  1008. /// Calculate the offset of the current rectangle within the maximum rectangle range
  1009. /// </summary>
  1010. /// <param name="currentRect">
  1011. /// The rectangle cached when pressed
  1012. /// </param>
  1013. /// <param name="offsetPoint">
  1014. /// The offset value equivalent to when pressed
  1015. /// </param>
  1016. /// <param name="maxRect">
  1017. /// The maximum rectangle range
  1018. /// </param>
  1019. /// <returns>
  1020. /// Offset value after calculation
  1021. /// </returns>
  1022. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  1023. {
  1024. double cLeft = currentRect.Left;
  1025. double cRight = currentRect.Right;
  1026. double cUp = currentRect.Top;
  1027. double cDown = currentRect.Bottom;
  1028. double TmpLeft = cLeft + offsetPoint.X;
  1029. double TmpRight = cRight + offsetPoint.X;
  1030. double TmpUp = cUp + offsetPoint.Y;
  1031. double TmpDown = cDown + offsetPoint.Y;
  1032. if (TmpLeft < maxRect.Left)
  1033. {
  1034. TmpRight = (cRight - cLeft) + maxRect.Left;
  1035. TmpLeft = maxRect.Left;
  1036. }
  1037. if (TmpUp < maxRect.Top)
  1038. {
  1039. TmpDown = (cDown - cUp) + maxRect.Top;
  1040. TmpUp = maxRect.Top;
  1041. }
  1042. if (TmpRight > maxRect.Right)
  1043. {
  1044. TmpLeft = maxRect.Right - (cRight - cLeft);
  1045. TmpRight = maxRect.Right;
  1046. }
  1047. if (TmpDown > maxRect.Bottom)
  1048. {
  1049. TmpUp = maxRect.Bottom - (cDown - cUp);
  1050. TmpDown = maxRect.Bottom;
  1051. }
  1052. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  1053. return offsetPoint;
  1054. }
  1055. /// <summary>
  1056. /// Used for notification events during the drawing data process/completion.
  1057. /// </summary>
  1058. /// <param name="isFinish">
  1059. /// Identifies whether the data has been changed
  1060. /// </param>
  1061. protected void InvokeDataChangEvent(bool isFinish)
  1062. {
  1063. if (isFinish)
  1064. {
  1065. DataChanged?.Invoke(this, moveOffset);
  1066. }
  1067. else
  1068. {
  1069. DataChanging?.Invoke(this, moveOffset);
  1070. }
  1071. }
  1072. /// <summary>
  1073. /// Get the current set of ignored points
  1074. /// </summary>
  1075. /// <returns>
  1076. /// Dataset of ignored points
  1077. /// </returns>
  1078. private List<PointControlType> GetIgnorePoints()
  1079. {
  1080. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  1081. foreach (PointControlType type in ignorePoints)
  1082. {
  1083. IgnorePointsList.Add(type);
  1084. }
  1085. return IgnorePointsList;
  1086. }
  1087. /// <summary>
  1088. /// Get which control point the coordinate is on
  1089. /// </summary>
  1090. /// <param name="clickPoint">
  1091. /// Coordinate point
  1092. /// </param>
  1093. /// <returns>
  1094. /// Control point type
  1095. /// </returns>
  1096. public PointControlType GetHitControlIndex(Point point)
  1097. {
  1098. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  1099. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  1100. {
  1101. List<PointControlType> ignoreList = GetIgnorePoints();
  1102. List<Point> IgnorePointsList = new List<Point>();
  1103. foreach (PointControlType type in ignoreList)
  1104. {
  1105. if ((int)type < controlPoints.Count)
  1106. {
  1107. IgnorePointsList.Add(controlPoints[(int)type]);
  1108. }
  1109. }
  1110. for (int i = 0; i < controlPoints.Count; i++)
  1111. {
  1112. Point checkPoint = controlPoints[i];
  1113. if (IgnorePointsList.Contains(checkPoint))
  1114. {
  1115. continue;
  1116. }
  1117. switch (currentDrawPointType)
  1118. {
  1119. case DrawPointType.Circle:
  1120. Vector checkVector = checkPoint - point;
  1121. if (checkVector.Length < pointSize)
  1122. {
  1123. return (PointControlType)i;
  1124. }
  1125. break;
  1126. case DrawPointType.Square:
  1127. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  1128. if (checkRect.Contains(point))
  1129. {
  1130. return (PointControlType)i;
  1131. }
  1132. break;
  1133. default:
  1134. break;
  1135. }
  1136. }
  1137. Rect defrect = drawDefaultRect;
  1138. defrect.X -= rectPadding;
  1139. defrect.Y -= rectPadding;
  1140. defrect.Width += rectPadding;
  1141. defrect.Height += rectPadding;
  1142. if (drawDefaultRect.Contains(point))
  1143. {
  1144. Rect rect = new Rect(
  1145. Math.Max(drawDefaultRect.X + rectPadding, 0),
  1146. Math.Max(drawDefaultRect.Y + rectPadding, 0),
  1147. drawDefaultRect.Width - 2 * rectPadding,
  1148. drawDefaultRect.Height - 2 * rectPadding);
  1149. if (rect.Contains(point))
  1150. {
  1151. if (!ignoreList.Contains(PointControlType.Body))
  1152. {
  1153. return PointControlType.Body;
  1154. }
  1155. }
  1156. if (!ignoreList.Contains(PointControlType.Body))
  1157. {
  1158. return PointControlType.Line;
  1159. }
  1160. }
  1161. }
  1162. return PointControlType.None;
  1163. }
  1164. /// <summary>
  1165. /// Get the rectangle where the current point is located
  1166. /// </summary>
  1167. /// <param name="clickPoint">
  1168. /// Coordinate point
  1169. /// </param>
  1170. /// <returns>
  1171. /// Control point type
  1172. /// </returns>
  1173. public SelectedRect GetHitControlRect(Point point)
  1174. {
  1175. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  1176. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  1177. {
  1178. foreach (SelectedRect selectedRect in selectedRects) {
  1179. Rect defrect = selectedRect.GetRect();
  1180. defrect.X -= rectPadding;
  1181. defrect.Y -= rectPadding;
  1182. defrect.Width += rectPadding;
  1183. defrect.Height += rectPadding;
  1184. if (defrect.Contains(point))
  1185. {
  1186. Rect rect = new Rect(
  1187. Math.Max(defrect.X + rectPadding, 0),
  1188. Math.Max(defrect.Y + rectPadding, 0),
  1189. defrect.Width - 2 * rectPadding,
  1190. defrect.Height - 2 * rectPadding);
  1191. if (rect.Contains(point))
  1192. {
  1193. return selectedRect;
  1194. }
  1195. }
  1196. }
  1197. }
  1198. return null;
  1199. }
  1200. }
  1201. }