Control PathClip() { var control = new Drawable { Size = new Size(350, 250) }; control.Paint += (sender, e) => { var path = new GraphicsPath(); path.AddEllipse(25, 25, 50, 50); path.AddRectangle(125, 25, 50, 50); path.AddLines(new PointF(225, 25), new PointF(225, 75), new PointF(275, 50)); path.CloseFigure(); e.Graphics.SetClip(path); if (ResetClip) e.Graphics.ResetClip(); e.Graphics.FillRectangle(Brushes.Blue, path.Bounds); path.Transform(Matrix.FromTranslation(0, 75)); e.Graphics.SetClip(path); if (ResetClip) e.Graphics.ResetClip(); e.Graphics.FillRectangle(Brushes.Red, path.Bounds); path.Transform(Matrix.FromTranslation(0, 75)); e.Graphics.SetClip(path); if (ResetClip) e.Graphics.ResetClip(); e.Graphics.FillRectangle(Brushes.Green, path.Bounds); }; PropertyChanged += (sender, e) => { if (e.PropertyName == "ResetClip") control.Invalidate(); }; return control; }
GraphicsPath CreatePath() { var newPath = new GraphicsPath(); var start = StartFigures; var close = CloseFigures; // connected segments newPath.MoveTo(10, 10); newPath.LineTo(20, 90); newPath.LineTo(10, 60); newPath.LineTo(90, 80); newPath.LineTo(60, 30); if (close && start) newPath.CloseFigure(); if (start) newPath.StartFigure(); newPath.AddArc(100, 0, 100, 50, 200, -160); if (close && start) newPath.CloseFigure(); if (start) newPath.StartFigure(); newPath.AddBezier(new PointF(200, 10), new PointF(285, 20), new PointF(210, 85), new PointF(300, 90)); if (close && start) newPath.CloseFigure(); if (start) newPath.StartFigure(); newPath.AddCurve(new PointF(310, 90), new PointF(390, 90), new PointF(390, 10), new PointF(310, 10)); if (close && start) newPath.CloseFigure(); if (start) newPath.StartFigure(); newPath.AddLine(410, 10, 410, 90); if (close && start) newPath.CloseFigure(); if (start) newPath.StartFigure(); newPath.AddLines(new PointF(420, 10), new PointF(420, 90)); if (close && start) newPath.CloseFigure(); if (start) newPath.StartFigure(); newPath.AddLines(new PointF(430, 10), new PointF(430, 90)); if (close) newPath.CloseFigure(); // separate segments if (start) newPath.StartFigure(); newPath.AddEllipse(100, 100, 100, 45); if (close) newPath.CloseFigure(); if (start) newPath.StartFigure(); newPath.AddRectangle(10, 110, 80, 80); if (close) newPath.CloseFigure(); // at the end, draw a line so we can potentially connect to parent path if (start) newPath.StartFigure(); newPath.AddLines(new PointF(440, 10), new PointF(440, 90)); if (close) newPath.CloseFigure(); return newPath; }