Click here to Skip to main content
15,880,543 members
Articles / Productivity Apps and Services / Microsoft Office
Alternative
Tip/Trick

Getting the Office 2010 button like Gradient

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Jun 2011CPOL 7.4K   1   1
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)


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNicely done. Pin
ElectronicShocker20-Jun-11 17:59
ElectronicShocker20-Jun-11 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.