Пример #1
0
        private static void DrawBorderLine(Graphics g, Point[] p, eStyleBorderType border, int lineWidth, Color colorStart, Color colorEnd, Color colorLightStart, Color colorLightEnd, eBorderSide side, BorderDrawInfo drawInfo)
        {
            if (border == eStyleBorderType.Etched || border == eStyleBorderType.Double)
            {
                if (colorLightStart.IsEmpty)
                {
                    colorLightStart = System.Windows.Forms.ControlPaint.Light(colorStart);
                    colorStart = System.Windows.Forms.ControlPaint.Dark(colorStart);
                }

                if (drawInfo.IsTwistedDoubleBorder && (side == eBorderSide.Bottom || side == eBorderSide.Right))
                {
                    Color ct1 = colorStart;
                    Color ct2 = colorEnd;
                    colorStart = colorLightStart;
                    colorEnd = colorLightEnd;
                    colorLightStart = ct1;
                    colorLightEnd = ct2;
                }

                DisplayHelp.DrawGradientLine(g, p[0], p[1], colorStart, colorEnd, 90, lineWidth);

                if (side == eBorderSide.Top)
                {
                    p[0].Y += lineWidth;
                    p[1].Y += lineWidth;
                    if (drawInfo.HasLeftBorder)
                        p[0].X += 1;
                    if (drawInfo.HasRightBorder)
                        p[1].X -= 1;
                }
                else if (side == eBorderSide.Bottom)
                {
                    p[0].Y -= lineWidth;
                    p[1].Y -= lineWidth;
                    if (!drawInfo.IsTwistedDoubleBorder)
                    {
                        if (drawInfo.HasLeftBorder)
                            p[0].X += 1;
                        if (drawInfo.HasRightBorder)
                            p[1].X -= 1;
                    }
                }
                else if (side == eBorderSide.Left || side == eBorderSide.BottomLeft || side == eBorderSide.TopLeft)
                {
                    p[0].X += lineWidth;
                    p[1].X += lineWidth;
                    if (drawInfo.HasTopBorder)
                        p[0].Y += 1;
                    if (drawInfo.HasBottomBorder)
                        p[1].Y -= 1;
                }
                else if (side == eBorderSide.Right || side == eBorderSide.BottomRight || side == eBorderSide.TopRight)
                {
                    p[0].X -= lineWidth;
                    p[1].X -= lineWidth;
                    if (!drawInfo.IsTwistedDoubleBorder)
                    {
                        if (drawInfo.HasTopBorder)
                            p[0].Y += 1;
                        if (drawInfo.HasBottomBorder)
                            p[1].Y -= 1;
                    }
                }

                DisplayHelp.DrawGradientLine(g, p[0], p[1], colorLightStart, colorLightEnd, 90, lineWidth);
            }
            else
            {
                if (colorEnd.IsEmpty)
                {
                    SmoothingMode sm = g.SmoothingMode;
                    g.SmoothingMode = SmoothingMode.None;
                    using (Pen pen = CreatePen(border, lineWidth, colorStart))
                        g.DrawLine(pen, p[0], p[1]);
                    g.SmoothingMode = sm;
                }
                else
                    DisplayHelp.DrawGradientLine(g, p[0], p[1], colorStart, colorEnd, 90, lineWidth);
            }
        }
