コード例 #1
0
        public static Brush CreateBrush(Rectangle bounds, BackgroundColorBlendCollection colorBlend, int gradientAngle, eGradientType gradientType)
        {
            eBackgroundColorBlendType blendType = colorBlend.GetBlendType();
            if (blendType == eBackgroundColorBlendType.Invalid)
                return null;

            if (blendType == eBackgroundColorBlendType.SolidColor)
            {
                return new SolidBrush(colorBlend[0].Color);
            }
            else if (blendType == eBackgroundColorBlendType.Relative)
            {
                try
                {
                    if (gradientType == eGradientType.Linear)
                    {
                        bounds.Inflate(1, 1);
                        LinearGradientBrush brush =
                        DisplayHelp.CreateLinearGradientBrush(bounds, colorBlend[0].Color, colorBlend[colorBlend.Count - 1].Color,
                                                          gradientAngle);
                        brush.InterpolationColors = colorBlend.GetColorBlend();
                        return brush;
                    }
                    else if (gradientType == eGradientType.Radial)
                    {
                        int d = (int)Math.Sqrt(bounds.Width * bounds.Width + bounds.Height * bounds.Height) + 4;
                        GraphicsPath fillPath = new GraphicsPath();
                        fillPath.AddEllipse(bounds.X - (d - bounds.Width) / 2, bounds.Y - (d - bounds.Height) / 2, d, d);
                        PathGradientBrush brush = new PathGradientBrush(fillPath);
                        brush.CenterColor = colorBlend[0].Color;
                        brush.SurroundColors = new Color[] { colorBlend[colorBlend.Count - 1].Color };
                        brush.InterpolationColors = colorBlend.GetColorBlend();
                        return brush;
                    }
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                BackgroundColorBlendCollection bc = colorBlend;
                for (int i = 0; i < bc.Count; i += 2)
                {
                    BackgroundColorBlend b1 = bc[i];
                    BackgroundColorBlend b2 = null;
                    if (i < bc.Count)
                        b2 = bc[i + 1];
                    if (b1 != null && b2 != null)
                    {
                        Rectangle rb = new Rectangle(bounds.X, bounds.Y + (int)b1.Position, bounds.Width,
                            (b2.Position == 1f ? bounds.Height : (int)b2.Position) - (int)b1.Position);
                        return DisplayHelp.CreateLinearGradientBrush(rb, b1.Color, b2.Color, gradientAngle);
                    }
                }
            }

            return null;
        }