| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using SweepBuilding;
- namespace WpfApp1 {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window {
- public MainWindow() {
- InitializeComponent();
- //var info = new UrlInfo() {
- // Url = "ScanLogs.html",
- // Title = "扫楼日志",
- // //ProjId = MBIControl.ProjectCur.Id,
- // // UserId = MBIControl.ManageInfo.Person_Id
- //};
- ////var win = new SweepBuilding.SweepBuilding(info);
- ////win.ShowDialog();
- //System.Diagnostics.Process.Start("SAGA.SweepBuilding.exe", "ScanLogs.html,扫楼日志");
- }
- public void GetConfig() {
- //反射获取获取projId,userId
- var path = this.GetType().Assembly.Location;
- var dllPath= System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "SAGA.MBI.exe");
- Assembly asm = Assembly.LoadFile(dllPath);
- Type type = asm.GetType("SAGA.MBI.Common.MBIControl");
- System.Reflection.PropertyInfo projectCur = type.GetProperty("ProjectCur"); //获取指定名称的属性
- System.Reflection.PropertyInfo manageInfo = type.GetProperty("ManageInfo"); //获取指定名称的属性
- var p = projectCur.GetValue(null, null); //获取属性值
- var m = manageInfo.GetValue(null, null); //获取属性值
- }
- /// <summary>
- /// 起始位置
- /// </summary>
- Point startPoint;
- /// <summary>
- /// 点集合
- /// </summary>
- List<Point> pointList = new List<Point>();
- /// <summary>
- /// 鼠标左键按下获取开始Point
- /// </summary>
- private void canvas_MouseDown(object sender, MouseButtonEventArgs e) {
- }
- private void canvas_MouseMove(object sender, MouseEventArgs e) {
- }
- private void canvas_MouseUp(object sender, MouseButtonEventArgs e) {
- }
- private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
- }
- private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
- }
- private void canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
- }
- private void canvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
- startPoint = e.GetPosition(canvas);
- }
- private void canvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
- Point point = e.GetPosition(canvas);
- if (isDraw == true)
- {
- if (isDraw == true && point != this.startPoint && this.startPoint != null) {
- var l = new SgLine(startPoint, point);
- string color = "红色";
- if (color == "默认") {
- l.Stroke = Brushes.Black;
- }
- if (color == "红色") {
- l.Stroke = Brushes.Red;
- }
- if (color == "绿色") {
- l.Stroke = Brushes.Green;
- }
- l.StrokeThickness = 1;
- //l.X1 = startPoint.X; // count-2 保证 line的起始点为点集合中的倒数第二个点。
- //l.Y1 = startPoint.Y;
- //// 终点X,Y 为当前point的X,Y
- //l.X2 = point.X;
- //l.Y2 = point.Y;
- canvas.Children.Add(l);
- }
- }
- else if (isDraw == false)//擦除线
- {
- foreach (var canvasChild in this.canvas.Children)
- {
- if (canvasChild is SgLine line)
- {
- if (PointIsInline(line, point))
- {
- canvas.Children.Remove(line);
- return;
- }
- }
- }
- }
-
- }
- /// <summary>
- /// 按下鼠标左键移动
- /// </summary>
- private void canvas_PreviewMouseMove(object sender, MouseEventArgs e) {
- return;
- if (e.LeftButton == MouseButtonState.Pressed) {
- // 返回指针相对于Canvas的位置
- Point point = e.GetPosition(canvas);
- if (pointList.Count == 0) {
- // 加入起始点
- pointList.Add(new Point(this.startPoint.X, this.startPoint.Y));
- }
- else {
- // 加入移动过程中的point
- pointList.Add(point);
- }
- // 去重复点
- var disList = pointList.Distinct().ToList();
- var count = disList.Count(); // 总点数
- if (point != this.startPoint && this.startPoint != null) {
- var l = new Line();
- string color = "红色";
- if (color == "默认") {
- l.Stroke = Brushes.Black;
- }
- if (color == "红色") {
- l.Stroke = Brushes.Red;
- }
- if (color == "绿色") {
- l.Stroke = Brushes.Green;
- }
- l.StrokeThickness = 1;
- if (count < 2)
- return;
- l.X1 = disList[count - 2].X; // count-2 保证 line的起始点为点集合中的倒数第二个点。
- l.Y1 = disList[count - 2].Y;
- // 终点X,Y 为当前point的X,Y
- l.X2 = point.X;
- l.Y2 = point.Y;
- canvas.Children.Add(l);
- }
- }
- }
- private bool? isDraw = null;
- private void btnDraw_Click(object sender, RoutedEventArgs e)
- {
- isDraw = true;
- Mouse.SetCursor(Cursors.Pen);
- }
- private void btnClear_Click(object sender, RoutedEventArgs e)
- {
- isDraw = false;
- Mouse.SetCursor(Cursors.Cross);
- }
- //判断点是否在直线上
- public static bool PointIsInline(SgLine line, Point point)
- {
- return GetPointIsInLine(point, line.StartPoint, line.EndPoint,3);
- }
- public static bool GetPointIsInLine(Point pf, Point p1, Point p2, double range=0) {
- //range 判断的的误差,不需要误差则赋值0
- //点在线段首尾两端之外则return false
- double cross = (p2.X - p1.X) * (pf.X - p1.X) + (p2.Y - p1.Y) * (pf.Y - p1.Y);
- if (cross <= 0) return false;
- double d2 = (p2.X - p1.X) * (p2.X - p1.X) + (p2.Y - p1.Y) * (p2.Y - p1.Y);
- if (cross >= d2) return false;
- double r = cross / d2;
- double px = p1.X + (p2.X - p1.X) * r;
- double py = p1.Y + (p2.Y - p1.Y) * r;
- //判断距离是否小于误差
- return Math.Sqrt((pf.X - px) * (pf.X - px) + (py - pf.Y) * (py - pf.Y)) <= range;
- }
- }
- }
|