Click here to Skip to main content
12,448,219 members (47,340 online)
Click here to Skip to main content
Add your own
alternative version

Stats

3K views
1 bookmarked
Posted

Getting the Office 2010 button like Gradient

, 20 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralNicely done. Pin
ElectronicShocker20-Jun-11 17:59
memberElectronicShocker20-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 20 Jun 2011
Article Copyright 2011 by adolfobarallobre
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid