MultiSelectedRect.cs 47 KB

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