SelectedRect.protected.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. using ComPDFKit.Import;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Windows;
  6. using System.Windows.Media;
  7. using static ComPDFKit.Tool.Help.ImportWin32;
  8. namespace ComPDFKit.Tool.DrawTool
  9. {
  10. public partial class SelectedRect
  11. {
  12. #region Properties
  13. /// <summary>
  14. /// Current control point drawing style.
  15. /// </summary>
  16. protected DrawPointType currentDrawPointType { get; set; }
  17. /// <summary>
  18. /// Current drag drawing style.
  19. /// </summary>
  20. protected DrawMoveType currentDrawMoveType { get; set; }
  21. /// <summary>
  22. /// Current click hit control point.
  23. /// </summary>
  24. protected PointControlType hitControlType { get; set; }
  25. /// <summary>
  26. /// Mouse down position information.
  27. /// </summary>
  28. protected Point mouseDownPoint { get; set; }
  29. /// <summary>
  30. /// Whether the mouse is pressed.
  31. /// </summary>
  32. protected bool isMouseDown { get; set; }
  33. /// <summary>
  34. /// Whether proportional scaling is required.
  35. /// </summary>
  36. protected bool isProportionalScaling { get; set; } = false;
  37. /// <summary>
  38. /// Current control point size.
  39. /// </summary>
  40. protected int pointSize { get; set; } = 4;
  41. /// <summary>
  42. /// Rectangular minimum width.
  43. /// </summary>
  44. protected int rectMinWidth { get; set; } = 10;
  45. /// <summary>
  46. /// Rectangular minimum height.
  47. /// </summary>
  48. protected int RectMinHeight { get; set; } = 10;
  49. /// <summary>
  50. /// Current set of ignore points.
  51. /// </summary>
  52. protected List<PointControlType> ignorePoints { get; set; } = new List<PointControlType>();
  53. /// <summary>
  54. /// Current set of drawing rectangles (original data).
  55. /// </summary>
  56. protected Rect SetDrawRect { get; set; } = new Rect(0, 0, 0, 0);
  57. /// <summary>
  58. /// Current drawing rectangle (calculated during operation).
  59. /// </summary>
  60. protected Rect drawRect { get; set; } = new Rect(0, 0, 0, 0);
  61. /// <summary>
  62. /// Maximum range that can be drawn.
  63. /// </summary>
  64. protected Rect maxRect { get; set; } = new Rect(0, 0, 0, 0);
  65. /// <summary>
  66. /// Current center point of the drawing rectangle.
  67. /// </summary>
  68. protected Point drawCenterPoint { get; private set; } = new Point(0, 0);
  69. /// <summary>
  70. /// When the mouse is pressed, the cached rectangle.
  71. /// </summary>
  72. protected Rect cacheRect { get; set; } = new Rect(0, 0, 0, 0);
  73. /// <summary>
  74. /// Current control point coordinates.
  75. /// </summary>
  76. protected List<Point> controlPoints { get; set; } = new List<Point>();
  77. /// <summary>
  78. /// Move offset during movement.
  79. /// </summary>
  80. protected Point moveOffset { get; set; } = new Point(0, 0);
  81. /// <summary>
  82. /// Current actual display width and height of PDFVIewer.
  83. /// </summary>
  84. protected double PDFViewerActualWidth { get; set; } = 0;
  85. protected double PDFViewerActualHeight { get; set; } = 0;
  86. protected double rectPadding = 6;
  87. protected double currentZoom = 1;
  88. protected SelectedAnnotData selectedRectData = new SelectedAnnotData();
  89. protected bool disable = false;
  90. #endregion
  91. #region Functions
  92. /// <summary>
  93. /// Calcuate the control points
  94. /// </summary>
  95. /// <param name="currentRect">
  96. /// Control points in the target rectangle
  97. /// </param>
  98. protected void CalcControlPoint(Rect currentRect)
  99. {
  100. controlPoints.Clear();
  101. double centerX = (currentRect.Left + currentRect.Right) / 2;
  102. double centerY = (currentRect.Top + currentRect.Bottom) / 2;
  103. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  104. controlPoints.Add(new Point(currentRect.Left, centerY));
  105. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  106. controlPoints.Add(new Point(centerX, currentRect.Bottom));
  107. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  108. controlPoints.Add(new Point(currentRect.Right, centerY));
  109. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  110. controlPoints.Add(new Point(centerX, currentRect.Top));
  111. }
  112. /// <summary>
  113. /// Calcuate the offset of the current rectangle in the maximum rectangle range
  114. /// </summary>
  115. /// <param name="currentRect">
  116. /// The rectangle cached when pressed
  117. /// </param>
  118. /// <param name="offsetPoint">
  119. /// Equivalent to the offset value when pressed
  120. /// </param>
  121. /// <param name="maxRect">
  122. /// The maximum rectangle range.
  123. /// </param>
  124. /// <returns></returns>
  125. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  126. {
  127. double cLeft = currentRect.Left;
  128. double cRight = currentRect.Right;
  129. double cUp = currentRect.Top;
  130. double cDown = currentRect.Bottom;
  131. double TmpLeft = cLeft + offsetPoint.X;
  132. double TmpRight = cRight + offsetPoint.X;
  133. double TmpUp = cUp + offsetPoint.Y;
  134. double TmpDown = cDown + offsetPoint.Y;
  135. if (TmpLeft < maxRect.Left)
  136. {
  137. TmpRight = (cRight - cLeft) + maxRect.Left;
  138. TmpLeft = maxRect.Left;
  139. }
  140. if (TmpUp < maxRect.Top)
  141. {
  142. TmpDown = (cDown - cUp) + maxRect.Top;
  143. TmpUp = maxRect.Top;
  144. }
  145. if (TmpRight > maxRect.Right)
  146. {
  147. TmpLeft = maxRect.Right - (cRight - cLeft);
  148. TmpRight = maxRect.Right;
  149. }
  150. if (TmpDown > maxRect.Bottom)
  151. {
  152. TmpUp = maxRect.Bottom - (cDown - cUp);
  153. TmpDown = maxRect.Bottom;
  154. }
  155. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  156. return offsetPoint;
  157. }
  158. /// <summary>
  159. /// Calculate the movement of the hit point
  160. /// </summary>
  161. /// <param name="mousePoint">
  162. /// Current mouse position
  163. /// </param>
  164. /// <returns>
  165. /// Whether the movement is successful
  166. /// </returns>
  167. protected bool CalcHitPointMove(Point mousePoint)
  168. {
  169. if (isMouseDown == false || hitControlType == PointControlType.None)
  170. {
  171. return false;
  172. }
  173. return NormalScaling(mousePoint);
  174. }
  175. private Size GetProportionalScalingSize(double width, double height)
  176. {
  177. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  178. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  179. if (minWidth > width || minHeight > height)
  180. {
  181. if (cacheRect.Width >= cacheRect.Height)
  182. {
  183. width = cacheRect.Width * minHeight / cacheRect.Height;
  184. height = minHeight;
  185. }
  186. else
  187. {
  188. height = cacheRect.Height * minWidth / cacheRect.Width;
  189. width = minWidth;
  190. }
  191. }
  192. return new Size(width, height);
  193. }
  194. /// <summary>
  195. /// Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
  196. /// </summary>
  197. /// <param name="mousePoint">Current mouse position.</param>
  198. /// <returns></returns>
  199. protected bool NormalScaling(Point mousePoint)
  200. {
  201. if (CanRotate)
  202. {
  203. return RotateScaling(mousePoint);
  204. }
  205. return GenerallyScaling(mousePoint);
  206. }
  207. private bool GenerallyScaling(Point mousePoint)
  208. {
  209. try
  210. {
  211. double left = 0, right = 0, top = 0, bottom = 0;
  212. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  213. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  214. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  215. Point moveVector = (Point)(mousePoint - centerPoint);
  216. moveVector = ProportionalScalingOffsetPos(moveVector);
  217. switch (hitControlType)
  218. {
  219. case PointControlType.LeftTop:
  220. {
  221. left = centerPoint.X + moveVector.X;
  222. right = cacheRect.Right;
  223. top = centerPoint.Y + moveVector.Y;
  224. bottom = cacheRect.Bottom;
  225. if (isProportionalScaling)
  226. {
  227. Size size = GetProportionalScalingSize(right - left, bottom - top);
  228. left = right - size.Width;
  229. top = bottom - size.Height;
  230. if (left < maxRect.Left)
  231. {
  232. double tmpWidth = right - left;
  233. left = maxRect.Left;
  234. double width = right - left;
  235. double height = (bottom - top) * width / tmpWidth;
  236. top = bottom - height;
  237. }
  238. if (top < maxRect.Top)
  239. {
  240. double tmpHeight = bottom - top;
  241. top = maxRect.Top;
  242. double height = bottom - top;
  243. double width = (right - left) * height / tmpHeight;
  244. left = right - width;
  245. }
  246. }
  247. else
  248. {
  249. if (left + minWidth > right)
  250. {
  251. left = right - minWidth;
  252. }
  253. if (top + minHeight > bottom)
  254. {
  255. top = bottom - minHeight;
  256. }
  257. }
  258. }
  259. break;
  260. case PointControlType.LeftMiddle:
  261. {
  262. left = centerPoint.X + moveVector.X;
  263. right = cacheRect.Right;
  264. top = cacheRect.Top;
  265. bottom = cacheRect.Bottom;
  266. if (left + minWidth > right)
  267. {
  268. left = right - minWidth;
  269. }
  270. }
  271. break;
  272. case PointControlType.LeftBottom:
  273. {
  274. left = centerPoint.X + moveVector.X;
  275. right = cacheRect.Right;
  276. top = cacheRect.Top;
  277. bottom = centerPoint.Y + moveVector.Y;
  278. if (isProportionalScaling)
  279. {
  280. Size size = GetProportionalScalingSize(right - left, bottom - top);
  281. left = right - size.Width;
  282. bottom = top + size.Height;
  283. if (left < maxRect.Left)
  284. {
  285. double tmpWidth = right - left;
  286. left = maxRect.Left;
  287. double width = right - left;
  288. double height = (bottom - top) * width / tmpWidth;
  289. bottom = top + height;
  290. }
  291. if (bottom > maxRect.Bottom)
  292. {
  293. double tmpHeight = bottom - top;
  294. bottom = maxRect.Bottom;
  295. double height = bottom - top;
  296. double width = (right - left) * height / tmpHeight;
  297. left = right - width;
  298. }
  299. }
  300. else
  301. {
  302. if (left + minWidth > right)
  303. {
  304. left = right - minWidth;
  305. }
  306. if (top + minHeight > bottom)
  307. {
  308. bottom = top + minHeight;
  309. }
  310. }
  311. }
  312. break;
  313. case PointControlType.MiddlBottom:
  314. {
  315. left = cacheRect.Left;
  316. right = cacheRect.Right;
  317. top = cacheRect.Top;
  318. bottom = centerPoint.Y + moveVector.Y;
  319. if (top + minHeight > bottom)
  320. {
  321. bottom = top + minHeight;
  322. }
  323. }
  324. break;
  325. case PointControlType.RightBottom:
  326. {
  327. left = cacheRect.Left;
  328. right = centerPoint.X + moveVector.X;
  329. top = cacheRect.Top;
  330. bottom = centerPoint.Y + moveVector.Y;
  331. if (isProportionalScaling)
  332. {
  333. Size size = GetProportionalScalingSize(right - left, bottom - top);
  334. right = left + size.Width;
  335. bottom = top + size.Height;
  336. if (right > maxRect.Right)
  337. {
  338. double tmpWidth = right - left;
  339. right = maxRect.Right;
  340. double width = right - left;
  341. double height = (bottom - top) * width / tmpWidth;
  342. bottom = top + height;
  343. }
  344. if (bottom > maxRect.Bottom)
  345. {
  346. double tmpHeight = bottom - top;
  347. bottom = maxRect.Bottom;
  348. double height = bottom - top;
  349. double width = (right - left) * height / tmpHeight;
  350. right = left + width;
  351. }
  352. }
  353. else
  354. {
  355. if (left + minWidth > right)
  356. {
  357. right = left + minWidth;
  358. }
  359. if (top + minHeight > bottom)
  360. {
  361. bottom = top + minHeight;
  362. }
  363. }
  364. }
  365. break;
  366. case PointControlType.RightMiddle:
  367. {
  368. left = cacheRect.Left;
  369. right = centerPoint.X + moveVector.X;
  370. top = cacheRect.Top;
  371. bottom = cacheRect.Bottom;
  372. if (left + minWidth > right)
  373. {
  374. right = left + minWidth;
  375. }
  376. }
  377. break;
  378. case PointControlType.RightTop:
  379. {
  380. left = cacheRect.Left;
  381. right = centerPoint.X + moveVector.X;
  382. top = centerPoint.Y + moveVector.Y;
  383. bottom = cacheRect.Bottom;
  384. if (isProportionalScaling)
  385. {
  386. Size size = GetProportionalScalingSize(right - left, bottom - top);
  387. right = left + size.Width;
  388. top = bottom - size.Height;
  389. if (right > maxRect.Right)
  390. {
  391. double tmpWidth = right - left;
  392. right = maxRect.Right;
  393. double width = right - left;
  394. double height = (bottom - top) * width / tmpWidth;
  395. top = bottom - height;
  396. }
  397. if (top < maxRect.Top)
  398. {
  399. double tmpHeight = bottom - top;
  400. top = maxRect.Top;
  401. double height = bottom - top;
  402. double width = (right - left) * height / tmpHeight;
  403. right = left + width;
  404. }
  405. }
  406. else
  407. {
  408. if (left + minWidth > right)
  409. {
  410. right = left + minWidth;
  411. }
  412. if (top + minHeight > bottom)
  413. {
  414. top = bottom - minHeight;
  415. }
  416. }
  417. }
  418. break;
  419. case PointControlType.MiddleTop:
  420. {
  421. left = cacheRect.Left;
  422. right = cacheRect.Right;
  423. top = centerPoint.Y + moveVector.Y;
  424. bottom = cacheRect.Bottom;
  425. if (top + minHeight > bottom)
  426. {
  427. top = bottom - minHeight;
  428. }
  429. }
  430. break;
  431. case PointControlType.Body:
  432. case PointControlType.Line:
  433. {
  434. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  435. left = cacheRect.Left + OffsetPos.X;
  436. right = cacheRect.Right + OffsetPos.X;
  437. top = cacheRect.Top + OffsetPos.Y;
  438. bottom = cacheRect.Bottom + OffsetPos.Y;
  439. }
  440. break;
  441. case PointControlType.Rotate:
  442. {
  443. Point rotatPoint = new Point(centerPoint.X, centerPoint.Y - cacheRect.Height / 2 - rotateline);
  444. Vector startVector = rotatPoint - centerPoint;
  445. Vector currentVector = mousePoint - centerPoint;
  446. angle = Vector.AngleBetween(startVector, currentVector);
  447. }
  448. break;
  449. default:
  450. break;
  451. }
  452. if (left < maxRect.Left)
  453. {
  454. left = maxRect.Left;
  455. }
  456. if (top < maxRect.Top)
  457. {
  458. top = maxRect.Top;
  459. }
  460. if (right > maxRect.Right)
  461. {
  462. right = maxRect.Right;
  463. }
  464. if (bottom > maxRect.Bottom)
  465. {
  466. bottom = maxRect.Bottom;
  467. }
  468. drawRect = new Rect(left, top, right - left, bottom - top);
  469. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  470. return true;
  471. }
  472. catch (Exception ex)
  473. {
  474. }
  475. return false;
  476. }
  477. private Point CalcRotateRawPoint(Point calcPoint)
  478. {
  479. if(CanRotate==false || angle==0)
  480. {
  481. return calcPoint;
  482. }
  483. //旋转回去
  484. Vector currentVector = calcPoint - rotateCenter;
  485. Matrix matrix = new Matrix();
  486. matrix.Rotate(-angle);
  487. Vector transVector = matrix.Transform(currentVector);
  488. Point checkPos = new Point(rotateCenter.X + transVector.X, rotateCenter.Y + transVector.Y);
  489. return checkPos;
  490. }
  491. private bool RotateScaling(Point uiPoint)
  492. {
  493. try
  494. {
  495. //旋转回去
  496. Point mousePoint = CalcRotateRawPoint(uiPoint);
  497. double left = 0, right = 0, top = 0, bottom = 0;
  498. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  499. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  500. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  501. Point moveVector = (Point)(mousePoint - centerPoint);
  502. moveVector = ProportionalScalingOffsetPos(moveVector);
  503. switch (hitControlType)
  504. {
  505. case PointControlType.LeftTop:
  506. {
  507. left = centerPoint.X + moveVector.X;
  508. right = cacheRect.Right;
  509. top = centerPoint.Y + moveVector.Y;
  510. bottom = cacheRect.Bottom;
  511. if (isProportionalScaling)
  512. {
  513. Size size = GetProportionalScalingSize(right - left, bottom - top);
  514. left = right - size.Width;
  515. top = bottom - size.Height;
  516. if (left < maxRect.Left)
  517. {
  518. double tmpWidth = right - left;
  519. left = maxRect.Left;
  520. double width = right - left;
  521. double height = (bottom - top) * width / tmpWidth;
  522. top = bottom - height;
  523. }
  524. if (top < maxRect.Top)
  525. {
  526. double tmpHeight = bottom - top;
  527. top = maxRect.Top;
  528. double height = bottom - top;
  529. double width = (right - left) * height / tmpHeight;
  530. left = right - width;
  531. }
  532. }
  533. else
  534. {
  535. if (left + minWidth > right)
  536. {
  537. left = right - minWidth;
  538. }
  539. if (top + minHeight > bottom)
  540. {
  541. top = bottom - minHeight;
  542. }
  543. }
  544. }
  545. break;
  546. case PointControlType.LeftMiddle:
  547. {
  548. left = centerPoint.X + moveVector.X;
  549. right = cacheRect.Right;
  550. top = cacheRect.Top;
  551. bottom = cacheRect.Bottom;
  552. if (left + minWidth > right)
  553. {
  554. left = right - minWidth;
  555. }
  556. }
  557. break;
  558. case PointControlType.LeftBottom:
  559. {
  560. left = centerPoint.X + moveVector.X;
  561. right = cacheRect.Right;
  562. top = cacheRect.Top;
  563. bottom = centerPoint.Y + moveVector.Y;
  564. if (isProportionalScaling)
  565. {
  566. Size size = GetProportionalScalingSize(right - left, bottom - top);
  567. left = right - size.Width;
  568. bottom = top + size.Height;
  569. if (left < maxRect.Left)
  570. {
  571. double tmpWidth = right - left;
  572. left = maxRect.Left;
  573. double width = right - left;
  574. double height = (bottom - top) * width / tmpWidth;
  575. bottom = top + height;
  576. }
  577. if (bottom > maxRect.Bottom)
  578. {
  579. double tmpHeight = bottom - top;
  580. bottom = maxRect.Bottom;
  581. double height = bottom - top;
  582. double width = (right - left) * height / tmpHeight;
  583. left = right - width;
  584. }
  585. }
  586. else
  587. {
  588. if (left + minWidth > right)
  589. {
  590. left = right - minWidth;
  591. }
  592. if (top + minHeight > bottom)
  593. {
  594. bottom = top + minHeight;
  595. }
  596. }
  597. }
  598. break;
  599. case PointControlType.MiddlBottom:
  600. {
  601. left = cacheRect.Left;
  602. right = cacheRect.Right;
  603. top = cacheRect.Top;
  604. bottom = centerPoint.Y + moveVector.Y;
  605. if (top + minHeight > bottom)
  606. {
  607. bottom = top + minHeight;
  608. }
  609. }
  610. break;
  611. case PointControlType.RightBottom:
  612. {
  613. left = cacheRect.Left;
  614. right = centerPoint.X + moveVector.X;
  615. top = cacheRect.Top;
  616. bottom = centerPoint.Y + moveVector.Y;
  617. if (isProportionalScaling)
  618. {
  619. Size size = GetProportionalScalingSize(right - left, bottom - top);
  620. right = left + size.Width;
  621. bottom = top + size.Height;
  622. if (right > maxRect.Right)
  623. {
  624. double tmpWidth = right - left;
  625. right = maxRect.Right;
  626. double width = right - left;
  627. double height = (bottom - top) * width / tmpWidth;
  628. bottom = top + height;
  629. }
  630. if (bottom > maxRect.Bottom)
  631. {
  632. double tmpHeight = bottom - top;
  633. bottom = maxRect.Bottom;
  634. double height = bottom - top;
  635. double width = (right - left) * height / tmpHeight;
  636. right = left + width;
  637. }
  638. }
  639. else
  640. {
  641. if (left + minWidth > right)
  642. {
  643. right = left + minWidth;
  644. }
  645. if (top + minHeight > bottom)
  646. {
  647. bottom = top + minHeight;
  648. }
  649. }
  650. }
  651. break;
  652. case PointControlType.RightMiddle:
  653. {
  654. left = cacheRect.Left;
  655. right = centerPoint.X + moveVector.X;
  656. top = cacheRect.Top;
  657. bottom = cacheRect.Bottom;
  658. if (left + minWidth > right)
  659. {
  660. right = left + minWidth;
  661. }
  662. }
  663. break;
  664. case PointControlType.RightTop:
  665. {
  666. left = cacheRect.Left;
  667. right = centerPoint.X + moveVector.X;
  668. top = centerPoint.Y + moveVector.Y;
  669. bottom = cacheRect.Bottom;
  670. if (isProportionalScaling)
  671. {
  672. Size size = GetProportionalScalingSize(right - left, bottom - top);
  673. right = left + size.Width;
  674. top = bottom - size.Height;
  675. if (right > maxRect.Right)
  676. {
  677. double tmpWidth = right - left;
  678. right = maxRect.Right;
  679. double width = right - left;
  680. double height = (bottom - top) * width / tmpWidth;
  681. top = bottom - height;
  682. }
  683. if (top < maxRect.Top)
  684. {
  685. double tmpHeight = bottom - top;
  686. top = maxRect.Top;
  687. double height = bottom - top;
  688. double width = (right - left) * height / tmpHeight;
  689. right = left + width;
  690. }
  691. }
  692. else
  693. {
  694. if (left + minWidth > right)
  695. {
  696. right = left + minWidth;
  697. }
  698. if (top + minHeight > bottom)
  699. {
  700. top = bottom - minHeight;
  701. }
  702. }
  703. }
  704. break;
  705. case PointControlType.MiddleTop:
  706. {
  707. left = cacheRect.Left;
  708. right = cacheRect.Right;
  709. top = centerPoint.Y + moveVector.Y;
  710. bottom = cacheRect.Bottom;
  711. if (top + minHeight > bottom)
  712. {
  713. top = bottom - minHeight;
  714. }
  715. }
  716. break;
  717. case PointControlType.Body:
  718. case PointControlType.Line:
  719. {
  720. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(uiPoint -mouseDownPoint)), maxRect);
  721. left = cacheRect.Left + OffsetPos.X;
  722. right = cacheRect.Right + OffsetPos.X;
  723. top = cacheRect.Top + OffsetPos.Y;
  724. bottom = cacheRect.Bottom + OffsetPos.Y;
  725. }
  726. break;
  727. case PointControlType.Rotate:
  728. {
  729. Point rotatPoint = new Point(rotateCenter.X, rotateCenter.Y - cacheRect.Height / 2 - rotateline);
  730. Vector startVector = rotatPoint - rotateCenter;
  731. Vector endVector = uiPoint - rotateCenter;
  732. angle = Vector.AngleBetween(startVector, endVector);
  733. }
  734. break;
  735. default:
  736. break;
  737. }
  738. if (left < maxRect.Left)
  739. {
  740. left = maxRect.Left;
  741. }
  742. if (top < maxRect.Top)
  743. {
  744. top = maxRect.Top;
  745. }
  746. if (right > maxRect.Right)
  747. {
  748. right = maxRect.Right;
  749. }
  750. if (bottom > maxRect.Bottom)
  751. {
  752. bottom = maxRect.Bottom;
  753. }
  754. drawRect = new Rect(left, top, right - left, bottom - top);
  755. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  756. return true;
  757. }
  758. catch (Exception ex)
  759. {
  760. }
  761. return false;
  762. }
  763. /// <summary>
  764. /// Proportional scaling offset calibration
  765. /// </summary>
  766. /// <param name="movePoint">
  767. /// The current movement point
  768. /// </param>
  769. /// <returns>
  770. /// The offset point after the proportional scaling
  771. /// </returns>
  772. protected Point ProportionalScalingOffsetPos(Point movePoint)
  773. {
  774. if (isProportionalScaling)
  775. {
  776. Point offsetPos = movePoint;
  777. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  778. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  779. switch (hitControlType)
  780. {
  781. case PointControlType.LeftTop:
  782. case PointControlType.RightBottom:
  783. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  784. break;
  785. case PointControlType.LeftBottom:
  786. case PointControlType.RightTop:
  787. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  788. break;
  789. case PointControlType.LeftMiddle:
  790. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  791. break;
  792. case PointControlType.RightMiddle:
  793. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  794. break;
  795. case PointControlType.MiddlBottom:
  796. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  797. break;
  798. case PointControlType.MiddleTop:
  799. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  800. break;
  801. default:
  802. break;
  803. }
  804. return offsetPos;
  805. }
  806. else
  807. {
  808. return movePoint;
  809. }
  810. }
  811. /// <summary>
  812. /// Inner drawing circle point
  813. /// </summary>
  814. /// <param name="drawingContext">
  815. /// Drawing context
  816. /// </param>
  817. /// <param name="ignoreList">
  818. /// Collection of positions that need to be drawn
  819. /// </param>
  820. /// <param name="PointSize">
  821. /// Size of the point
  822. /// </param>
  823. /// <param name="PointPen">
  824. /// Brush for drawing points
  825. /// </param>
  826. /// <param name="BorderBrush">
  827. /// Border brush for drawing points
  828. /// </param>
  829. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  830. {
  831. GeometryGroup controlGroup = new GeometryGroup();
  832. controlGroup.FillRule = FillRule.Nonzero;
  833. List<Point> ignorePointsList = new List<Point>();
  834. // Get specific points
  835. foreach (PointControlType type in ignoreList)
  836. {
  837. if ((int)type < controlPoints.Count)
  838. {
  839. ignorePointsList.Add(controlPoints[(int)type]);
  840. }
  841. }
  842. for (int i = 0; i < controlPoints.Count; i++)
  843. {
  844. Point controlPoint = controlPoints[i];
  845. if (ignorePointsList.Contains(controlPoint))
  846. {
  847. continue;
  848. }
  849. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  850. controlGroup.Children.Add(circlPoint);
  851. }
  852. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  853. }
  854. /// <summary>
  855. /// Inner drawing square
  856. /// </summary>
  857. /// <param name="drawingContext">
  858. /// Drawing context
  859. /// </param>
  860. /// <param name="ControlPoints">
  861. /// Collection of positions that need to be drawn
  862. /// </param>
  863. /// <param name="PointSize">
  864. /// Size of the point
  865. /// </param>
  866. /// <param name="PointPen">
  867. /// Brush for drawing points
  868. /// </param>
  869. /// <param name="BorderBrush">
  870. /// Border brush for drawing points
  871. /// </param>
  872. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  873. {
  874. GeometryGroup controlGroup = new GeometryGroup();
  875. controlGroup.FillRule = FillRule.Nonzero;
  876. List<Point> ignorePointsList = new List<Point>();
  877. // Get specific points
  878. foreach (PointControlType type in ignoreList)
  879. {
  880. if ((int)type < controlPoints.Count)
  881. {
  882. ignorePointsList.Add(controlPoints[(int)type]);
  883. }
  884. }
  885. for (int i = 0; i < controlPoints.Count; i++)
  886. {
  887. Point controlPoint = controlPoints[i];
  888. if (ignorePointsList.Contains(controlPoint))
  889. {
  890. continue;
  891. }
  892. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  893. PointSize * 2, PointSize * 2));
  894. controlGroup.Children.Add(rectPoint);
  895. }
  896. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  897. }
  898. protected void DrawCropPoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  899. {
  900. GeometryGroup controlGroup = new GeometryGroup();
  901. controlGroup.FillRule = FillRule.Nonzero;
  902. //Left Top Corner
  903. if (!ignoreList.Contains(PointControlType.LeftTop))
  904. {
  905. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[0].X - PointSize, controlPoints[0].Y - PointSize, PointSize, PointSize * 4));
  906. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[0].X - PointSize, controlPoints[0].Y - PointSize, PointSize * 4, PointSize));
  907. }
  908. //Left Center
  909. if (!ignoreList.Contains(PointControlType.LeftMiddle))
  910. {
  911. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[1].X - PointSize, (controlPoints[1].Y + controlPoints[1].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  912. }
  913. //Left Bottom Corner
  914. if (!ignoreList.Contains(PointControlType.LeftBottom))
  915. {
  916. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[2].X - PointSize, controlPoints[2].Y - PointSize * 3, PointSize, PointSize * 4));
  917. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[2].X - PointSize, controlPoints[2].Y, PointSize * 4, PointSize));
  918. }
  919. //Bottom Center
  920. if (!ignoreList.Contains(PointControlType.MiddlBottom))
  921. {
  922. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlPoints[3].X + controlPoints[3].X - PointSize * 5) / 2, controlPoints[3].Y, PointSize * 5, PointSize));
  923. }
  924. //Bottom Right Corner
  925. if (!ignoreList.Contains(PointControlType.RightBottom))
  926. {
  927. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[4].X, controlPoints[4].Y - PointSize * 3, PointSize, PointSize * 4));
  928. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[4].X - PointSize * 3, controlPoints[4].Y, PointSize * 4, PointSize));
  929. }
  930. //Right Center
  931. if (!ignoreList.Contains(PointControlType.RightMiddle))
  932. {
  933. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[5].X, (controlPoints[5].Y + controlPoints[5].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  934. }
  935. //Right Top Corner
  936. if (!ignoreList.Contains(PointControlType.RightTop))
  937. {
  938. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[6].X, controlPoints[6].Y - PointSize, PointSize, PointSize * 4));
  939. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[6].X - PointSize * 4, controlPoints[6].Y - PointSize, PointSize * 4, PointSize));
  940. }
  941. //Top Center
  942. if (!ignoreList.Contains(PointControlType.MiddleTop))
  943. {
  944. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlPoints[7].X + controlPoints[7].X - PointSize * 5) / 2, controlPoints[7].Y - PointSize, PointSize * 5, PointSize));
  945. }
  946. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  947. }
  948. /// <summary>
  949. /// Draw the reference line in the moving state
  950. /// </summary>
  951. /// <param name="drawDc">
  952. /// Draw context handle
  953. /// </param>
  954. /// <param name="controltype">
  955. /// Current selected control point type
  956. /// </param>
  957. /// <param name="activePen">
  958. /// Brush for drawing lines
  959. /// </param>
  960. /// <param name="moveBrush">
  961. /// Brush for drawing rectangles
  962. /// </param>
  963. /// <param name="moveRect">
  964. /// Current rectangle to draw
  965. /// </param>
  966. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect)
  967. {
  968. switch (controltype)
  969. {
  970. case PointControlType.LeftTop:
  971. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  972. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  973. break;
  974. case PointControlType.LeftMiddle:
  975. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  976. break;
  977. case PointControlType.LeftBottom:
  978. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  979. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  980. break;
  981. case PointControlType.MiddlBottom:
  982. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  983. break;
  984. case PointControlType.RightBottom:
  985. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  986. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  987. break;
  988. case PointControlType.RightMiddle:
  989. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  990. break;
  991. case PointControlType.RightTop:
  992. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  993. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  994. break;
  995. case PointControlType.MiddleTop:
  996. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  997. break;
  998. case PointControlType.Rotate:
  999. break;
  1000. case PointControlType.Body:
  1001. case PointControlType.Line:
  1002. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1003. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1004. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  1005. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  1006. break;
  1007. default:
  1008. break;
  1009. }
  1010. drawDc?.DrawRectangle(moveBrush, null, moveRect);
  1011. }
  1012. /// <summary>
  1013. /// Notify the event during/after the drawing data
  1014. /// </summary>
  1015. /// <param name="isFinish">
  1016. /// Identifies whether the data change is complete
  1017. /// </param>
  1018. protected void InvokeDataChangEvent(bool isFinish)
  1019. {
  1020. selectedRectData.Square = GetRect();
  1021. if (isFinish)
  1022. {
  1023. selectedRectData.Angle=angle;
  1024. DataChanged?.Invoke(this, selectedRectData);
  1025. }
  1026. else
  1027. {
  1028. DataChanging?.Invoke(this, selectedRectData);
  1029. }
  1030. }
  1031. /// <summary>
  1032. /// Align the rectangle drawing
  1033. /// </summary>
  1034. /// <param name="RectMovePoint">
  1035. /// Move distance required for the aligned algorithm to obtain the rectangle
  1036. /// </param>
  1037. private void DrawAlignRect(Point RectMovePoint)
  1038. {
  1039. double TmpLeft, TmpRight, TmpUp, TmpDown;
  1040. Point OffsetPos = CalcMoveBound(drawRect, RectMovePoint, maxRect);
  1041. TmpLeft = drawRect.Left + OffsetPos.X;
  1042. TmpRight = drawRect.Right + OffsetPos.X;
  1043. TmpUp = drawRect.Top + OffsetPos.Y;
  1044. TmpDown = drawRect.Bottom + OffsetPos.Y;
  1045. SetDrawRect = drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  1046. Draw();
  1047. }
  1048. /// <summary>
  1049. /// Get the current set of ignore points
  1050. /// </summary>
  1051. /// <returns>
  1052. /// Data set of ignored points
  1053. /// </returns>
  1054. private List<PointControlType> GetIgnorePoints()
  1055. {
  1056. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  1057. foreach (PointControlType type in ignorePoints)
  1058. {
  1059. IgnorePointsList.Add(type);
  1060. }
  1061. return IgnorePointsList;
  1062. }
  1063. #endregion
  1064. }
  1065. }