CommonHelper.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Media;
  14. namespace Compdfkit_Tools.Helper
  15. {
  16. public static class CommonHelper
  17. {
  18. /// <summary>
  19. /// Returns the file size based on the specified file path, with the smallest unit being bytes (B).
  20. /// </summary>
  21. /// <param name="filePath">The path to the file.</param>
  22. /// <returns>
  23. /// The calculated file size, with units in bytes (B), kilobytes (KB), megabytes (MB), or gigabytes (GB).
  24. /// </returns>
  25. public static string GetFileSize(string filePath)
  26. {
  27. FileInfo fileInfo = null;
  28. try
  29. {
  30. fileInfo = new FileInfo(filePath);
  31. }
  32. catch
  33. {
  34. return "0B";
  35. }
  36. if (fileInfo != null && fileInfo.Exists)
  37. {
  38. double fileSize = fileInfo.Length;
  39. if (fileSize > 1024)
  40. {
  41. fileSize = Math.Round(fileSize / 1024, 2);
  42. if (fileSize > 1024)
  43. {
  44. fileSize = Math.Round(fileSize / 1024, 2);
  45. if (fileSize > 1024)
  46. {
  47. fileSize = Math.Round(fileSize / 1024, 2);
  48. return fileSize + " GB";
  49. }
  50. else
  51. {
  52. return fileSize + " MB";
  53. }
  54. }
  55. else
  56. {
  57. return fileSize + " KB";
  58. }
  59. }
  60. else
  61. {
  62. return fileSize + " B";
  63. }
  64. }
  65. return "0B";
  66. }
  67. public static string GetFilePathOrEmpty()
  68. {
  69. string selectedFilePath = string.Empty;
  70. OpenFileDialog openFileDialog = new OpenFileDialog();
  71. openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
  72. if (openFileDialog.ShowDialog() == true)
  73. {
  74. selectedFilePath = openFileDialog.FileName;
  75. }
  76. return selectedFilePath;
  77. }
  78. /// <summary>
  79. /// 校验PageRange 输入是否合法,且可返回List<int> Pages 存放的索引值
  80. /// </summary>
  81. /// <param name="pageList">返回的页面集合</param>
  82. /// <param name="pageRange">需要判断的文本</param>
  83. /// <param name="count">页面总数</param>
  84. /// <param name="enumerationSeparator">例 new char[] { ',' }</param>
  85. /// <param name="rangeSeparator">例 new char[] { '-' }</param>
  86. /// <param name="inittag"></param>
  87. /// <returns></returns>
  88. public static bool GetPagesInRange(ref List<int> pageList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
  89. {
  90. string[] rangeSplit = pageRange.Split(enumerationSeparator);//根据分隔符 拆分字符串
  91. pageList.Clear();
  92. foreach (string range in rangeSplit)
  93. {
  94. int starttag = 1;
  95. if (inittag)
  96. {
  97. starttag = 0;
  98. }
  99. if (range.Contains("-"))//连续页
  100. {
  101. try
  102. {
  103. string[] limits = range.Split(rangeSeparator);//对子字符串再根据”-“ 拆分
  104. if (limits.Length >= 2 && !string.IsNullOrWhiteSpace(limits[0]) && !string.IsNullOrWhiteSpace(limits[1]))
  105. {
  106. int start = int.Parse(limits[0]);
  107. int end = int.Parse(limits[1]);
  108. if ((start < starttag) || (end > count) || (start > end))
  109. {
  110. //throw new Exception(string.Format("Invalid page(s) in range {0} - {1}", start, end));
  111. return false;
  112. }
  113. for (int i = start; i <= end; ++i)
  114. {
  115. if (pageList.Contains(i))
  116. {
  117. // throw new Exception(string.Format("Invalid page(s) in range {0} - {1}", start, end));
  118. return false;
  119. }
  120. pageList.Add(i - 1);
  121. }
  122. continue;
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. //MessageBox.Show("请检查符号或页码范围是否正确。","提示",MessageBoxButton.OKCancel,MessageBoxImage.Warning);
  128. return false;
  129. }
  130. }
  131. int pageNr;
  132. try
  133. {
  134. // Single page
  135. pageNr = int.Parse(range);//单页
  136. }
  137. catch (Exception)//格式不正确时
  138. {
  139. return false;
  140. }
  141. if (pageNr < starttag || pageNr > count)
  142. {
  143. return false;
  144. //throw new Exception(string.Format("Invalid page {0}", pageNr));
  145. }
  146. if (pageList.Contains(pageNr))
  147. {
  148. return false;
  149. // throw new Exception(string.Format("Invalid page {0}", pageNr));
  150. }
  151. pageList.Add(pageNr - 1);
  152. }
  153. return true;
  154. }
  155. internal static class PageEditHelper
  156. {
  157. //Find Child Element By Parents Element
  158. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  159. {
  160. while (obj != null)
  161. {
  162. if (obj is T)
  163. return obj as T;
  164. obj = VisualTreeHelper.GetParent(obj);
  165. }
  166. return null;
  167. }
  168. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  169. where childItem : DependencyObject
  170. {
  171. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  172. {
  173. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  174. if (child != null && child is childItem)
  175. return (childItem)child;
  176. else
  177. {
  178. childItem childOfChild = FindVisualChild<childItem>(child);
  179. if (childOfChild != null)
  180. return childOfChild;
  181. }
  182. }
  183. return null;
  184. }
  185. }
  186. public static class ViewportHelper
  187. {
  188. public static CPDFDocument CopyDoc;
  189. public static bool IsInViewport(ScrollViewer sv, Control item)
  190. {
  191. if (item == null) return false;
  192. ItemsControl itemsControl = null;
  193. if (item is ListBoxItem)
  194. itemsControl = ItemsControl.ItemsControlFromItemContainer(item) as ListBox;
  195. else
  196. throw new NotSupportedException(item.GetType().Name);
  197. ScrollContentPresenter scrollContentPresenter = (ScrollContentPresenter)sv.Template.FindName("PART_ScrollContentPresenter", sv);
  198. MethodInfo isInViewportMethod = sv.GetType().GetMethod("IsInViewport", BindingFlags.NonPublic | BindingFlags.Instance);
  199. return (bool)isInViewportMethod.Invoke(sv, new object[] { scrollContentPresenter, item });
  200. }
  201. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  202. {
  203. while (obj != null)
  204. {
  205. if (obj is T)
  206. return obj as T;
  207. obj = VisualTreeHelper.GetParent(obj);
  208. }
  209. return null;
  210. }
  211. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  212. where childItem : DependencyObject
  213. {
  214. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  215. {
  216. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  217. if (child != null && child is childItem)
  218. return (childItem)child;
  219. else
  220. {
  221. childItem childOfChild = FindVisualChild<childItem>(child);
  222. if (childOfChild != null)
  223. return childOfChild;
  224. }
  225. }
  226. return null;
  227. }
  228. }
  229. public class ArrowHelper
  230. {
  231. /// <summary>
  232. /// 是否有开始箭头
  233. /// </summary>
  234. public bool HasStartArrow
  235. {
  236. get
  237. {
  238. if (StartSharp != C_LINE_TYPE.LINETYPE_UNKNOWN && StartSharp != C_LINE_TYPE.LINETYPE_NONE)
  239. {
  240. return true;
  241. }
  242. return false;
  243. }
  244. }
  245. /// <summary>
  246. /// 开始箭头是否封闭
  247. /// </summary>
  248. public bool IsStartClosed
  249. {
  250. get
  251. {
  252. if (StartSharp == C_LINE_TYPE.LINETYPE_CLOSEDARROW || StartSharp == C_LINE_TYPE.LINETYPE_RCLOSEDARROW || StartSharp == C_LINE_TYPE.LINETYPE_DIAMOND)
  253. {
  254. return true;
  255. }
  256. return false;
  257. }
  258. }
  259. /// <summary>
  260. /// 是否有结束箭头
  261. /// </summary>
  262. public bool HasEndArrow
  263. {
  264. get
  265. {
  266. if (EndSharp != C_LINE_TYPE.LINETYPE_UNKNOWN && EndSharp != C_LINE_TYPE.LINETYPE_NONE)
  267. {
  268. return true;
  269. }
  270. return false;
  271. }
  272. }
  273. /// <summary>
  274. /// 结束箭头是否封闭
  275. /// </summary>
  276. public bool IsEndClosed
  277. {
  278. get
  279. {
  280. if (EndSharp == C_LINE_TYPE.LINETYPE_CLOSEDARROW || EndSharp == C_LINE_TYPE.LINETYPE_RCLOSEDARROW || EndSharp == C_LINE_TYPE.LINETYPE_DIAMOND)
  281. {
  282. return true;
  283. }
  284. return false;
  285. }
  286. }
  287. /// <summary>
  288. /// 箭头角度
  289. /// </summary>
  290. public uint ArrowAngle { get; set; }
  291. /// <summary>
  292. /// 箭头长度
  293. /// </summary>
  294. public uint ArrowLength { get; set; }
  295. /// <summary>
  296. /// 起始点
  297. /// </summary>
  298. public Point? LineStart { get; set; }
  299. /// <summary>
  300. /// 结束点
  301. /// </summary>
  302. public Point? LineEnd { get; set; }
  303. /// <summary>
  304. /// 线段路径
  305. /// </summary>
  306. public PathGeometry Body { get; set; }
  307. /// <summary>
  308. /// 开始箭头形状
  309. /// </summary>
  310. public C_LINE_TYPE StartSharp { get; set; }
  311. /// <summary>
  312. /// 结束箭头形状
  313. /// </summary>
  314. public C_LINE_TYPE EndSharp { get; set; }
  315. /// <summary>
  316. /// 箭头帮助类
  317. /// </summary>
  318. public ArrowHelper()
  319. {
  320. Body = new PathGeometry();
  321. ArrowLength = 12;
  322. ArrowAngle = 60;
  323. }
  324. protected PathFigure CreateLineBody()
  325. {
  326. if (LineStart != null && LineEnd != null)
  327. {
  328. PathFigure lineFigure = new PathFigure();
  329. // lineFigure.IsClosed = true;
  330. lineFigure.StartPoint = (Point)LineStart;
  331. LineSegment linePath = new LineSegment();
  332. linePath.Point = (Point)LineEnd;
  333. //linePath.IsSmoothJoin = true;
  334. //linePath.IsStroked = true;
  335. lineFigure.Segments.Add(linePath);
  336. return lineFigure;
  337. }
  338. return null;
  339. }
  340. protected PathFigure CreateStartArrow()
  341. {
  342. switch (StartSharp)
  343. {
  344. case C_LINE_TYPE.LINETYPE_NONE:
  345. case C_LINE_TYPE.LINETYPE_UNKNOWN:
  346. break;
  347. case C_LINE_TYPE.LINETYPE_ARROW:
  348. case C_LINE_TYPE.LINETYPE_CLOSEDARROW:
  349. return CreateStartOpenArrow();
  350. case C_LINE_TYPE.LINETYPE_ROPENARROW:
  351. case C_LINE_TYPE.LINETYPE_RCLOSEDARROW:
  352. return CreateStartReverseArrow();
  353. case C_LINE_TYPE.LINETYPE_BUTT:
  354. return CreateStartButtArrow();
  355. case C_LINE_TYPE.LINETYPE_DIAMOND:
  356. return CreateStartDiamondArrow();
  357. case C_LINE_TYPE.LINETYPE_CIRCLE:
  358. return CreateStartRoundArrow();
  359. case C_LINE_TYPE.LINETYPE_SQUARE:
  360. return CreateStartSquareArrow();
  361. case C_LINE_TYPE.LINETYPE_SLASH:
  362. return CreateStartSlashArrow();
  363. default:
  364. break;
  365. }
  366. return null;
  367. }
  368. protected virtual PathFigure CreateEndArrow()
  369. {
  370. switch (EndSharp)
  371. {
  372. case C_LINE_TYPE.LINETYPE_NONE:
  373. case C_LINE_TYPE.LINETYPE_UNKNOWN:
  374. break;
  375. case C_LINE_TYPE.LINETYPE_ARROW:
  376. case C_LINE_TYPE.LINETYPE_CLOSEDARROW:
  377. return CreateEndOpenArrow();
  378. case C_LINE_TYPE.LINETYPE_ROPENARROW:
  379. case C_LINE_TYPE.LINETYPE_RCLOSEDARROW:
  380. return CreateEndReverseArrow();
  381. case C_LINE_TYPE.LINETYPE_BUTT:
  382. return CreateEndButtArrow();
  383. case C_LINE_TYPE.LINETYPE_DIAMOND:
  384. return CreateEndDiamondArrow();
  385. case C_LINE_TYPE.LINETYPE_CIRCLE:
  386. return CreateEndRoundArrow();
  387. case C_LINE_TYPE.LINETYPE_SQUARE:
  388. return CreateEndSquareArrow();
  389. case C_LINE_TYPE.LINETYPE_SLASH:
  390. return CreateEndSlashArrow();
  391. default:
  392. break;
  393. }
  394. return null;
  395. }
  396. /// <summary>
  397. /// 创建箭头路径
  398. /// </summary>
  399. /// <returns></returns>
  400. public PathGeometry BuildArrowBody()
  401. {
  402. Body.Figures.Clear();
  403. PathFigure lineBody = CreateLineBody();
  404. if (lineBody != null)
  405. {
  406. Body.Figures.Add(lineBody);
  407. PathFigure arrowFigure = CreateStartArrow();
  408. if (arrowFigure != null)
  409. {
  410. Body.Figures.Add(arrowFigure);
  411. }
  412. arrowFigure = CreateEndArrow();
  413. if (arrowFigure != null)
  414. {
  415. Body.Figures.Add(arrowFigure);
  416. }
  417. }
  418. return Body;
  419. }
  420. /// <summary>
  421. /// 绘制开始箭头
  422. /// </summary>
  423. /// <returns></returns>
  424. private PathFigure CreateStartOpenArrow()
  425. {
  426. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  427. {
  428. return null;
  429. }
  430. PathFigure arrowFigure = new PathFigure();
  431. PolyLineSegment arrowSegment = new PolyLineSegment();
  432. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  433. lineVector.Normalize();
  434. lineVector *= ArrowLength;
  435. Matrix rotateMatrix = new Matrix();
  436. rotateMatrix.Rotate(ArrowAngle / 2);
  437. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  438. arrowSegment.Points.Add((Point)LineStart);
  439. rotateMatrix.Rotate(-ArrowAngle);
  440. arrowSegment.Points.Add((Point)LineStart + (lineVector * rotateMatrix));
  441. arrowFigure.Segments.Add(arrowSegment);
  442. arrowFigure.IsClosed = IsStartClosed;
  443. arrowFigure.IsFilled = IsStartClosed;
  444. return arrowFigure;
  445. }
  446. /// <summary>
  447. /// 绘制结束箭头
  448. /// </summary>
  449. /// <returns></returns>
  450. private PathFigure CreateEndOpenArrow()
  451. {
  452. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  453. {
  454. return null;
  455. }
  456. PathFigure arrowFigure = new PathFigure();
  457. PolyLineSegment arrowSegment = new PolyLineSegment();
  458. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  459. lineVector.Normalize();
  460. lineVector *= ArrowLength;
  461. Matrix rotateMatrix = new Matrix();
  462. rotateMatrix.Rotate(ArrowAngle / 2);
  463. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  464. arrowSegment.Points.Add((Point)LineEnd);
  465. rotateMatrix.Rotate(-ArrowAngle);
  466. arrowSegment.Points.Add((Point)LineEnd + (lineVector * rotateMatrix));
  467. arrowFigure.Segments.Add(arrowSegment);
  468. arrowFigure.IsClosed = IsEndClosed;
  469. arrowFigure.IsFilled = IsEndClosed;
  470. return arrowFigure;
  471. }
  472. /// <summary>
  473. /// 绘制开始箭头(逆向)
  474. /// </summary>
  475. /// <returns></returns>
  476. private PathFigure CreateStartReverseArrow()
  477. {
  478. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  479. {
  480. return null;
  481. }
  482. PathFigure arrowFigure = new PathFigure();
  483. PolyLineSegment arrowSegment = new PolyLineSegment();
  484. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  485. lineVector.Normalize();
  486. lineVector *= ArrowLength;
  487. Matrix rotateMatrix = new Matrix();
  488. rotateMatrix.Rotate(ArrowAngle / 2);
  489. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  490. arrowSegment.Points.Add((Point)LineStart);
  491. rotateMatrix.Rotate(-ArrowAngle);
  492. arrowSegment.Points.Add((Point)LineStart + (lineVector * rotateMatrix));
  493. arrowFigure.Segments.Add(arrowSegment);
  494. arrowFigure.IsClosed = IsStartClosed;
  495. arrowFigure.IsFilled = IsStartClosed;
  496. return arrowFigure;
  497. }
  498. /// <summary>
  499. /// 绘制结束箭头(逆向)
  500. /// </summary>
  501. /// <returns></returns>
  502. private PathFigure CreateEndReverseArrow()
  503. {
  504. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  505. {
  506. return null;
  507. }
  508. PathFigure arrowFigure = new PathFigure();
  509. PolyLineSegment arrowSegment = new PolyLineSegment();
  510. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  511. lineVector.Normalize();
  512. lineVector *= ArrowLength;
  513. Matrix rotateMatrix = new Matrix();
  514. rotateMatrix.Rotate(ArrowAngle / 2);
  515. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  516. arrowSegment.Points.Add((Point)LineEnd);
  517. rotateMatrix.Rotate(-ArrowAngle);
  518. arrowSegment.Points.Add((Point)LineEnd + (lineVector * rotateMatrix));
  519. arrowFigure.Segments.Add(arrowSegment);
  520. arrowFigure.IsClosed = IsEndClosed;
  521. arrowFigure.IsFilled = IsEndClosed;
  522. return arrowFigure;
  523. }
  524. /// <summary>
  525. /// 绘制开始平头
  526. /// </summary>
  527. /// <returns></returns>
  528. private PathFigure CreateStartButtArrow()
  529. {
  530. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  531. {
  532. return null;
  533. }
  534. PathFigure arrowFigure = new PathFigure();
  535. LineSegment buttSegment = new LineSegment();
  536. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  537. lineVector.Normalize();
  538. lineVector *= ArrowLength;
  539. Matrix rotateMatrix = new Matrix();
  540. rotateMatrix.Rotate(90);
  541. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  542. rotateMatrix.Rotate(-180);
  543. buttSegment.Point = ((Point)LineStart + (lineVector * rotateMatrix));
  544. arrowFigure.Segments.Add(buttSegment);
  545. return arrowFigure;
  546. }
  547. /// <summary>
  548. /// 绘制结束平头
  549. /// </summary>
  550. /// <returns></returns>
  551. private PathFigure CreateEndButtArrow()
  552. {
  553. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  554. {
  555. return null;
  556. }
  557. PathFigure arrowFigure = new PathFigure();
  558. LineSegment buttSegment = new LineSegment();
  559. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  560. lineVector.Normalize();
  561. lineVector *= ArrowLength;
  562. Matrix rotateMatrix = new Matrix();
  563. rotateMatrix.Rotate(90);
  564. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  565. rotateMatrix.Rotate(-180);
  566. buttSegment.Point = ((Point)LineEnd + (lineVector * rotateMatrix));
  567. arrowFigure.Segments.Add(buttSegment);
  568. return arrowFigure;
  569. }
  570. /// <summary>
  571. /// 绘制开始菱形
  572. /// </summary>
  573. /// <returns></returns>
  574. private PathFigure CreateStartDiamondArrow()
  575. {
  576. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  577. {
  578. return null;
  579. }
  580. PathFigure arrowFigure = new PathFigure();
  581. PolyLineSegment arrowSegment = new PolyLineSegment();
  582. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  583. lineVector.Normalize();
  584. lineVector *= ArrowLength;
  585. Matrix rotateMatrix = new Matrix();
  586. rotateMatrix.Rotate(45);
  587. Point cornerTop = (Point)LineStart + (lineVector * rotateMatrix);
  588. Vector turnVector = cornerTop - (Point)LineStart;
  589. turnVector.Normalize();
  590. turnVector *= ArrowLength;
  591. Matrix turnMatrix = new Matrix();
  592. turnMatrix.Rotate(-90);
  593. Point awayPoint = cornerTop + (turnVector * turnMatrix);
  594. rotateMatrix = new Matrix();
  595. rotateMatrix.Rotate(-45);
  596. Point cornerDown = (Point)LineStart + (lineVector * rotateMatrix);
  597. arrowFigure.StartPoint = (Point)LineStart;
  598. arrowSegment.Points.Add(cornerTop);
  599. arrowSegment.Points.Add(awayPoint);
  600. arrowSegment.Points.Add(cornerDown);
  601. arrowSegment.Points.Add((Point)LineStart);
  602. arrowFigure.Segments.Add(arrowSegment);
  603. arrowFigure.IsClosed = IsStartClosed;
  604. arrowFigure.IsFilled = IsStartClosed;
  605. return arrowFigure;
  606. }
  607. /// <summary>
  608. /// 绘制结束菱形
  609. /// </summary>
  610. /// <returns></returns>
  611. private PathFigure CreateEndDiamondArrow()
  612. {
  613. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  614. {
  615. return null;
  616. }
  617. PathFigure arrowFigure = new PathFigure();
  618. PolyLineSegment arrowSegment = new PolyLineSegment();
  619. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  620. lineVector.Normalize();
  621. lineVector *= ArrowLength;
  622. Matrix rotateMatrix = new Matrix();
  623. rotateMatrix.Rotate(45);
  624. Point cornerTop = (Point)LineEnd + (lineVector * rotateMatrix);
  625. Vector turnVector = cornerTop - (Point)LineEnd;
  626. turnVector.Normalize();
  627. turnVector *= ArrowLength;
  628. Matrix turnMatrix = new Matrix();
  629. turnMatrix.Rotate(-90);
  630. Point awayPoint = cornerTop + (turnVector * turnMatrix);
  631. rotateMatrix = new Matrix();
  632. rotateMatrix.Rotate(-45);
  633. Point cornerDown = (Point)LineEnd + (lineVector * rotateMatrix);
  634. arrowFigure.StartPoint = (Point)LineEnd;
  635. arrowSegment.Points.Add(cornerTop);
  636. arrowSegment.Points.Add(awayPoint);
  637. arrowSegment.Points.Add(cornerDown);
  638. arrowSegment.Points.Add((Point)LineEnd);
  639. arrowFigure.Segments.Add(arrowSegment);
  640. arrowFigure.IsClosed = IsEndClosed;
  641. arrowFigure.IsFilled = IsEndClosed;
  642. return arrowFigure;
  643. }
  644. /// <summary>
  645. /// 绘制开始圆形
  646. /// </summary>
  647. /// <returns></returns>
  648. private PathFigure CreateStartRoundArrow()
  649. {
  650. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  651. {
  652. return null;
  653. }
  654. PathFigure arrowFigure = new PathFigure();
  655. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  656. lineVector.Normalize();
  657. lineVector *= ArrowLength;
  658. Matrix rotateMatrix = new Matrix();
  659. rotateMatrix.Rotate(180);
  660. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  661. ArcSegment circleSegment = new ArcSegment();
  662. circleSegment.Point = (Point)LineStart;
  663. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  664. arrowFigure.Segments.Add(circleSegment);
  665. circleSegment = new ArcSegment();
  666. circleSegment.Point = (Point)arrowFigure.StartPoint;
  667. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  668. arrowFigure.Segments.Add(circleSegment);
  669. return arrowFigure;
  670. }
  671. /// <summary>
  672. /// 绘制结束圆形
  673. /// </summary>
  674. /// <returns></returns>
  675. private PathFigure CreateEndRoundArrow()
  676. {
  677. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  678. {
  679. return null;
  680. }
  681. PathFigure arrowFigure = new PathFigure();
  682. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  683. lineVector.Normalize();
  684. lineVector *= ArrowLength;
  685. Matrix rotateMatrix = new Matrix();
  686. rotateMatrix.Rotate(180);
  687. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  688. ArcSegment circleSegment = new ArcSegment();
  689. circleSegment.Point = (Point)LineEnd;
  690. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  691. arrowFigure.Segments.Add(circleSegment);
  692. circleSegment = new ArcSegment();
  693. circleSegment.Point = (Point)arrowFigure.StartPoint;
  694. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  695. arrowFigure.Segments.Add(circleSegment);
  696. return arrowFigure;
  697. }
  698. /// <summary>
  699. /// 绘制开始方形
  700. /// </summary>
  701. /// <returns></returns>
  702. private PathFigure CreateStartSquareArrow()
  703. {
  704. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  705. {
  706. return null;
  707. }
  708. PathFigure arrowFigure = new PathFigure();
  709. PolyLineSegment squreSegment = new PolyLineSegment();
  710. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  711. lineVector.Normalize();
  712. lineVector *= (ArrowLength / 2);
  713. Matrix rotateMatrix = new Matrix();
  714. rotateMatrix.Rotate(90);
  715. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  716. rotateMatrix.Rotate(-180);
  717. Point pointCorner = (Point)LineStart + (lineVector * rotateMatrix);
  718. squreSegment.Points.Add(pointCorner);
  719. Vector moveVector = arrowFigure.StartPoint - pointCorner;
  720. moveVector.Normalize();
  721. moveVector *= (ArrowLength);
  722. rotateMatrix = new Matrix();
  723. rotateMatrix.Rotate(90);
  724. squreSegment.Points.Add(pointCorner + (moveVector * rotateMatrix));
  725. squreSegment.Points.Add(arrowFigure.StartPoint + (moveVector * rotateMatrix));
  726. squreSegment.Points.Add(arrowFigure.StartPoint);
  727. squreSegment.Points.Add((Point)LineStart);
  728. arrowFigure.Segments.Add(squreSegment);
  729. return arrowFigure;
  730. }
  731. /// <summary>
  732. /// 绘制结束方形
  733. /// </summary>
  734. /// <returns></returns>
  735. private PathFigure CreateEndSquareArrow()
  736. {
  737. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  738. {
  739. return null;
  740. }
  741. PathFigure arrowFigure = new PathFigure();
  742. PolyLineSegment squreSegment = new PolyLineSegment();
  743. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  744. lineVector.Normalize();
  745. lineVector *= (ArrowLength / 2);
  746. Matrix rotateMatrix = new Matrix();
  747. rotateMatrix.Rotate(90);
  748. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  749. rotateMatrix.Rotate(-180);
  750. Point pointCorner = (Point)LineEnd + (lineVector * rotateMatrix);
  751. squreSegment.Points.Add(pointCorner);
  752. Vector moveVector = arrowFigure.StartPoint - pointCorner;
  753. moveVector.Normalize();
  754. moveVector *= (ArrowLength);
  755. rotateMatrix = new Matrix();
  756. rotateMatrix.Rotate(90);
  757. squreSegment.Points.Add(pointCorner + (moveVector * rotateMatrix));
  758. squreSegment.Points.Add(arrowFigure.StartPoint + (moveVector * rotateMatrix));
  759. squreSegment.Points.Add(arrowFigure.StartPoint);
  760. squreSegment.Points.Add((Point)LineEnd);
  761. arrowFigure.Segments.Add(squreSegment);
  762. return arrowFigure;
  763. }
  764. /// <summary>
  765. /// 绘制开始斜线
  766. /// </summary>
  767. /// <returns></returns>
  768. private PathFigure CreateStartSlashArrow()
  769. {
  770. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  771. {
  772. return null;
  773. }
  774. PathFigure arrowFigure = new PathFigure();
  775. LineSegment buttSegment = new LineSegment();
  776. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  777. lineVector.Normalize();
  778. lineVector *= ArrowLength;
  779. Matrix rotateMatrix = new Matrix();
  780. rotateMatrix.Rotate(45);
  781. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  782. rotateMatrix.Rotate(-180);
  783. buttSegment.Point = ((Point)LineStart + (lineVector * rotateMatrix));
  784. arrowFigure.Segments.Add(buttSegment);
  785. return arrowFigure;
  786. }
  787. /// <summary>
  788. /// 绘制结束斜线
  789. /// </summary>
  790. /// <returns></returns>
  791. private PathFigure CreateEndSlashArrow()
  792. {
  793. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  794. {
  795. return null;
  796. }
  797. PathFigure arrowFigure = new PathFigure();
  798. LineSegment buttSegment = new LineSegment();
  799. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  800. lineVector.Normalize();
  801. lineVector *= ArrowLength;
  802. Matrix rotateMatrix = new Matrix();
  803. rotateMatrix.Rotate(45);
  804. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  805. rotateMatrix.Rotate(-180);
  806. buttSegment.Point = ((Point)LineEnd + (lineVector * rotateMatrix));
  807. arrowFigure.Segments.Add(buttSegment);
  808. return arrowFigure;
  809. }
  810. }
  811. }
  812. }