Exemplo n.º 1
0
        protected CustomLineCap GetClone(Pen pen, float size, bool startCap)
        {
            float endPoint;

            if (pen.Width * _designWidth < size)
            {
                endPoint = pen.Width == 0 ? 1 : size / pen.Width;
            }
            else
            {
                endPoint = _designWidth;
            }

            if (startCap)
            {
                endPoint = -endPoint;
            }

            GraphicsPath hPath = new GraphicsPath();

            // Create the outline for our custom end cap.
            hPath.AddLine(new PointF(endPoint < 0 ? 0.5f : -0.5f, 0), new PointF(endPoint / 2, 0));
            CustomLineCap clone = new CustomLineCap(null, hPath); // we set the stroke path only

            clone.SetStrokeCaps(LineCap.Flat, LineCap.Flat);
            return(clone);
        }
Exemplo n.º 2
0
        private CustomLineCap GetClone(Pen pen, float size)
        {
            float endPoint;

            endPoint  = pen.Width == 0 ? 1 : size / pen.Width;
            endPoint /= 2;
            if (endPoint <= 0)
            {
                endPoint = 1e-3f;
            }

            var hPath = new GraphicsPath();

            var r = endPoint / (2 * Math.Sin(_designAngle * (Math.PI / 180)));
            var b = r - r * Math.Cos(_designAngle * (Math.PI / 180));
            var h = endPoint / 2;

            // Create the outline for our custom end cap.

            hPath.AddArc(
                (float)(-r - h), (float)(-b),
                (float)(2 * r), (float)(2 * r),
                (float)(-90 - _designAngle), (float)(2 * _designAngle));

            hPath.AddArc(
                (float)(h - r), (float)(b - 1.999999 * r),
                (float)(2 * r), (float)(2 * r),
                (float)(90 + _designAngle), (float)(-2 * _designAngle));

            var clone = new CustomLineCap(null, hPath); // we set the stroke path only

            clone.SetStrokeCaps(LineCap.Flat, LineCap.Flat);
            return(clone);
        }
Exemplo n.º 3
0
        private CustomLineCap GetClone(Pen pen, float size)
        {
            float endPoint;

            endPoint = pen.Width == 0 ? 1 : size / (pen.Width * 2) - 0.5f;

            if (endPoint < 0)
            {
                endPoint = 1e-3f;
            }

            var hPath = new GraphicsPath();

            // Create the outline for our custom end cap.
            hPath.AddPolygon(new PointF[] {
                new PointF(endPoint, -endPoint),
                new PointF(endPoint, endPoint),
                new PointF(-endPoint, endPoint),
                new PointF(-endPoint, -endPoint),
            });
            var clone = new CustomLineCap(null, hPath, LineCap.Flat, endPoint); // we set the stroke path only

            clone.SetStrokeCaps(LineCap.Flat, LineCap.Flat);
            return(clone);
        }
Exemplo n.º 4
0
        private CustomLineCap GetClone(Pen pen, float size)
        {
            float endPoint;

            endPoint = pen.Width == 0 ? 1 : size / (pen.Width);
            if (endPoint <= 0)
            {
                endPoint = 1e-3f;
            }

            float c = 0.8660254f; //  0.5 / Math.Tan(30°);

            float r = 1;          // 0.5/Math.Sin(30°);

            var hPath = new GraphicsPath();

            // Create the outline for our custom end cap.
            hPath.AddPolygon(new PointF[] {
                new PointF(0, -endPoint * 0.866f + r),
                new PointF(endPoint / 2 - c, -0.5f),
                new PointF(-endPoint / 2 + c, -0.5f),
            });
            var clone = new CustomLineCap(null, hPath, LineCap.Flat, endPoint * 0.866f - r); // we set the stroke path only

            clone.SetStrokeCaps(LineCap.Flat, LineCap.Flat);
            return(clone);
        }
Exemplo n.º 5
0
        public void SetThenGetStrokeCaps_Success(LineCap startCap, LineCap endCap)
        {
            using (GraphicsPath strokePath = new GraphicsPath())
                using (CustomLineCap customLineCap = new CustomLineCap(null, strokePath))
                {
                    customLineCap.SetStrokeCaps(startCap, endCap);
                    customLineCap.GetStrokeCaps(out LineCap retrievedStartCap, out LineCap retrievedEndCap);

                    Assert.Equal(startCap, retrievedStartCap);
                    Assert.Equal(endCap, retrievedEndCap);
                }
        }
