MultiSelectedRect.cs 50 KB

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