MultiSelectedRect.cs 45 KB

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