Exemplo n.º 6
0
        [InlineData(LineCap.Custom + 1, LineCap.Flat)] // Above valid enum range
        public void SetStrokeCaps_InvalidLineCap_ThrowsArgumentException(LineCap startCap, LineCap endCap)
        {
            using (GraphicsPath strokePath = new GraphicsPath())
                using (CustomLineCap customLineCap = new CustomLineCap(null, strokePath))
                {
                    AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.SetStrokeCaps(startCap, endCap));

                    // start and end cap should be unchanged.
                    customLineCap.GetStrokeCaps(out LineCap retrievedStartCap, out LineCap retrievedEndCap);
                    Assert.Equal(LineCap.Flat, retrievedStartCap);
                    Assert.Equal(LineCap.Flat, retrievedEndCap);
                }
        }
Exemplo n.º 7
0
                /// <summary>
                /// Serialization constructor
                /// </summary>
                /// <param name="info"></param>
                /// <param name="context"></param>
                public CustomLineCapRef(SerializationInfo info, StreamingContext context)
                {
                    var baseCap    = (LineCap)info.GetInt32("BaseCap");
                    var baseInset  = info.GetSingle("BaseInset");
                    var fillPath   = (GraphicsPath)info.GetValue("FillPath", (typeof(GraphicsPath)));
                    var strokePath = (GraphicsPath)info.GetValue("StrokePath", (typeof(GraphicsPath)));

                    var startCap = (LineCap)info.GetInt32("StartCap");
                    var endCap   = (LineCap)info.GetInt32("StartCap");

                    _clp            = new CustomLineCap(fillPath, strokePath, baseCap, baseInset);
                    _clp.StrokeJoin = (LineJoin)info.GetInt32("StrokeJoin");
                    _clp.WidthScale = info.GetSingle("WidthScale");
                    _clp.SetStrokeCaps(startCap, endCap);
                }
Exemplo n.º 8
0
 public void Disposed_MembersThrow()
 {
     using (GraphicsPath strokePath = new GraphicsPath())
         using (CustomLineCap customLineCap = new CustomLineCap(null, strokePath))
         {
             customLineCap.Dispose();
             AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.StrokeJoin);
             AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.BaseCap);
             AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.BaseInset);
             AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.WidthScale);
             AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.Clone());
             AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.SetStrokeCaps(LineCap.Flat, LineCap.Flat));
             AssertExtensions.Throws <ArgumentException>(null, () => customLineCap.GetStrokeCaps(out LineCap startCap, out LineCap endCap));
         }
 }
Exemplo n.º 9
0
        private CustomLineCap GetClone(Pen pen, float size)
        {
            float endPoint;

            endPoint = pen.Width == 0 ? 1 : size / pen.Width;

            var hPath = new GraphicsPath();

            // Create the outline for our custom end cap.
            hPath.AddLine(new PointF(-endPoint / 2, 0), new PointF(endPoint / 2, 0));
            var clone = new CustomLineCap(null, hPath); // we set the stroke path only

            clone.SetStrokeCaps(LineCap.Flat, LineCap.Flat);
            return(clone);
        }
Exemplo n.º 10
0
        private void DrawIncludes(Point start, Point end)
        {
            GraphicsPath hPath = new GraphicsPath();

            hPath.AddLine(0, 0, -2, -2);
            hPath.AddLine(0, 0, 2, -2);
            CustomLineCap HookCap = new CustomLineCap(null, hPath);

            HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
            HookCap.WidthScale = 1.7f;
            float[] dashValues = { 2, 2, 2, 2 };
            Pen     arrowPen   = new Pen(this.Pen.Color, this.Pen.Width);

            arrowPen.DashPattern  = dashValues;
            arrowPen.CustomEndCap = HookCap;
            this.Graphics.DrawLine(arrowPen, start, end);
        }
Exemplo n.º 11
0
        private CustomLineCap ArrowLineCap()
        {
            float        num          = 1f;
            GraphicsPath graphicsPath = new GraphicsPath();

            graphicsPath.AddLines(new PointF[5]
            {
                new PointF(0f, 0f),
                new PointF(-1f * num, 0f),
                new PointF(0f, 3f * num),
                new PointF(1f * num, 0f),
                new PointF(0f, 0f)
            });
            graphicsPath.CloseAllFigures();
            CustomLineCap customLineCap = new CustomLineCap(null, graphicsPath, LineCap.ArrowAnchor);

            customLineCap.SetStrokeCaps(LineCap.Flat, LineCap.Flat);
            graphicsPath.Dispose();
            return(customLineCap);
        }
