MainWindow.xaml.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using SweepBuilding;
  17. namespace WpfApp1 {
  18. /// <summary>
  19. /// MainWindow.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class MainWindow : Window {
  22. public MainWindow() {
  23. InitializeComponent();
  24. //var info = new UrlInfo() {
  25. // Url = "ScanLogs.html",
  26. // Title = "扫楼日志",
  27. // //ProjId = MBIControl.ProjectCur.Id,
  28. // // UserId = MBIControl.ManageInfo.Person_Id
  29. //};
  30. ////var win = new SweepBuilding.SweepBuilding(info);
  31. ////win.ShowDialog();
  32. //System.Diagnostics.Process.Start("SAGA.SweepBuilding.exe", "ScanLogs.html,扫楼日志");
  33. }
  34. public void GetConfig() {
  35. //反射获取获取projId,userId
  36. var path = this.GetType().Assembly.Location;
  37. var dllPath= System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "SAGA.MBI.exe");
  38. Assembly asm = Assembly.LoadFile(dllPath);
  39. Type type = asm.GetType("SAGA.MBI.Common.MBIControl");
  40. System.Reflection.PropertyInfo projectCur = type.GetProperty("ProjectCur"); //获取指定名称的属性
  41. System.Reflection.PropertyInfo manageInfo = type.GetProperty("ManageInfo"); //获取指定名称的属性
  42. var p = projectCur.GetValue(null, null); //获取属性值
  43. var m = manageInfo.GetValue(null, null); //获取属性值
  44. }
  45. /// <summary>
  46. /// 起始位置
  47. /// </summary>
  48. Point startPoint;
  49. /// <summary>
  50. /// 点集合
  51. /// </summary>
  52. List<Point> pointList = new List<Point>();
  53. /// <summary>
  54. /// 鼠标左键按下获取开始Point
  55. /// </summary>
  56. private void canvas_MouseDown(object sender, MouseButtonEventArgs e) {
  57. }
  58. private void canvas_MouseMove(object sender, MouseEventArgs e) {
  59. }
  60. private void canvas_MouseUp(object sender, MouseButtonEventArgs e) {
  61. }
  62. private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
  63. }
  64. private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
  65. }
  66. private void canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
  67. }
  68. private void canvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
  69. startPoint = e.GetPosition(canvas);
  70. }
  71. private void canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
  72. Point point = e.GetPosition(canvas);
  73. if (isDraw == true)
  74. {
  75. if (isDraw == true && point != this.startPoint && this.startPoint != null) {
  76. var l = new SgLine(startPoint, point);
  77. string color = "红色";
  78. if (color == "默认") {
  79. l.Stroke = Brushes.Black;
  80. }
  81. if (color == "红色") {
  82. l.Stroke = Brushes.Red;
  83. }
  84. if (color == "绿色") {
  85. l.Stroke = Brushes.Green;
  86. }
  87. l.StrokeThickness = 1;
  88. //l.X1 = startPoint.X; // count-2 保证 line的起始点为点集合中的倒数第二个点。
  89. //l.Y1 = startPoint.Y;
  90. //// 终点X,Y 为当前point的X,Y
  91. //l.X2 = point.X;
  92. //l.Y2 = point.Y;
  93. canvas.Children.Add(l);
  94. }
  95. }
  96. else if (isDraw == false)//擦除线
  97. {
  98. foreach (var canvasChild in this.canvas.Children)
  99. {
  100. if (canvasChild is SgLine line)
  101. {
  102. if (PointIsInline(line, point))
  103. {
  104. canvas.Children.Remove(line);
  105. return;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// 按下鼠标左键移动
  113. /// </summary>
  114. private void canvas_PreviewMouseMove(object sender, MouseEventArgs e) {
  115. return;
  116. if (e.LeftButton == MouseButtonState.Pressed) {
  117. // 返回指针相对于Canvas的位置
  118. Point point = e.GetPosition(canvas);
  119. if (pointList.Count == 0) {
  120. // 加入起始点
  121. pointList.Add(new Point(this.startPoint.X, this.startPoint.Y));
  122. }
  123. else {
  124. // 加入移动过程中的point
  125. pointList.Add(point);
  126. }
  127. // 去重复点
  128. var disList = pointList.Distinct().ToList();
  129. var count = disList.Count(); // 总点数
  130. if (point != this.startPoint && this.startPoint != null) {
  131. var l = new Line();
  132. string color = "红色";
  133. if (color == "默认") {
  134. l.Stroke = Brushes.Black;
  135. }
  136. if (color == "红色") {
  137. l.Stroke = Brushes.Red;
  138. }
  139. if (color == "绿色") {
  140. l.Stroke = Brushes.Green;
  141. }
  142. l.StrokeThickness = 1;
  143. if (count < 2)
  144. return;
  145. l.X1 = disList[count - 2].X; // count-2 保证 line的起始点为点集合中的倒数第二个点。
  146. l.Y1 = disList[count - 2].Y;
  147. // 终点X,Y 为当前point的X,Y
  148. l.X2 = point.X;
  149. l.Y2 = point.Y;
  150. canvas.Children.Add(l);
  151. }
  152. }
  153. }
  154. private bool? isDraw = null;
  155. private void btnDraw_Click(object sender, RoutedEventArgs e)
  156. {
  157. isDraw = true;
  158. Mouse.SetCursor(Cursors.Pen);
  159. }
  160. private void btnClear_Click(object sender, RoutedEventArgs e)
  161. {
  162. isDraw = false;
  163. Mouse.SetCursor(Cursors.Cross);
  164. }
  165. //判断点是否在直线上
  166. public static bool PointIsInline(SgLine line, Point point)
  167. {
  168. return GetPointIsInLine(point, line.StartPoint, line.EndPoint,3);
  169. }
  170. public static bool GetPointIsInLine(Point pf, Point p1, Point p2, double range=0) {
  171. //range 判断的的误差,不需要误差则赋值0
  172. //点在线段首尾两端之外则return false
  173. double cross = (p2.X - p1.X) * (pf.X - p1.X) + (p2.Y - p1.Y) * (pf.Y - p1.Y);
  174. if (cross <= 0) return false;
  175. double d2 = (p2.X - p1.X) * (p2.X - p1.X) + (p2.Y - p1.Y) * (p2.Y - p1.Y);
  176. if (cross >= d2) return false;
  177. double r = cross / d2;
  178. double px = p1.X + (p2.X - p1.X) * r;
  179. double py = p1.Y + (p2.Y - p1.Y) * r;
  180. //判断距离是否小于误差
  181. return Math.Sqrt((pf.X - px) * (pf.X - px) + (py - pf.Y) * (py - pf.Y)) <= range;
  182. }
  183. }
  184. }