//step: //0 初始 //1 按下左键开始拖动 //2 完成选择的拖动,确认选择,再次按下左键开始拖动,若按下右键取消选择从0开始 //3 拖动开始,光标改为箭头,释放左键完成拖动 public override void MouseLeftButtonDown(object sender, Point point) { if (Step == 0) { //Point p = e.GetPosition(CMain); RectangleGeometry rect = new RectangleGeometry(); rect.Rect = new Rect(point, point); //p1 = p; Path path = CreatePath(); path.Tag = point; path.Data = rect; path.StrokeThickness = 0.8; path.StrokeDashArray = new DoubleCollection() { 5, 5 }; CMain.Children.Add(path); Step = 1; selectedPathes.Clear(); } else if (Step == 1) { if (CMain.Children.Count > CommonParam._defaultCount + 1) { Rect selected = Next(point); //MyCapture.Points.Add(rect.BottomLeft); //MyCapture.Points.Add(rect.BottomRight); //MyCapture.Points.Add(rect.TopLeft); //MyCapture.Points.Add(rect.TopRight); for (int i = CommonParam._defaultCount; i < CMain.Children.Count - 1; i++) { var temp = CMain.Children[i]; if (temp is Path) { Path path = temp as Path; path.Stroke = Brushes.White; if (path != null) { if (path.Data is LineGeometry) { LineGeometry line = path.Data as LineGeometry; bool p1 = CommonFun.IsPointIn(selected, line.StartPoint); bool p2 = CommonFun.IsPointIn(selected, line.EndPoint); if (p1 && p2) { path.Stroke = Brushes.LightBlue; selectedPathes.Add(path); } } else if (path.Data is RectangleGeometry) { RectangleGeometry rect = path.Data as RectangleGeometry; bool p1 = CommonFun.IsPointIn(selected, rect.Rect.BottomLeft); bool p2 = CommonFun.IsPointIn(selected, rect.Rect.BottomRight); bool p3 = CommonFun.IsPointIn(selected, rect.Rect.TopLeft); bool p4 = CommonFun.IsPointIn(selected, rect.Rect.TopRight); if (p1 && p2 && p3 && p4) { path.Stroke = Brushes.LightBlue; selectedPathes.Add(path); } } else if (path.Data is EllipseGeometry) { EllipseGeometry ellipse = path.Data as EllipseGeometry; bool p1 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X - ellipse.RadiusX, ellipse.Center.Y)); bool p2 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X + ellipse.RadiusX, ellipse.Center.Y)); bool p3 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X, ellipse.Center.Y + ellipse.RadiusY)); bool p4 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X, ellipse.Center.Y + ellipse.RadiusY)); if (p1 && p2 && p3 && p4) { path.Stroke = Brushes.LightBlue; selectedPathes.Add(path); } } } } } } CMain.Children.RemoveAt(CMain.Children.Count - 1); if (selectedPathes.Count > 0) { Step = 2; } else { Step = 0; } } else if (Step == 2) { pm1 = point; CMain.Cursor = Cursors.Arrow; Step = 3; } }
protected Point AddSelected(Point point) { Rect selected = Next(point); //MyCapture.Points.Add(rect.BottomLeft); //MyCapture.Points.Add(rect.BottomRight); //MyCapture.Points.Add(rect.TopLeft); //MyCapture.Points.Add(rect.TopRight); for (int i = CommonParam._defaultCount; i < CMain.Children.Count - 1; i++) { var temp = CMain.Children[i]; if (temp is Path) { Path path = temp as Path; path.Stroke = Brushes.White; if (path != null) { if (path.Data is LineGeometry) { LineGeometry line = path.Data as LineGeometry; bool p1 = CommonFun.IsPointIn(selected, line.StartPoint); bool p2 = CommonFun.IsPointIn(selected, line.EndPoint); if (p1 && p2) { path.Stroke = Brushes.LightBlue; selectedPathes.Add(path); } } else if (path.Data is RectangleGeometry) { RectangleGeometry rect = path.Data as RectangleGeometry; bool p1 = CommonFun.IsPointIn(selected, rect.Rect.BottomLeft); bool p2 = CommonFun.IsPointIn(selected, rect.Rect.BottomRight); bool p3 = CommonFun.IsPointIn(selected, rect.Rect.TopLeft); bool p4 = CommonFun.IsPointIn(selected, rect.Rect.TopRight); if (p1 && p2 && p3 && p4) { path.Stroke = Brushes.LightBlue; selectedPathes.Add(path); } } else if (path.Data is EllipseGeometry) { EllipseGeometry ellipse = path.Data as EllipseGeometry; bool p1 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X - ellipse.RadiusX, ellipse.Center.Y)); bool p2 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X + ellipse.RadiusX, ellipse.Center.Y)); bool p3 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X, ellipse.Center.Y + ellipse.RadiusY)); bool p4 = CommonFun.IsPointIn(selected, new Point(ellipse.Center.X, ellipse.Center.Y + ellipse.RadiusY)); if (p1 && p2 && p3 && p4) { path.Stroke = Brushes.LightBlue; selectedPathes.Add(path); } } } } else if (temp is TextBlock) { TextBlock edit = temp as TextBlock; double x = Canvas.GetLeft(edit); double y = Canvas.GetTop(edit); bool p1 = CommonFun.IsPointIn(selected, new Point(x, y)); bool p2 = CommonFun.IsPointIn(selected, new Point(x + edit.ActualWidth, y + edit.ActualHeight)); if (p1 && p2) { edit.Foreground = Brushes.LightBlue; selectedPathes.Add(edit); } } } return(point); }