MultiSelectedRect.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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, PointBrush);
  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.MiddleBottom:
  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.MiddleBottom);
  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.MiddleBottom);
  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.MiddleBottom:
  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.MiddleBottom:
  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. if (IgnorePointsList.Contains(checkPoint))
  1159. {
  1160. continue;
  1161. }
  1162. Vector checkVector = checkPoint - point;
  1163. double wlen = drawRect.Width;
  1164. if (wlen > 50)
  1165. {
  1166. wlen = 20;
  1167. }
  1168. else
  1169. {
  1170. wlen = wlen / 3;
  1171. }
  1172. double hlen = drawRect.Height;
  1173. if (hlen > 50)
  1174. {
  1175. hlen = 20;
  1176. }
  1177. else
  1178. {
  1179. hlen = wlen / 3;
  1180. }
  1181. if ((PointControlType)i == PointControlType.RightMiddle)
  1182. {
  1183. if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height / 3)
  1184. {
  1185. return (PointControlType)i;
  1186. }
  1187. }
  1188. if ((PointControlType)i == PointControlType.LeftMiddle)
  1189. {
  1190. if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height / 3)
  1191. {
  1192. return (PointControlType)i;
  1193. }
  1194. }
  1195. if ((PointControlType)i == PointControlType.MiddleTop)
  1196. {
  1197. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width / 3)
  1198. {
  1199. return (PointControlType)i;
  1200. }
  1201. }
  1202. if ((PointControlType)i == PointControlType.MiddleBottom)
  1203. {
  1204. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width / 3)
  1205. {
  1206. return (PointControlType)i;
  1207. }
  1208. }
  1209. if (checkVector.Length < pointSize)
  1210. {
  1211. return (PointControlType)i;
  1212. }
  1213. break;
  1214. case DrawPointType.Square:
  1215. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  1216. if (checkRect.Contains(point))
  1217. {
  1218. return (PointControlType)i;
  1219. }
  1220. break;
  1221. default:
  1222. break;
  1223. }
  1224. }
  1225. Rect defrect = drawRect;
  1226. defrect.X -= rectPadding;
  1227. defrect.Y -= rectPadding;
  1228. defrect.Width += rectPadding;
  1229. defrect.Height += rectPadding;
  1230. if (drawRect.Contains(point))
  1231. {
  1232. Rect rect = new Rect(
  1233. Math.Max(drawRect.X + rectPadding, 0),
  1234. Math.Max(drawRect.Y + rectPadding, 0),
  1235. drawRect.Width - 2 * rectPadding,
  1236. drawRect.Height - 2 * rectPadding);
  1237. if (rect.Contains(point))
  1238. {
  1239. if (!ignoreList.Contains(PointControlType.Body))
  1240. {
  1241. return PointControlType.Body;
  1242. }
  1243. }
  1244. if (!ignoreList.Contains(PointControlType.Body))
  1245. {
  1246. return PointControlType.Line;
  1247. }
  1248. }
  1249. }
  1250. return PointControlType.None;
  1251. }
  1252. /// <summary>
  1253. /// Get the rectangle where the current point is located
  1254. /// </summary>
  1255. /// <param name="clickPoint">
  1256. /// Coordinate point
  1257. /// </param>
  1258. /// <returns>
  1259. /// Control point type
  1260. /// </returns>
  1261. public SelectedRect GetHitControlRect(Point point)
  1262. {
  1263. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  1264. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  1265. {
  1266. foreach (SelectedRect selectedRect in selectedRects) {
  1267. Rect defrect = selectedRect.GetRect();
  1268. defrect.X -= rectPadding;
  1269. defrect.Y -= rectPadding;
  1270. defrect.Width += rectPadding;
  1271. defrect.Height += rectPadding;
  1272. if (defrect.Contains(point))
  1273. {
  1274. Rect rect = new Rect(
  1275. Math.Max(defrect.X + rectPadding, 0),
  1276. Math.Max(defrect.Y + rectPadding, 0),
  1277. defrect.Width - 2 * rectPadding,
  1278. defrect.Height - 2 * rectPadding);
  1279. if (rect.Contains(point))
  1280. {
  1281. return selectedRect;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. return null;
  1287. }
  1288. }
  1289. }