CommonHelper.cs 34 KB

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