private void UpdateSettings()
 {
     lastCornerStyle      = CornerStyle;
     lastCurveFidelity    = curveFidelity;
     lastCornerLength     = cornerLength;
     lastStraightFidelity = straightFidelity;
     lastShape            = Shape;
 }
Example #2
0
      /// <summary>See <c>WpfExtensions</c> for details.</summary>
      public static void DrawRectangle(DrawingContext dc, Rect rect, Paint paint, CornerRadius corners, CornerStyle cornerStyle, BorderSide borderSide, bool snapToPixel) {
         // get brush and stroke resources
         if (paint == null) return;
         var fill = paint.Fill;
         var strokes = paint.GetStrokePens().ToArray();
         var hasFill = (fill != null);
         var hasStroke = (strokes.Length > 0 && borderSide != BorderSide.None);
         if (!hasFill && !hasStroke) return;

         // compute the drawing rectangle with offset for pixel snapping 
         // (assumptions: the box itself is pixel snapped, all strokes have same thickness, and the thickness is a whole number)
         var halfThickness = Math.Round(hasStroke ? strokes[0].Thickness : 0.0) / 2;
         if (snapToPixel && hasStroke) {
            rect = new Rect(rect.X + halfThickness, rect.Y + halfThickness, (rect.Width - 2 * halfThickness).Max(0), (rect.Height - 2 * halfThickness).Max(0));
         }

         // select drawing function based on the corners complexity 
         DrawingFunction fillFunc = null;
         if (corners.IsZero()) {
            fillFunc = _drawSimpleRectangle;
         } else if (corners.IsUniform()) {
            fillFunc = _drawRoundedRectangle;
         } else {
            fillFunc = _drawComplexRectangle;
         }

         // draw the border 
         if (hasStroke) {
            if (paint.IsA<QuadPaint>()) {
               rect = _drawSide(dc, rect, null, strokes[0], corners, cornerStyle, BorderSide.Left);
               rect = _drawSide(dc, rect, null, strokes[1], corners, cornerStyle, BorderSide.Top);
               rect = _drawSide(dc, rect, null, strokes[2], corners, cornerStyle, BorderSide.Right);
               rect = _drawSide(dc, rect, null, strokes[3], corners, cornerStyle, BorderSide.Bottom);
               corners = _inflateCorners(corners, strokes[3]);
            } else {
               var strokeFunc = (borderSide == BorderSide.All ? fillFunc : _drawSide);
               foreach (var stroke in strokes) {
                  rect = strokeFunc(dc, rect, null, stroke, corners, cornerStyle, borderSide);
                  corners = _inflateCorners(corners, stroke);
               }
            }
         }

         // draw the background
         if (hasFill) {
            if (snapToPixel) {
               rect = _deflateRect(rect, -halfThickness);
            }
            if (paint.FillMode == BrushMappingMode.Absolute) {
               fill = _mapAbsoluteFill(fill, rect);
            }
            fillFunc(dc, rect, fill, null, corners, cornerStyle, borderSide);
         }

      }
        /// <summary>
        /// Advs the rect.
        /// </summary>
        /// <param name="Rectangle">The rectangle.</param>
        /// <param name="CornerStyle">The corner style.</param>
        /// <param name="Curve">The curve.</param>
        /// <returns>GraphicsPath.</returns>
        public static GraphicsPath AdvRect(Rectangle Rectangle, CornerStyle CornerStyle, int Curve)
        {
            GraphicsPath functionReturnValue = default(GraphicsPath);

            functionReturnValue = new GraphicsPath();
            int ArcRectangleWidth = Curve * 2;

            if (CornerStyle.TopLeft)
            {
                functionReturnValue.AddArc(new Rectangle(Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -180, 90);
            }
            else
            {
                functionReturnValue.AddLine(Rectangle.X, Rectangle.Y, Rectangle.X + ArcRectangleWidth, Rectangle.Y);
            }

            if (CornerStyle.TopRight)
            {
                functionReturnValue.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -90, 90);
            }
            else
            {
                functionReturnValue.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y, Rectangle.X + Rectangle.Width, Rectangle.Y + ArcRectangleWidth);
            }

            if (CornerStyle.BottomRight)
            {
                functionReturnValue.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 0, 90);
            }
            else
            {
                functionReturnValue.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height, Rectangle.X + Rectangle.Width - ArcRectangleWidth, Rectangle.Y + Rectangle.Height);
            }

            if (CornerStyle.BottomLeft)
            {
                functionReturnValue.AddArc(new Rectangle(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 90, 90);
            }
            else
            {
                functionReturnValue.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height, Rectangle.X, Rectangle.Y + Rectangle.Height - ArcRectangleWidth);
            }

            functionReturnValue.CloseAllFigures();

            return(functionReturnValue);

            return(functionReturnValue);
        }
        public static GraphicsPath AdvRect(Rectangle Rectangle, CornerStyle CornerStyle, int Curve)
        {
            GraphicsPath tempAdvRect = null;

            tempAdvRect = new GraphicsPath();
            int ArcRectangleWidth = Curve * 2;

            if (CornerStyle.TopLeft)
            {
                tempAdvRect.AddArc(new Rectangle(Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -180F, 90F);
            }
            else
            {
                tempAdvRect.AddLine(Rectangle.X, Rectangle.Y, Rectangle.X + ArcRectangleWidth, Rectangle.Y);
            }

            if (CornerStyle.TopRight)
            {
                tempAdvRect.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -90F, 90F);
            }
            else
            {
                tempAdvRect.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y, Rectangle.X + Rectangle.Width, Rectangle.Y + ArcRectangleWidth);
            }

            if (CornerStyle.BottomRight)
            {
                tempAdvRect.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 0F, 90F);
            }
            else
            {
                tempAdvRect.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height, Rectangle.X + Rectangle.Width - ArcRectangleWidth, Rectangle.Y + Rectangle.Height);
            }

            if (CornerStyle.BottomLeft)
            {
                tempAdvRect.AddArc(new Rectangle(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 90F, 90F);
            }
            else
            {
                tempAdvRect.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height, Rectangle.X, Rectangle.Y + Rectangle.Height - ArcRectangleWidth);
            }

            tempAdvRect.CloseAllFigures();

            return(tempAdvRect);
        }