Пример #2
0
        /// <summary>
        /// Paint style border.
        /// </summary>
        /// <param name="e">Style display information.</param>
        public static void PaintBorder(ElementStyleDisplayInfo e)
        {
            bool disposeStyle = false;
            ElementStyle style = GetElementStyle(e.Style, out disposeStyle);
            Rectangle backBounds = GetBackgroundRectangle(style, e.Bounds);
            Rectangle bounds = ElementStyleDisplay.GetBorderRectangle(style, backBounds);
            try
            {
                if (bounds.Width < 2 || bounds.Height < 2)
                {
                    return;
                }

                eCornerType cornerTopLeft = GetCornerType(style.CornerType, style.CornerTypeTopLeft);
                eCornerType cornerTopRight = GetCornerType(style.CornerType, style.CornerTypeTopRight);
                eCornerType cornerBottomLeft = GetCornerType(style.CornerType, style.CornerTypeBottomLeft);
                eCornerType cornerBottomRight = GetCornerType(style.CornerType, style.CornerTypeBottomRight);

                if (style.PaintBorder && style.Border == eStyleBorderType.Solid && cornerTopLeft == eCornerType.Rounded &&
                    cornerTopRight == eCornerType.Rounded &&
                    cornerBottomLeft == eCornerType.Rounded && cornerBottomRight == eCornerType.Rounded &&
                    style.BorderLeftWidth == style.BorderWidth && style.BorderRightWidth == style.BorderWidth
                    && style.BorderTopWidth == style.BorderWidth && style.BorderBottomWidth == style.BorderWidth)
                {
                    if (style.BorderWidth == 1)
                    {
                        bounds.Width++;
                        bounds.Height++;
                    }
                    DisplayHelp.DrawRoundGradientRectangle(e.Graphics, bounds, style.BorderColor, style.BorderColor2,
                                                           style.BorderGradientAngle, style.BorderWidth,
                                                           style.CornerDiameter);
                    return;
                }
                else if (style.PaintAllBorders && style.Border == eStyleBorderType.Solid &&
                         cornerTopLeft == eCornerType.Square && cornerTopRight == eCornerType.Square &&
                         cornerBottomLeft == eCornerType.Square && cornerBottomRight == eCornerType.Square &&
                         style.BorderLeftWidth == style.BorderWidth && style.BorderRightWidth == style.BorderWidth
                         && style.BorderTopWidth == style.BorderWidth && style.BorderBottomWidth == style.BorderWidth)
                {
                    if (style.BorderWidth == 1)
                    {
                        bounds.Width++;
                        bounds.Height++;
                    }
                    DisplayHelp.DrawGradientRectangle(e.Graphics, bounds, style.BorderColor, style.BorderColor2,
                                                      style.BorderGradientAngle, style.BorderWidth);
                    return;
                }
                else if (style.Border == eStyleBorderType.None && style.BorderRight == eStyleBorderType.Etched &&
                         style.BorderLeft == eStyleBorderType.None &&
                         style.BorderTop == eStyleBorderType.None && style.BorderBottom == eStyleBorderType.None)
                {
                    DisplayHelp.DrawGradientRectangle(e.Graphics,
                                                      new Rectangle(bounds.Right - 2, bounds.Y, 3,
                                                                    bounds.Height + 1),
                                                      style.BorderColorLight,
                                                      style.BorderColorLight2,
                                                      90,
                                                      1);
                    DisplayHelp.DrawGradientLine(e.Graphics,
                                                 new Point(bounds.Right, bounds.Y),
                                                 new Point(bounds.Right, bounds.Bottom + 1),
                                                 style.BorderColor,
                                                 style.BorderColor2,
                                                 90,
                                                 1);
                    return;
                }

                if (!style.BorderColor2.IsEmpty || !style.BorderColorLight.IsEmpty)
                {
                    if (style.Border == eStyleBorderType.Solid)
                    {
                        using (
                            GraphicsPath path = DisplayHelp.GetRoundedRectanglePath(bounds, style.CornerDiameter,
                                                                                    eStyleBackgroundPathPart.Complete,
                                                                                    cornerTopLeft, cornerTopRight,
                                                                                    cornerBottomLeft, cornerBottomRight)
                            )
                        {
                            DisplayHelp.DrawGradientPathBorder(e.Graphics, path, style.BorderColor, style.BorderColor2,
                                                               style.BorderGradientAngle, style.BorderWidth);
                        }
                        return;
                    }
                    else if (style.Border == eStyleBorderType.Etched)
                    {
                        Rectangle r = bounds;

                        r.Width -= style.BorderWidth;
                        r.Height -= style.BorderWidth;
                        r.Offset(style.BorderWidth, style.BorderWidth);
                        if (r.Width > 2 && r.Height > 2)
                        {
                            using (
                                GraphicsPath path = DisplayHelp.GetRoundedRectanglePath(r, style.CornerDiameter,
                                                                                        eStyleBackgroundPathPart.
                                                                                            Complete,
                                                                                        cornerTopLeft, cornerTopRight,
                                                                                        cornerBottomLeft,
                                                                                        cornerBottomRight)
                                )
                            {
                                DisplayHelp.DrawGradientPathBorder(e.Graphics, path, style.BorderColorLight,
                                                                   style.BorderColorLight2,
                                                                   style.BorderLightGradientAngle,
                                                                   style.BorderWidth);
                            }
                            r.Offset(-style.BorderWidth, -style.BorderWidth);
                            using (
                                GraphicsPath path = DisplayHelp.GetRoundedRectanglePath(r, style.CornerDiameter,
                                                                                        eStyleBackgroundPathPart.
                                                                                            Complete,
                                                                                        cornerTopLeft, cornerTopRight,
                                                                                        cornerBottomLeft,
                                                                                        cornerBottomRight)
                                )
                            {
                                DisplayHelp.DrawGradientPathBorder(e.Graphics, path, style.BorderColor,
                                                                   style.BorderColor2,
                                                                   style.BorderGradientAngle, style.BorderWidth);
                            }
                        }
                        return;
                    }
                    else if ((style.BorderTop == eStyleBorderType.Double && style.BorderLeft == eStyleBorderType.Double &&
                              style.BorderRight == eStyleBorderType.Double &&
                              style.BorderBottom == eStyleBorderType.Double) ||
                             (style.BorderTop == eStyleBorderType.None && style.BorderLeft == eStyleBorderType.Double &&
                              style.BorderRight == eStyleBorderType.Double &&
                              style.BorderBottom == eStyleBorderType.Double))
                    {
                        Rectangle r = bounds;
                        Region oldClip = null;
                        bool clipSet = false;
                        if (style.BorderTop == eStyleBorderType.None)
                        {
                            Rectangle clipRect = r;
                            clipRect.Width++;
                            clipRect.Height++;
                            e.Graphics.SetClip(clipRect);
                            r.Y -= 3;
                            r.Height += 3;
                        }
                        using (
                            GraphicsPath path = DisplayHelp.GetBorderPath(r, style.CornerDiameter,
                                                                          eStyleBackgroundPathPart.Complete,
                                                                          cornerTopLeft, cornerTopRight,
                                                                          cornerBottomLeft,
                                                                          cornerBottomRight, style.PaintLeftBorder,
                                                                          style.PaintRightBorder,
                                                                          style.PaintTopBorder,
                                                                          style.PaintBottomBorder))
                        {
                            using (Pen pen = new Pen(style.BorderColor, style.BorderWidth))
                                path.Widen(pen);
                            DisplayHelp.FillPath(e.Graphics, path, style.BorderColor, style.BorderColor2);
                        }
                        r.Inflate(-style.BorderWidth, -style.BorderWidth);
                        using (
                            GraphicsPath path = DisplayHelp.GetBorderPath(r, style.CornerDiameter,
                                                                          eStyleBackgroundPathPart.Complete,
                                                                          cornerTopLeft, cornerTopRight,
                                                                          cornerBottomLeft,
                                                                          cornerBottomRight, style.PaintLeftBorder,
                                                                          style.PaintRightBorder,
                                                                          style.PaintTopBorder,
                                                                          style.PaintBottomBorder))
                        {
                            using (Pen pen = new Pen(style.BorderColor, style.BorderWidth))
                                path.Widen(pen);
                            DisplayHelp.FillPath(e.Graphics, path, style.BorderColorLight, style.BorderColorLight2);
                        }

                        if (clipSet)
                            e.Graphics.Clip = oldClip;

                        return;
                    }
                }


                Color colorStart = style.BorderColor;
                Color colorEnd = style.BorderColor2;
                Color colorLightStart = style.BorderColorLight;
                Color colorLightEnd = style.BorderColorLight2;
                BorderDrawInfo borderDrawInfo = new BorderDrawInfo(style.PaintLeftBorder, style.PaintRightBorder,
                                                                   style.PaintTopBorder, style.PaintBottomBorder,
                                                                   e.IsTwistedDoubleBorder);

                if (style.PaintLeftBorder)
                {
                    Color color = style.BorderColor;
                    if (!style.BorderLeftColor.IsEmpty)
                        color = style.BorderLeftColor;
                    Point[] p = new Point[2];

                    // Corner type square is default setting
                    p[0] = bounds.Location;
                    p[1].X = bounds.X;
                    p[1].Y = bounds.Bottom;

                    if (cornerTopLeft != eCornerType.Square)
                    {
                        p[0].Y += style.CornerDiameter;
                    }
                    if (cornerBottomLeft != eCornerType.Square)
                    {
                        p[1].Y -= style.CornerDiameter;
                    }

                    ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderLeft, style.BorderLeftWidth, color,
                                                       colorEnd, colorLightStart, colorLightEnd, eBorderSide.Left,
                                                       borderDrawInfo);

                    if (style.PaintTopBorder && cornerTopLeft != eCornerType.Square)
                    {
                        if (cornerTopLeft == eCornerType.Diagonal)
                        {
                            p[0].X = bounds.X;
                            p[0].Y = bounds.Y + style.CornerDiameter;
                            p[1].X = bounds.X + style.CornerDiameter;
                            p[1].Y = bounds.Y;
                            ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderLeft, style.BorderLeftWidth, color,
                                                               colorEnd, colorLightStart, colorLightEnd, eBorderSide.TopLeft,
                                                               borderDrawInfo);
                        }
                        else if (cornerTopLeft == eCornerType.Rounded)
                        {
                            ArcData a = GetCornerArc(bounds, style.CornerDiameter, eCornerArc.TopLeft);
                            // new ArcData(bounds.X,bounds.Y,style.CornerDiameter*2,style.CornerDiameter*2,180,90);
                            ElementStyleDisplay.DrawCornerArc(e.Graphics, a, style.BorderLeft, style.BorderLeftWidth, color);
                        }
                    }

                    if (style.PaintBottomBorder && cornerBottomLeft != eCornerType.Square)
                    {
                        if (cornerBottomLeft == eCornerType.Diagonal)
                        {
                            p[0].X = bounds.X;
                            p[0].Y = bounds.Bottom - style.CornerDiameter;
                            p[1].X = bounds.X + style.CornerDiameter;
                            p[1].Y = bounds.Bottom;
                            ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderLeft, style.BorderLeftWidth, color,
                                                               colorEnd, colorLightStart, colorLightEnd,
                                                               eBorderSide.BottomLeft, borderDrawInfo);
                        }
                        else if (cornerBottomLeft == eCornerType.Rounded)
                        {
                            ArcData a = GetCornerArc(bounds, style.CornerDiameter, eCornerArc.BottomLeft);
                            // new ArcData(bounds.X,bounds.Y,style.CornerDiameter*2,style.CornerDiameter*2,180,90);
                            ElementStyleDisplay.DrawCornerArc(e.Graphics, a, style.BorderLeft, style.BorderLeftWidth, color);
                        }
                    }
                }

                if (style.PaintTopBorder)
                {
                    Color color = style.BorderColor;
                    if (!style.BorderTopColor.IsEmpty)
                        color = style.BorderTopColor;
                    Point[] p = new Point[2];
                    // Default setting for Square corner type on both sides
                    p[0] = bounds.Location;
                    p[1].X = bounds.Right;
                    p[1].Y = bounds.Y;

                    if (cornerTopLeft != eCornerType.Square)
                    {
                        p[0].X += style.CornerDiameter;
                    }
                    if (cornerTopRight != eCornerType.Square)
                    {
                        p[1].X -= style.CornerDiameter;
                    }
                    ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderTop, style.BorderTopWidth, color, colorEnd,
                                                       colorLightStart, colorLightEnd, eBorderSide.Top, borderDrawInfo);
                }

                if (style.PaintBottomBorder)
                {
                    Color color = style.BorderColor;
                    if (!style.BorderBottomColor.IsEmpty)
                        color = style.BorderBottomColor;
                    Point[] p = new Point[2];
                    // Default for Square corner type on both sides
                    p[0].X = bounds.X;
                    p[0].Y = bounds.Bottom;
                    p[1].X = bounds.Right;
                    p[1].Y = bounds.Bottom;

                    if (cornerBottomLeft != eCornerType.Square)
                    {
                        p[0].X += style.CornerDiameter;
                    }
                    if (cornerBottomRight != eCornerType.Square)
                    {
                        p[1].X -= style.CornerDiameter;
                    }

                    ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderBottom, style.BorderBottomWidth, color,
                                                       colorEnd, colorLightStart, colorLightEnd, eBorderSide.Bottom,
                                                       borderDrawInfo);
                }

                if (style.PaintRightBorder)
                {
                    Color color = style.BorderColor;
                    if (!style.BorderRightColor.IsEmpty)
                        color = style.BorderRightColor;
                    Point[] p = new Point[2];
                    // Default for Square corner type on both sides
                    p[0].X = bounds.Right;
                    p[0].Y = bounds.Y;
                    p[1].X = bounds.Right;
                    p[1].Y = bounds.Bottom;

                    if (cornerTopRight != eCornerType.Square)
                    {
                        p[0].Y += style.CornerDiameter;
                    }
                    if (cornerBottomRight != eCornerType.Square)
                    {
                        p[1].Y -= style.CornerDiameter;
                    }

                    ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderRight, style.BorderRightWidth, color,
                                                       colorEnd, colorLightStart, colorLightEnd, eBorderSide.Right,
                                                       borderDrawInfo);

                    if (style.PaintTopBorder && cornerTopRight != eCornerType.Square)
                    {
                        if (cornerTopRight == eCornerType.Diagonal)
                        {
                            p[0].X = bounds.Right - style.CornerDiameter;
                            p[0].Y = bounds.Y;
                            p[1].X = bounds.Right;
                            p[1].Y = bounds.Y + style.CornerDiameter;
                            ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderLeft, style.BorderRightWidth,
                                                               color, colorEnd, colorLightStart, colorLightEnd,
                                                               eBorderSide.TopRight, borderDrawInfo);
                        }
                        else if (cornerTopRight == eCornerType.Rounded)
                        {
                            ArcData a = GetCornerArc(bounds, style.CornerDiameter, eCornerArc.TopRight);
                            // new ArcData(bounds.X,bounds.Y,style.CornerDiameter*2,style.CornerDiameter*2,180,90);
                            ElementStyleDisplay.DrawCornerArc(e.Graphics, a, style.BorderLeft, style.BorderLeftWidth, color);
                        }
                    }

                    if (style.PaintBottomBorder && cornerBottomRight != eCornerType.Square)
                    {
                        if (cornerBottomRight == eCornerType.Diagonal)
                        {
                            p[0].X = bounds.Right;
                            p[0].Y = bounds.Bottom - style.CornerDiameter;
                            p[1].X = bounds.Right - style.CornerDiameter;
                            p[1].Y = bounds.Bottom;
                            ElementStyleDisplay.DrawBorderLine(e.Graphics, p, style.BorderLeft, style.BorderRightWidth,
                                                               color, colorEnd, colorLightStart, colorLightEnd,
                                                               eBorderSide.BottomRight, borderDrawInfo);
                        }
                        else if (cornerBottomRight == eCornerType.Rounded)
                        {
                            ArcData a = GetCornerArc(bounds, style.CornerDiameter, eCornerArc.BottomRight);
                            // new ArcData(bounds.X,bounds.Y,style.CornerDiameter*2,style.CornerDiameter*2,180,90);
                            ElementStyleDisplay.DrawCornerArc(e.Graphics, a, style.BorderLeft, style.BorderLeftWidth, color);
                        }
                    }
                }
            }
            finally
            {
                if (disposeStyle) style.Dispose();
            }
        }