Exemple #1
0
        public override void Draw(Graphics g, Point startPoint, Point endPoint)
        {
            GPath = new System.Drawing.Drawing2D.GraphicsPath();
            GPath.AddLine(startPoint, endPoint);

            // OutOfMemoryException if two points equal
            if (startPoint != endPoint)
            {
                try
                {
                    GPath.Widen(new Pen(Color.Red, 50F));
                }
                catch
                {
                    // Do nothing
                }
            }

            g.DrawLine(ArrowPen, startPoint, endPoint);

            if (IsFocused || !string.IsNullOrEmpty(MiddleText))
            {
                int    textHeight = (int)Math.Ceiling(g.MeasureString("A", Font).Height);
                double angle      = ShapeHelper.GetAngleBetween2PointsInDegrees(startPoint, endPoint);

                if (IsFocused && !String.IsNullOrEmpty(StartText))
                {
                    int   textLength = (int)Math.Ceiling(g.MeasureString(StartText, Font).Width);
                    Point textPoint  = ShapeHelper.GetPointOnCircle(startPoint, 40, angle);
                    textPoint.X  = textPoint.X - textLength / 2;
                    textPoint.Y -= textHeight / 2;
                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle(textPoint.X, textPoint.Y, textLength, textHeight));
                    g.DrawString(StartText, Font, TextBrush, textPoint);
                }
                if (IsFocused && !string.IsNullOrEmpty(EndText))
                {
                    int   textLength = (int)Math.Ceiling(g.MeasureString(EndText, Font).Width);
                    Point textPoint  = ShapeHelper.GetPointOnCircle(endPoint, -40, angle);
                    textPoint.X  = textPoint.X - textLength / 2;
                    textPoint.Y -= textHeight / 2;
                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle(textPoint.X, textPoint.Y, textLength, textHeight));
                    g.DrawString(EndText, Font, TextBrush, textPoint);
                }
                if (!string.IsNullOrEmpty(MiddleText))
                {
                    int    textLength = (int)Math.Ceiling(g.MeasureString(MiddleText, Font).Width);
                    double lineLength = ShapeHelper.GetLineLength(startPoint, endPoint);
                    Point  textPoint  = ShapeHelper.GetPointOnCircle(startPoint, (int)(lineLength / 2), angle);
                    textPoint.X  = textPoint.X - textLength / 2;
                    textPoint.Y -= textHeight / 2;
                    g.FillRectangle(new SolidBrush(Color.White), new Rectangle(textPoint.X, textPoint.Y, textLength, textHeight));
                    g.DrawString(MiddleText, Font, TextBrush, textPoint);
                }
            }
        }
        public virtual void Draw(Graphics g, Point startPoint, Point endPoint)
        {
            if (startPoint == endPoint)
            {
                return;
            }

            MidPoint = ShapeHelper.GetLineMidPoint(startPoint, endPoint);
            GPath    = new System.Drawing.Drawing2D.GraphicsPath();
            //GPath.AddLine(startPoint, endPoint);

            Point pointA = new Point(startPoint.X + 16, startPoint.Y);
            Point pointB = new Point(endPoint.X - 16, endPoint.Y);

            GPath.AddLine(pointA, pointB);

            // OutOfMemoryException if two points equal
            if (pointA != pointB)
            {
                try
                {
                    GPath.Widen(new Pen(Color.Red, 50F));
                }
                catch
                {
                    // Do nothing
                }
            }

            g.DrawLine(ArrowPen, startPoint, endPoint);
            bool isHorizontal = startPoint.Y == endPoint.Y;
            bool isVertical   = startPoint.X == endPoint.X;

            GPathFinishEnd = new GraphicsPath();
            GPathStartEnd  = new GraphicsPath();

            if ((IsFocused || !ShowEndTextOnlyWhenFocused) || (!string.IsNullOrEmpty(MiddleText) && !ShowMiddleTextOnlyWhenFocused))
            {
                int    textHeight = (int)Math.Ceiling(g.MeasureString("A", Font).Height);
                double angle      = ShapeHelper.GetAngleBetween2PointsInDegrees(startPoint, endPoint);

                if ((IsFocused || !ShowEndTextOnlyWhenFocused) && !String.IsNullOrEmpty(StartText))
                {
                    SizeF textSize   = g.MeasureString(StartText, Font);
                    int   textLength = (int)Math.Ceiling(textSize.Width);
                    Point textPoint  = ShapeHelper.GetPointAlongLine(startPoint, endPoint, 35);

                    if (isHorizontal)
                    {
                        if (startPoint.X < endPoint.X)
                        {
                            textPoint.X  = startPoint.X + 10;
                            textPoint.Y -= textHeight + 2;
                        }
                        else
                        {
                            textPoint.X = startPoint.X - textLength - 5;
                            textPoint.Y = startPoint.Y + 2;
                        }
                    }
                    else if (isVertical)
                    {
                        if (startPoint.Y > endPoint.Y)
                        {
                            textPoint.X = startPoint.X - textLength / 2;
                            textPoint.Y = startPoint.Y - textHeight * 2;
                        }
                        else
                        {
                            textPoint.X = startPoint.X - textLength / 2;
                            textPoint.Y = startPoint.Y + textHeight;
                        }
                    }
                    else
                    {
                        textPoint.X  = textPoint.X - textLength / 2;
                        textPoint.Y -= textHeight / 2;
                    }
                    EndRectangle1 = new Rectangle(textPoint, new Size(textLength, textHeight));

                    if (!isHorizontal && !isVertical)
                    {
                        g.FillRectangle(BackBrush, EndRectangle1);
                    }

                    g.DrawString(StartText, Font, TextBrush, textPoint);
                    GPathFinishEnd.AddRectangle(EndRectangle1);
                    GPathStartImage = new GraphicsPath();

                    if (IsFocused && StartImage != null)
                    {
                        //StartImageRectangle = new Rectangle(EndRectangle1.Left - StartImage.Width - 2, EndRectangle1.Top, StartImage.Width, StartImage.Height);
                        StartImageRectangle = new Rectangle(EndRectangle1.Right + 2, EndRectangle1.Top, StartImage.Width, StartImage.Height);
                        g.FillRectangle(BackBrush, StartImageRectangle);
                        g.DrawImage(StartImage, StartImageRectangle);
                        GPathStartImage.AddRectangle(StartImageRectangle);
                    }
                }
                if ((IsFocused || !ShowEndTextOnlyWhenFocused) && !string.IsNullOrEmpty(EndText))
                {
                    int   textLength = (int)Math.Ceiling(g.MeasureString(EndText, Font).Width);
                    Point textPoint  = ShapeHelper.GetPointAlongLine(endPoint, startPoint, 35);

                    if (isHorizontal)
                    {
                        if (startPoint.X < endPoint.X)
                        {
                            textPoint.X = endPoint.X - textLength - 5;
                            textPoint.Y = endPoint.Y + 2;
                        }
                        else
                        {
                            textPoint.X  = endPoint.X + 10;
                            textPoint.Y -= textHeight + 2;
                        }
                    }
                    else if (isVertical)
                    {
                        if (startPoint.Y > endPoint.Y)
                        {
                            textPoint.X = endPoint.X - textLength / 2;
                            textPoint.Y = endPoint.Y + textHeight;
                        }
                        else
                        {
                            textPoint.X = endPoint.X - textLength / 2;
                            textPoint.Y = endPoint.Y - textHeight * 2;
                        }
                    }
                    else
                    {
                        textPoint.X  = textPoint.X - textLength / 2;
                        textPoint.Y -= textHeight / 2;
                    }
                    EndRectangle2 = new Rectangle(textPoint, new Size(textLength, textHeight));

                    if (!isHorizontal && !isVertical)
                    {
                        g.FillRectangle(BackBrush, EndRectangle2);
                    }

                    g.DrawString(EndText, Font, TextBrush, textPoint);
                    GPathStartEnd.AddRectangle(EndRectangle2);

                    GPathEndImage = new GraphicsPath();

                    if (IsFocused && StartImage != null)
                    {
                        //EndImageRectangle = new Rectangle(EndRectangle2.Right + 2, EndRectangle2.Bottom - EndImage.Height, EndImage.Width, EndImage.Height);
                        EndImageRectangle = new Rectangle(EndRectangle2.Left - EndImage.Width - 2, EndRectangle2.Bottom - EndImage.Height, EndImage.Width, EndImage.Height);
                        g.FillRectangle(BackBrush, EndImageRectangle);
                        g.DrawImage(EndImage, EndImageRectangle);
                        GPathEndImage.AddRectangle(EndImageRectangle);
                    }
                }
                if (!string.IsNullOrEmpty(MiddleText) &&
                    (IsFocused || !ShowMiddleTextOnlyWhenFocused))
                {
                    int   textLength = (int)Math.Ceiling(g.MeasureString(MiddleText, Font).Width);
                    Point textPoint  = MidPoint;

                    if (isHorizontal)
                    {
                        textPoint.X  = textPoint.X - textLength / 2;
                        textPoint.Y -= textHeight + 2;

                        if (MiddleImage != null)
                        {
                            textPoint.Y -= MiddleImage.Height;
                        }
                    }
                    else
                    {
                        textPoint.X  = textPoint.X - textLength / 2;
                        textPoint.Y -= textHeight * MiddleTextLineCount + MiddleImage.Height / 2 + 2;
                    }
                    g.FillRectangle(BackBrush, new Rectangle(textPoint.X, textPoint.Y, textLength, textHeight * MiddleTextLineCount));
                    g.DrawString(MiddleText, Font, TextBrush, textPoint);
                }
            }
            GPathMiddleImage = new System.Drawing.Drawing2D.GraphicsPath();
            Image image;

            if (!IsFocused)
            {
                image = MiddleImage;
            }
            else
            {
                image = MiddleImageFocused != null ? MiddleImageFocused : MiddleImage;
            }

            if (image != null)
            {
                Point imagePos = MidPoint;
                imagePos.Offset(-1 * image.Width / 2, -1 * image.Height / 2);
                MiddleImageRectangle = new Rectangle(imagePos, image.Size);

                g.FillRectangle(BackBrush, MiddleImageRectangle);
                g.DrawImage(image, MiddleImageRectangle);
                GPathMiddleImage.AddRectangle(MiddleImageRectangle);
            }
            //#region Add circles at each end
            //int radius = 20;
            //Point circleCentre = ShapeHelper.GetPointAlongLine(startPoint, endPoint, radius);
            //GPathEnd1.AddPath(ShapeHelper.GetCirclePath(circleCentre, radius), false);
            ////g.DrawPath(new Pen(TextBrush), GPathEnd1);

            //circleCentre = ShapeHelper.GetPointAlongLine(endPoint, startPoint, radius);
            //GPathEnd2.AddPath(ShapeHelper.GetCirclePath(circleCentre, radius), false);
            ////g.DrawPath(new Pen(TextBrush), GPathEnd2);
            //#endregion
        }