MultiSelectedRect.cs 50 KB

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