Click here to Skip to main content
12,359,751 members (64,989 online)
Click here to Skip to main content
Add your own
alternative version

Stats

15.2K views
11 bookmarked
Posted

Getting the Office 2010 button like Gradient

, 17 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Here is how you can achieve Gradients used in Office 2010 Buttons with a glow at the bottom.
I came across a requirement to achieve a Office 2010 like gradient, if you take a closer look at the Office 2010 buttons you can see the gradient is not actually linear, it is more like a light source as in photo editors. I have raised this in an MSDN article and received a clue on achieving the solution. Here is the generic solution - a reusable office button.

public class OfficeButton : Button
  {
    public OfficeButton() : base() { }
    public Color GradientGlowColor { get; set; }
    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);
    }
  }

Original thread is here[^]

Posting here, therefore it may help someone who is in need.

License

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

Share

About the Author

VallarasuS
Software Developer
India India
I code, learn, listen, and some day in a near future be a proud farmer.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 I like it, thanks. Pin
Talon010920-Jun-11 15:41
memberTalon010920-Jun-11 15:41 
BugMouse over event and setting image for the button Pin
salimdz200228-Jun-11 0:28
membersalimdz200228-Jun-11 0:28 
GeneralRe: Mouse over event and setting image for the button Pin
vallarasus29-Jun-11 6:02
membervallarasus29-Jun-11 6:02 

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
Web02 | 2.8.160621.1 | Last Updated 17 Jun 2011
Article Copyright 2011 by VallarasuS
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid