Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / Office

Getting the Office 2010 button like Gradient

0.00/5 (No votes)
20 Jun 2011CPOL 7.7K  
Here is code for an alternate:public class OfficeButton : Button{ public OfficeButton() : base() { } private Color mGradientGlowColorColor; // the Color field public Color GradientGlowColor // the Color property { get { return...

Here is code for an alternate:


C#
public class OfficeButton : Button
{
    public OfficeButton() : base() { }
    private Color mGradientGlowColorColor;  // the Color field
    public Color GradientGlowColor    // the Color property
    {
        get
        {
            return mGradientGlowColorColor;
        }
        set
        {
            mGradientGlowColorColor = value;
        }
    }
    protected override void OnPaint(PaintEventArgs pevent)
    {
        Bitmap bmp = new Bitmap(this.Width, this.Height);
        Rectangle rect = this.ClientRectangle;
        Graphics g = Graphics.FromImage(bmp);
        g.Clear(GradientGlowColor);
        g.InterpolationMode = InterpolationMode.NearestNeighbor;
        using (SolidBrush brush = new SolidBrush(this.BackColor))
        {
            g.FillRectangle(brush, rect);
        }
        rect.Inflate(-1, -1);
        rect.Width -= 1; rect.Height -= 1;
        using (Pen pen = new Pen(GradientGlowColor))
        {
            g.DrawRectangle(pen, rect);
        }
        RectangleF[] CornerDots = new RectangleF[]
    {
      new RectangleF(rect.Left + 1, rect.Top + 1, 1, 1),
      new RectangleF(rect.Right - 1, rect.Top + 1, 1, 1),
      new RectangleF(rect.Left + 1, rect.Bottom - 1, 1, 1),
      new RectangleF(rect.Right - 1, rect.Bottom - 1, 1, 1)
    };
        using (SolidBrush brush = new SolidBrush(this.GradientGlowColor))
        {
            g.FillRectangles(brush, CornerDots);
        }
        using (GraphicsPath ellipse = new GraphicsPath())
        {
            float offset = this.Width / 5;
            ellipse.AddEllipse(new RectangleF(-offset / 2, 0, this.Width + offset, 30));
            ellipse.Transform(new Matrix(1, 0, 0, 1, 0, (this.Height - 15)));
            using (PathGradientBrush p = new PathGradientBrush(ellipse))
            {
                p.CenterColor = this.GradientGlowColor;
                p.SurroundColors = new Color[] { Color.FromArgb(12, this.GradientGlowColor) };
                p.FocusScales = new PointF(0.6F, 0.1F);
                g.FillPath(p, ellipse);
            }
        }
        using (Pen pen = new Pen(this.BackColor))
        {
            rect = this.ClientRectangle;
            rect.Height -= 1; rect.Width -= 1;
            g.DrawRectangle(pen, rect);
        }
        pevent.Graphics.DrawImage(bmp, Point.Empty);
        StringFormat strFormat = new StringFormat();
        switch (this.TextAlign)
        {
            case ContentAlignment.BottomCenter:
                strFormat.Alignment= StringAlignment.Center;
                strFormat.LineAlignment = StringAlignment.Far;
                break;
            case ContentAlignment.BottomLeft:
                strFormat.Alignment = StringAlignment.Near;
                strFormat.LineAlignment = StringAlignment.Far;
                break;
            case ContentAlignment.BottomRight:
                strFormat.Alignment = StringAlignment.Far;
                strFormat.LineAlignment = StringAlignment.Far;
                break;
            case ContentAlignment.MiddleCenter:
                strFormat.Alignment = StringAlignment.Center;
                strFormat.LineAlignment = StringAlignment.Center;
                break;
            case ContentAlignment.MiddleLeft:
                strFormat.Alignment = StringAlignment.Near;
                strFormat.LineAlignment = StringAlignment.Center;
                break;
            case ContentAlignment.MiddleRight:
                strFormat.Alignment = StringAlignment.Far;
                strFormat.LineAlignment = StringAlignment.Center;
                break;
            case ContentAlignment.TopCenter:
                strFormat.Alignment = StringAlignment.Center;
                strFormat.LineAlignment = StringAlignment.Near;
                break;
            case ContentAlignment.TopLeft:
                strFormat.Alignment = StringAlignment.Near;
                strFormat.LineAlignment = StringAlignment.Near;
                break;
            case ContentAlignment.TopRight:
                strFormat.Alignment = StringAlignment.Far;
                strFormat.LineAlignment = StringAlignment.Near;
                break;
            default:
                break;
        }
        
        pevent.Graphics.DrawString(this.Text, this.Font, 
          new SolidBrush(this.ForeColor), this.ClientRectangle, strFormat);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)