Exemplo n.º 12
0
        static Edge2()
        {
            GraphicsPath hPath = new GraphicsPath();

            // Create the outline for the custom end cap.
            float sin = arrowSize * (float)Math.Sin(Math.PI / 3);
            float cos = arrowSize * 0.5f;

            hPath.AddPolygon(new PointF[] { new PointF(0, 0), new PointF(cos, -sin), new PointF(-cos, -sin) });

            // Construct the hook-shaped end cap.
            CustomLineCap HookCap = new CustomLineCap(hPath, null);

            // Set the start cap and end cap of the HookCap to be rounded.
            HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);

            linePen              = new Pen(Color.Black, 1f);
            linePen.StartCap     = LineCap.RoundAnchor;
            linePen.CustomEndCap = HookCap;
            linePen.LineJoin     = LineJoin.Round;
        }
Exemplo n.º 13
0
        ////////////////////////
        protected void DrawCaps(PaintEventArgs e)
        {
            GraphicsPath hPath = new GraphicsPath();

            // Create the outline for our custom end cap.
            hPath.AddLine(new Point(0, 0), new Point(0, 5));
            hPath.AddLine(new Point(0, 5), new Point(5, 1));
            hPath.AddLine(new Point(5, 1), new Point(3, 1));

            // Construct the hook-shaped end cap.
            CustomLineCap HookCap = new CustomLineCap(null, hPath);

            // Set the start cap and end cap of the HookCap to be rounded.
            HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);

            // Create a pen and set end custom start and end
            // caps to the hook cap.
            Pen customCapPen = new Pen(Color.Black, 5);

            customCapPen.CustomStartCap = HookCap;
            customCapPen.CustomEndCap   = HookCap;

            // Create a second pen using the start and end caps from
            // the hook cap.
            Pen     capPen = new Pen(Color.Red, 10);
            LineCap startCap;
            LineCap endCap;

            HookCap.GetStrokeCaps(out startCap, out endCap);
            capPen.StartCap = startCap;
            capPen.EndCap   = endCap;

            // Create a line to draw.
            Point[] points = { new Point(100, 100), new Point(200, 50),
                               new Point(250, 300) };

            // Draw the lines.
            e.Graphics.DrawLines(capPen, points);
            e.Graphics.DrawLines(customCapPen, points);
        }
Exemplo n.º 14
0
        private void SetStrokeCapsMenu_Click(object sender,
                                             System.EventArgs e)
        {
            // Create a graphic
            Graphics g = this.CreateGraphics();

            g.Clear(this.BackColor);
            // Create a path for custom line cap. This
            // path will have two lines from points
            // -3, -3 to 0, 0 and 0, 0 to 3, -3
            Point[] points =
            {
                new Point(-3, -3),
                new Point(0,   0),
                new Point(3, -3)
            };
            GraphicsPath path = new GraphicsPath();

            path.AddLines(points);
            // Create a Custom line cap from the path
            CustomLineCap cap =
                new CustomLineCap(null, path);

            // Set the start and end caps of the Custom cap
            cap.SetStrokeCaps(LineCap.Round, LineCap.Triangle);
            // Create a Pen object and set its start and end
            // caps
            Pen redPen = new Pen(Color.Red, 15);

            redPen.CustomStartCap = cap;
            redPen.CustomEndCap   = cap;
            redPen.DashStyle      = DashStyle.DashDotDot;
            // Draw the line
            g.DrawLine(redPen,
                       new Point(100, 100),
                       new Point(400, 100));
            // Dispose
            g.Dispose();
        }