Example #5
0
        internal void CreatePreview()
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Render,
                                   new Action(() =>
            {
                try
                {
                    if (colorTag == null)
                    {
                        return;
                    }

                    UpdateLayout();
                    CornerStyle HighlightCorner = Lighthouse.Options.HighlightCorner;

                    t.Text = colorTag.Criteria;


                    r.Fill = new SolidColorBrush(Color.FromArgb(60,
                                                                colorTag.ColorSwatch.R,
                                                                colorTag.ColorSwatch.G,
                                                                colorTag.ColorSwatch.B));
                    r.Stroke = new SolidColorBrush(Color.FromArgb(100,
                                                                  colorTag.ColorSwatch.R,
                                                                  colorTag.ColorSwatch.G,
                                                                  colorTag.ColorSwatch.B));

                    double Vert       = colorTag.Blur != BlurIntensity.None ? 3 : 2;
                    const double Horz = 4;                        //colorTag.isFullLine ? 1920 : 4;

                    r.Width = colorTag.isFullLine ? previewGrid.ActualWidth - 8 : t.ActualWidth;

                    if (r.Width < t.ActualWidth)
                    {
                        r.Width = t.ActualWidth + 2;
                    }

                    t.Padding = new Thickness(Horz, Vert, Horz, Vert);

                    r.Effect = null;

                    colorTag.Blur = colorTag.Blur;
                    bool isLine   = colorTag.isUnderline;

                    if (isLine)
                    {
                        r.Height = 4;
                        r.Margin = new Thickness(0, t.ActualHeight - 2, 0, 0);
                    }
                    else
                    {
                        r.Height = t.ActualHeight;
                    }

                    if (colorTag.Blur != BlurIntensity.None)
                    {
                        r.Effect = new BlurEffect
                        {
                            KernelType    = KernelType.Gaussian,
                            RenderingBias = RenderingBias.Performance
                        };


                        switch (colorTag.Blur)
                        {
                        case BlurIntensity.Low:
                            ((SolidColorBrush)r.Fill).Color.ChangeAlpha(80);
                            ((BlurEffect)r.Effect).Radius = isLine ? 2 : 4.0;
                            break;

                        case BlurIntensity.Medium:
                            ((SolidColorBrush)r.Fill).Color.ChangeAlpha(120);
                            ((BlurEffect)r.Effect).Radius = isLine ? 4 : 7.0;
                            break;

                        case BlurIntensity.High:
                            ((SolidColorBrush)r.Fill).Color.ChangeAlpha(170);
                            ((BlurEffect)r.Effect).Radius = isLine ? 6 : 11.0;
                            break;

                        case BlurIntensity.Ultra:
                            ((SolidColorBrush)r.Fill).Color.ChangeAlpha(255);
                            ((BlurEffect)r.Effect).Radius = isLine ? 8 : 20.0;
                            break;
                        }

                        r.Stroke = null;
                    }

                    double cornerRadius;

                    try
                    {
                        switch (HighlightCorner)
                        {
                        case CornerStyle.Square:
                            cornerRadius = 0.0;
                            break;

                        case CornerStyle.RoundedCorner:
                            cornerRadius = 2.0;
                            break;

                        case CornerStyle.Soft:
                            cornerRadius = 6.0;
                            break;

                        default:
                            cornerRadius = 2.0;
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        cornerRadius = 2.0;
                    }

                    r.RadiusX = cornerRadius;
                    r.RadiusY = cornerRadius;
                }
                catch (Exception) { }
            }));
        }
        /// <summary>
        /// 创建路径。
        /// </summary>
        /// <param name="rect">用来创建路径的矩形。</param>
        /// <param name="cornerStyle">圆角弯曲样式。</param>
        /// <param name="roundStyle">圆角的样式。</param>
        /// <param name="radius">圆角半径。</param>
        /// <param name="correct">是否把矩形长宽减 1,以便画出边框。</param>
        /// <returns>创建的路径。</returns>
        public static GraphicsPath CreateGraphicsPath(Rectangle rect, CornerStyle cornerStyle, RoundStyle roundStyle, float radius, bool correct)
        {
            //-----------------校准-----------------
            if (correct)
            {
                rect.Width--;
                rect.Height--;
            }

            //-----------------定义返回值-----------------
            GraphicsPath path = new GraphicsPath();

            //-----------------特殊情况处理-----------------
            if (float.IsNaN(radius) || radius <= 0f)
            {
                path.AddRectangle(rect);
                return(path);
            }
            //-----------------临时变量定义-----------------
            float  diameter       = radius * 2;                                          //直径
            float  halfWidth      = rect.Width / 2f;                                     //宽度一半
            float  halfHeight     = rect.Height / 2f;                                    //高度一半
            PointF ptMiddleCenter = new PointF(rect.X + halfWidth, rect.Y + halfHeight); //中心点
            float  lrDegrees      = 0f;                                                  //半径大于半高,圆心角
            float  lrOffset       = 0f;                                                  //半径大于半高,圆弧到边距离

            if ((roundStyle & RoundStyle.All) != 0 && radius > halfHeight)
            {
                double lrRadian = Math.Acos((radius - halfHeight) / radius); //弧度
                lrDegrees = (float)MathEx.ToDegrees(lrRadian);               //角度
                lrOffset  = (float)(radius * Math.Sin(lrRadian));
            }
            float tbDegrees = 0f; //半径大于半宽,圆心角
            float tbOffset  = 0f; //半径大于办宽,圆弧到边距离

            if ((roundStyle & RoundStyle.All) != 0 && radius > halfWidth)
            {
                double tbRadian = Math.Acos((radius - halfWidth) / radius); //弧度
                tbDegrees = (float)MathEx.ToDegrees(tbRadian);              //角度
                tbOffset  = (float)(radius * Math.Sin(tbRadian));
            }
            //临时变量
            PointF ptBegin;
            PointF ptEnd;


            #region 左上
            if ((roundStyle & RoundStyle.TopLeft) == 0)//直角
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    ptBegin = new PointF(rect.X + radius, ptMiddleCenter.Y);
                    ptEnd   = new PointF(rect.X, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    ptBegin = new PointF(rect.X, ptMiddleCenter.Y);
                    ptEnd   = new PointF(rect.X + radius, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    ptBegin = new PointF(rect.X, rect.Y);
                    ptEnd   = new PointF(ptMiddleCenter.X, rect.Y + radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    ptBegin = new PointF(rect.X, rect.Y + radius);
                    ptEnd   = new PointF(ptMiddleCenter.X, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else
                {
                    ptBegin = ptEnd = new PointF(rect.X, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else//圆角
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.X - radius, rect.Y, diameter, diameter, 270 + lrDegrees, -lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X - radius, rect.Y, diameter, diameter, 0, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.X - (radius - lrOffset), rect.Y, diameter, diameter, 270 - lrDegrees, lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
                    }
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.X, rect.Y - radius, diameter, diameter, 180, -tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X, rect.Y - radius, diameter, diameter, 180, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.X, rect.Y - (radius - tbOffset), diameter, diameter, 180, tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
                    }
                }
                else
                {
                    path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
                }
            }
            #endregion


            #region 右上
            if ((roundStyle & RoundStyle.TopRight) == 0)
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    ptBegin = new PointF(rect.Right, rect.Y);
                    ptEnd   = new PointF(rect.Right - radius, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    ptBegin = new PointF(rect.Right - radius, rect.Y);
                    ptEnd   = new PointF(rect.Right, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Y + radius);
                    ptEnd   = new PointF(rect.Right, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Y);
                    ptEnd   = new PointF(rect.Right, rect.Y + radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else//矩形
                {
                    ptBegin = ptEnd = new PointF(rect.Right, rect.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.Right - radius, rect.Y, diameter, diameter, 270, -lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - radius, rect.Y, diameter, diameter, 270, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.Right - radius - lrOffset, rect.Y, diameter, diameter, 270, lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
                    }
                }
                else if ((cornerStyle & CornerStyle.TopIn) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.Right - diameter, rect.Y - radius, diameter, diameter, tbDegrees, -tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - diameter, rect.Y - radius, diameter, diameter, 90, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.TopOut) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.Right - diameter, rect.Y - (radius - tbOffset), diameter, diameter, 360 - tbDegrees, tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
                    }
                }
                else
                {
                    path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
                }
            }
            #endregion


            #region 右下
            if ((roundStyle & RoundStyle.BottomRight) == 0)
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    ptBegin = new PointF(rect.Right - radius, ptMiddleCenter.Y);
                    ptEnd   = new PointF(rect.Right, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    ptBegin = new PointF(rect.Right, ptMiddleCenter.Y);
                    ptEnd   = new PointF(rect.Right - radius, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    ptBegin = new PointF(rect.Right, rect.Bottom);
                    ptEnd   = new PointF(ptMiddleCenter.X, rect.Bottom - radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    ptBegin = new PointF(rect.Right, rect.Bottom - radius);
                    ptEnd   = new PointF(ptMiddleCenter.X, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else//矩形
                {
                    ptBegin = ptEnd = new PointF(rect.Right, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else
            {
                if ((cornerStyle & CornerStyle.RightIn) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.Right - radius, rect.Bottom - diameter, diameter, diameter, 90 + lrDegrees, -lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - radius, rect.Bottom - diameter, diameter, diameter, 180, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.RightOut) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.Right - radius - lrOffset, rect.Bottom - diameter, diameter, diameter, 90 - lrDegrees, lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
                    }
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.Right - diameter, rect.Bottom - radius, diameter, diameter, 0, -tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - diameter, rect.Bottom - radius, diameter, diameter, 0, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.Right - diameter, rect.Bottom - radius - tbOffset, diameter, diameter, 0, tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
                    }
                }
                else
                {
                    path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
                }
            }
            #endregion


            #region 左下
            if ((roundStyle & RoundStyle.BottomLeft) == 0)
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    ptBegin = new PointF(rect.X, rect.Bottom);
                    ptEnd   = new PointF(rect.X + radius, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    ptBegin = new PointF(rect.X + radius, rect.Bottom);
                    ptEnd   = new PointF(rect.X, ptMiddleCenter.Y);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Bottom - radius);
                    ptEnd   = new PointF(rect.X, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    ptBegin = new PointF(ptMiddleCenter.X, rect.Bottom);
                    ptEnd   = new PointF(rect.X, rect.Bottom - radius);
                    path.AddLine(ptBegin, ptEnd);
                }
                else
                {
                    ptBegin = ptEnd = new PointF(rect.X, rect.Bottom);
                    path.AddLine(ptBegin, ptEnd);
                }
            }
            else
            {
                if ((cornerStyle & CornerStyle.LeftIn) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.X - radius, rect.Bottom - diameter, diameter, diameter, 90, -lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X - radius, rect.Bottom - diameter, diameter, diameter, 90, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.LeftOut) != 0)
                {
                    if (radius > halfHeight)
                    {
                        path.AddArc(rect.X - (radius - lrOffset), rect.Bottom - diameter, diameter, diameter, 90, lrDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
                    }
                }
                else if ((cornerStyle & CornerStyle.BottomIn) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.X, rect.Bottom - radius, diameter, diameter, 180 + tbDegrees, -tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X, rect.Bottom - radius, diameter, diameter, 270, -90);
                    }
                }
                else if ((cornerStyle & CornerStyle.BottomOut) != 0)
                {
                    if (radius > halfWidth)
                    {
                        path.AddArc(rect.X, rect.Bottom - radius - tbOffset, diameter, diameter, 180 - tbDegrees, tbDegrees);
                    }
                    else
                    {
                        path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
                    }
                }
                else
                {
                    path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
                }
            }
            #endregion


            //闭合返回
            path.CloseFigure();
            return(path);
        }
Example #7
0
      private static Rect _drawSide(DrawingContext dc, Rect rect, Brush fill, Pen stroke, CornerRadius corners, CornerStyle cornerStyle, BorderSide borderSide) {
         var full = stroke.Thickness;
         var half = full / 2.0;

         var x0 = rect.Left;
         var x1 = rect.Right;
         var y0 = rect.Top;
         var y1 = rect.Bottom;
         
         var TL = corners.TopLeft;
         var TR = corners.TopRight;
         var BR = corners.BottomRight;
         var BL = corners.BottomLeft;

         var SQ = 0.0;
         switch (cornerStyle) {
            case CornerStyle.Diagonal:
               SQ = 1.0 / 2.0;
               break;
            case CornerStyle.Round:
               SQ = 1.0 - 1.0 / Math.Sqrt(2); 
               break;
            case CornerStyle.Concave:
               SQ = 1.0 / Math.Sqrt(2);
               break;
         }
            
         var hasCorners = false;

         var R = new double[4];
         var P = new Point[4];
         switch (borderSide) {
            case BorderSide.Left: 
               if (TL > 0) {
                  R[0] = TL;
                  P[0].X = x0 + SQ * (TL - full);
                  P[0].Y = y0 + SQ * TL;
                  hasCorners = true;
               }
               P[1].X = x0;
               P[1].Y = y0 + (TL - half);
               P[2].X = x0;
               P[2].Y = y1 - (BL - half);
               if (BL > 0) {
                  R[3] = BL;
                  P[3].X = x0 + SQ * BL;
                  P[3].Y = y1 - SQ * BL;
                  hasCorners = true;
               }
               rect.X += full;
               rect.Width = (rect.Width - full).Max(0);
               break;

            case BorderSide.Top:
               if (TR > 0) {
                  R[0] = TR;
                  P[0].X = x1 - SQ * TR;
                  P[0].Y = y0 + SQ * (TR - full);
                  hasCorners = true;
               }
               P[1].X = x1 - (TR - half);
               P[1].Y = y0;
               P[2].X = x0 + (TL - half);
               P[2].Y = y0;
               if (TL > 0) {
                  R[3] = TL;
                  P[3].X = x0 + SQ * (TL - full);
                  P[3].Y = y0 + SQ * TL;
                  hasCorners = true;
               }
               rect.Y += full;
               rect.Height = (rect.Height - full).Max(0);
               break;

            case BorderSide.Right:
               if (BR > 0) {
                  R[0] = BR;
                  P[0].X = x1 - SQ * (BR + full);
                  P[0].Y = y1 - SQ * BR;
                  hasCorners = true;
               }
               P[1].X = x1;
               P[1].Y = y1 - (BR - half);
               P[2].X = x1;
               P[2].Y = y0 + (TR - half);
               if (TR > 0) {
                  R[3] = TR;
                  P[3].X = x1 - SQ * TR;
                  P[3].Y = y0 + SQ * (TR - full);
                  hasCorners = true;
               }
               rect.Width = (rect.Width - full).Max(0);
               break;

            case BorderSide.Bottom:
               if (BL > 0) {
                  R[0] = BL;
                  P[0].X = x0 + SQ * (BL - full);
                  P[0].Y = y1 - SQ * (BL - full);
                  hasCorners = true;
               }
               P[1].X = x0 + (BL - half);
               P[1].Y = y1;
               P[2].X = x1 - (BR - half);
               P[2].Y = y1;
               if (BR > 0) {
                  R[3] = BR;
                  P[3].X = x1 - SQ * (BR - full);
                  P[3].Y = y1 - SQ * (BR - full);
                  hasCorners = true;
               }
               rect.Height = (rect.Height - full).Max(0);
               break;

            default:
               return rect;
         }

         if (!hasCorners) {
            dc.DrawLine(stroke, P[1], P[2]);
         } else {
            var sg = new StreamGeometry();
            var gc = sg.Open();
            switch (cornerStyle) {
               case CornerStyle.Concave:
               case CornerStyle.Round:
                  var sweep = (cornerStyle == CornerStyle.Round ? SweepDirection.Counterclockwise : SweepDirection.Clockwise);
                  if (R[0] == 0) {
                     gc.BeginFigure(P[1], false, false);
                  } else {
                     gc.BeginFigure(P[0], false, false);
                     gc.ArcTo(P[1], new Size(R[0], R[0]), 0, false, sweep, true, false);
                  }
                  gc.LineTo(P[2], true, false);
                  if (R[3] != 0) {
                     gc.ArcTo(P[3], new Size(R[3], R[3]), 0, false, sweep, true, false);
                  }
                  break;
               case CornerStyle.Diagonal:
                  if (R[0] == 0) {
                     gc.BeginFigure(P[1], false, false);
                  } else {
                     gc.BeginFigure(P[0], false, false);
                     gc.LineTo(P[1], true, false);
                  }
                  gc.LineTo(P[2], true, false);
                  if (R[3] != 0) {
                     gc.LineTo(P[3], true, false);
                  }
                  break;
            }
            gc.Close();
            sg.Freeze();
            dc.DrawGeometry(null, stroke, sg);
         }
         return rect;
      }
Example #8
0
 private static Rect _drawComplexRectangle(DrawingContext dc, Rect rect, Brush fill, Pen stroke, CornerRadius corners, CornerStyle cornerStyle, BorderSide borderSide) {
    var geometry = new StreamGeometry();
    using (var ctx = geometry.Open()) {
       AddRectangleFigure(ctx, rect, (fill != null), (stroke != null), corners, cornerStyle);
    }
    geometry.Freeze();
    dc.DrawGeometry(fill, stroke, geometry);
    return _deflateRect(rect, stroke);
 }
Example #9
0
 private static Rect _drawRoundedRectangle(DrawingContext dc, Rect rect, Brush fill, Pen stroke, CornerRadius corners, CornerStyle cornerStyle, BorderSide borderSide) {
    var r = corners.TopLeft;
    dc.DrawRoundedRectangle(fill, stroke, rect, r, r);
    return _deflateRect(rect, stroke);
 }
Example #10
0
 private static Rect _drawSimpleRectangle(DrawingContext dc, Rect rect, Brush fill, Pen stroke, CornerRadius corners, CornerStyle cornerStyle, BorderSide borderSide) {
    dc.DrawRectangle(fill, stroke, rect);
    return _deflateRect(rect, stroke);
 }
Example #11
0
      /// <summary>See <c>WpfExtensions</c> for details.</summary>
      public static void AddRectangleFigure(StreamGeometryContext ctx, Rect rect, bool fill, bool stroke, CornerRadius corners, CornerStyle cornerStyle) {
         double x0 = rect.Left, x1 = rect.Right, y0 = rect.Top, y1 = rect.Bottom;
         double TL = corners.TopLeft, TR = corners.TopRight, BL = corners.BottomLeft, BR = corners.BottomRight;
         var sweep = (cornerStyle == CornerStyle.Round ? SweepDirection.Counterclockwise : SweepDirection.Clockwise);
         var round = (cornerStyle != CornerStyle.Diagonal);

         // left side
         ctx.BeginFigure(new Point(x0, y0 + TL), fill, true);
         ctx.LineTo(new Point(x0, y1 - BL), stroke, false);

         // bottom-left corner
         if (BL > 0) {
            var bl = new Point(x0 + BL, y1);
            if (round) {
               ctx.ArcTo(bl, new Size(BL, BL), 0, false, sweep, stroke, false);
            } else {
               ctx.LineTo(bl, stroke, false);
            }
         }

         // bottom side
         ctx.LineTo(new Point(x1 - BR, y1), stroke, false);

         // bottom-right corner
         if (BR > 0) {
            var br = new Point(x1, y1 - BR);
            if (round) {
               ctx.ArcTo(br, new Size(BR, BR), 0, false, sweep, stroke, false);
            } else {
               ctx.LineTo(br, stroke, false);
            }
         }

         // right side
         ctx.LineTo(new Point(x1, y0 + TR), stroke, false);

         // top-right corner
         if (TR > 0) {
            var tr = new Point(x1 - TR, y0);
            if (round) {
               ctx.ArcTo(tr, new Size(TR, TR), 0, false, sweep, stroke, false);
            } else {
               ctx.LineTo(tr, stroke, false);
            }
         }

         // top side and top-left corner
         if (TL > 0) {
            ctx.LineTo(new Point(x0 + TL, y0), stroke, false);
            if (round) {
               ctx.ArcTo(new Point(x0, y0 + TL), new Size(TL, TL), 0, false, sweep, stroke, false);
            }
         }
      }