Exemplo n.º 15
0
        public ConnectionDrawer()
        {
            penLine.EndCap = LineCap.ArrowAnchor;

            GraphicsPath hPath = new GraphicsPath();

            // Create the outline for our custom end cap.

            //hPath.AddLine(new Point(1, -8), new Point(0,-4));

            //hPath.AddLine(new Point(0,-4), new Point(-1,-8));

            //hPath.AddLine(new Point(-1,-8), new Point(1,-8));

            int bias      = 14;
            int halfWidth = 2;
            int length    = 4;

            hPath.AddLine(new Point(halfWidth, -bias), new Point(0, -bias + length));

            hPath.AddLine(new Point(0, -bias + length), new Point(-halfWidth, -bias));

            hPath.AddLine(new Point(-halfWidth, -bias), new Point(halfWidth, -bias));

            //hPath.AddLine(new Point(0, 0), new Point(0, 10));


            // Construct the hook-shaped end cap.

            HookCap = new CustomLineCap(null, hPath);

            // Set the start cap and end cap of the HookCap to be rounded.

            HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);

            penLine.CustomEndCap = HookCap;

            LineWidth = 1;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates a custom line cap given the <paramref name="lineShape"/> and scales.
        /// </summary>
        /// <param name="lineShape">Desired line shape.</param>
        /// <param name="scaleFill">Scale value used for drawing elements that are filled.</param>
        /// <param name="scaleStroke">Scale value used for drawing elements that are just outlines.</param>
        /// <returns>
        /// New <see cref="CustomLineCap"/> configured to produce <paramref name="lineShape"/>.
        /// </returns>
        private static CustomLineCap GetLineShapeCap(LineShape lineShape, float scaleFill, float scaleStroke)
        {
            float baseInset = 0.0f;
            bool  fill      = true;

            using (var path = new GraphicsPath())
            {
                switch (lineShape)
                {
                case LineShape.Arrow:
                {
                    var size   = new SizeF(2.75f * scaleStroke, 4.0f * scaleStroke);
                    var halfW  = size.Width * 0.5f;
                    var height = size.Height;
                    fill = false;
                    path.AddLines(new PointF[]
                        {
                            new PointF(-halfW, -height), new PointF(0, 0), new PointF(halfW, -height)
                        });
                }
                break;

                case LineShape.SolidArrow:
                {
                    var size     = new SizeF(4.25f * scaleFill, 5.57f * scaleFill);
                    var halfW    = size.Width * 0.5f;
                    var height75 = size.Height * 0.73f;
                    var height25 = size.Height * 0.27f;
                    baseInset = height75;
                    path.AddLines(new PointF[]
                        {
                            new PointF(0, -height75),
                            new PointF(halfW, -height75),
                            new PointF(0, height25),
                            new PointF(-halfW, -height75),
                            new PointF(0, -height75),
                        });
                }
                break;

                case LineShape.Tail:
                {
                    var size   = new SizeF(2.10f * scaleStroke, 4.0f * scaleStroke);
                    var halfW  = size.Width * 0.5f;
                    var height = size.Height;
                    fill = false;
                    path.AddLines(new[] {
                            new PointF(-halfW, 0),
                            new PointF(0, -height * 0.5f),
                            new PointF(halfW, 0)
                        });
                    path.StartFigure();
                    path.AddLines(new[] {
                            new PointF(-halfW, height * 0.5f),
                            new PointF(0, 0),
                            new PointF(halfW, height * 0.5f)
                        });
                }
                break;

                case LineShape.SolidTail:
                {
                    var size     = new SizeF(3.02f * scaleFill, 4.65f * scaleFill);
                    var halfW    = size.Width * 0.5f;
                    var halfH    = size.Height * 0.5f;
                    var height38 = size.Height * 0.38f;
                    var height62 = size.Height * 0.62f;
                    path.AddPolygon(new[] {
                            new PointF(0, -height38),
                            new PointF(halfW, 0),
                            new PointF(halfW, height62),
                            new PointF(0, height62 - height38),
                            new PointF(-halfW, height62),
                            new PointF(-halfW, 0),
                            new PointF(0, -height38),
                        });
                }
                break;

                case LineShape.Circle:
                {
                    var size = new SizeF(2.4f * scaleFill, 2.4f * scaleFill);
                    path.AddEllipse(-size.Width * 0.5f, -size.Height * 0.5f, size.Width, size.Height);
                }
                break;

                case LineShape.Square:
                {
                    var size = new SizeF(3.0f * scaleFill, 3.0f * scaleFill);
                    path.AddRectangle(new RectangleF(-size.Width * 0.5f, -size.Height * 0.5f, size.Width, size.Height));
                }
                break;

                default:
                    break;
                }
                var result = new CustomLineCap(fill ? path : null, fill ? null : path, LineCap.Flat, baseInset);
                result.SetStrokeCaps(LineCap.Round, LineCap.Round);
                result.StrokeJoin = LineJoin.Round;
                return(result);
            }
        }