Click here to Skip to main content
Click here to Skip to main content

WindowsVistaRenderer: A New Button Generation

By , 23 Nov 2007
 
Screenshot - VistaRenderer.jpg

Introduction

This article demonstrates how to use the WindowsVistaRenderer and how it was created.

Background

Don't you miss the days when a button was drawn with a couple of lines to show a 3D effect? Drawing a button was as simple as drawing the light border and the shadow border. Those days are gone. Vista has arrived and our old apps may need to be renewed.

The first time you look at a button like the ones on Vista look and feel, you must feel dizzy. How to draw buttons like that? Well it turns out that it is not such a great deal. It took me a while to totally understand how these buttons are drawn, but I think the result is a good approach.

Using the Code

Thank God for the ToolStripRenderer technology.

To use this renderer on a ToolStrip you only need one line of code:

//
// Apply Windows Vista look and feel
//

toolStrip1.Renderer = new Renderers.WindowsVistaRenderer();

The Renderer initializes the necessary properties for the ToolStrip, except for the LayoutStyle property which I recommend to be the default HorizontalStackWithOverflow, otherwise the toolbar may look ugly in some cases.

The source solution contains a project named Renderers. Reference that DLL to your project or copy the source files to your project.

How to Draw a Button Like That

As I said before, the old-school buttons were drawn in a very easy way:

Screenshot - VistaRenderer1.gif Screenshot - VistaRenderer2.gif

The whole idea is to make the user think that he or she is really pushing a button, when clicked shadows were inverted, text pushed one pixel on x and one pixel on y, and the click effect was done.

The Vista buttons are way more complex. I've found different layers on the button drawing.

Borders

Three rounded rectangles are drown as a border, I call them the outer border, the border and the inner border.

Screenshot - VistaRenderer3.gif

Glossy Effect

A glossy effect is drawn on the north of the button. The green color represents an almost transparent color.

Screenshot - VistaRenderer4.gif

Glow

A radial gradient simulates a color glow on the south of the button. The green color represents an almost tansparent color.

Screenshot - VistaRenderer5.gif

Button Fill

Similar to the glossy effect, the inner border area is emphasized with a linear button fill from north to south.

Putting It All Together

Now, the order in which we draw these layers is critical. That order is:

  1. Outer border
  2. Button background color (if button is checked)
  3. Glossy effect
  4. Border
  5. Button fill glossy emphasis
  6. Inner border
  7. Glow
  8. Text and image of the button
Screenshot - VistaRenderer6.jpg

Some Other Details

To create the click effect, the color of the inner border and the button fill are changed. When clicked, the text is not pushed one pixel like in the old days.

When checked, the background color of the button changes. That color was extracted from the tabs on the MediaPlayer.

To make the toolbar a full-Vista-experience component, menus are drawn using the Windows Vista look and feel.

Credits

Thanks to Lukasz Swiatkowski for the methods on creating rounded rectangles and the bottom radial path.

History

  • 15 Oct 2007: Article creation
  • 25 Oct 2007: Subitem initialization by recursion solved (suggested by rvpilot)
  • 21 Nov 2007: Combobox and Textbox support. Better overflow chevron.
  • 23 Nov 2007: MenuStrip support.

License

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

About the Author

Jose Menendez Póo
Team Leader
Mexico Mexico
Member
Jose Manuel Menéndez Poó
 
- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is user interface usability.
 
Questions and stuff by twitter: @menendezpoo
 
Jose Runs Goplek in Mexico
www.goplek.com
 
Blog
menendezpoo.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to Change background colorsmembervijayksingh2 Apr '13 - 2:15 
Can we change background color from current one to blue (office like)
GeneralMy vote of 2memberprogrammerdon17 Oct '12 - 8:15 
"Out of Memory" exception when docking toolstrip left or right...
QuestionBinariesmemberVercas21 Jun '11 - 2:55 
I do not even care about this if you don't have compiled binaries to prove.
I am tired of extracting, converting and compiling everyone's projects.
AnswerRe: BinariesmemberJose Menendez Póo21 Jun '11 - 3:05 
Dude, chill. The article describes the technique well enough, you don't even have to download anything.
J

GeneralRe: BinariesmemberVercas21 Jun '11 - 3:21 
For someone in a hurry to get the control (like me), this is a murderer.
QuestionIs there a renderer with Windows 7 theme?memberstax7614 Aug '09 - 0:48 
Does anybody know if there is a renderer with Windows 7 theme and if NET 4.0 will include renderers with Vista and Windows 7 theme?
Generalnot properly recover default renderermemberUnruled Boy17 Jan '09 - 15:36 
when switch back to the default renderer(ToolStripProfessionalRenderer) by:
 
FooToolStrip.Renderer = new ToolStripProfessionalRenderer();
 
the height does not set back to default(28px), it's still very high(much larger than 28px), and font color does not set back to system default(black), it's still white.
 
Regards,
unruledboy_at_gmail_dot_com
http://www.xnlab.com

Generalmenu not properly rendered in windows 2003memberUnruled Boy17 Jan '09 - 14:43 
check out the snap shot
 
http://www.cnblogs.com/images/cnblogs_com/unruledboy/19874/o_VistaRendererMenu.jpg[^]
 
Regards,
unruledboy_at_gmail_dot_com
http://www.xnlab.com

Questionrenderer in vb.netmemberJoeFisher716 Nov '08 - 5:35 
I converted your renderer to vb.net and changed your menu(incorporating the same button colors and made a transparent rounded button when mouse is over item), added a vista form background, and created a vista button as well. Would you mind if I posted my code if I gave you credit for all of your code?
AnswerRe: renderer in vb.netmemberJose M. Menendez Poó6 Nov '08 - 6:36 
There's absolutely no problem as long as you mention my work.
 
Thanks for asking.
 
Remember to send me the link to the article! Looking forward to see it.
 
Jm
www.menendezpoo.com

General[Message Removed]memberKatekortez25 Oct '08 - 9:08 
Spam message removed
GeneralAbout new button generationmembermarclenoir20051 Sep '08 - 6:27 
hi Jose
 
i'd like to if this button is also available for vb 2005 or 2008.
 
2nd-) Let's say that i'm using vb 2005, how can i create this kind of button using code 2005?
 
Regards.
GeneralRe: About new button generationmemberJose M. Menendez Poó1 Sep '08 - 6:55 
Actually, the code provided with the article was made in vs 2005.
 
If you want the source code in VB, I strongly suggest using the Lutz Roeder's .Net Reflector to uncompile in VB Mode.
 
Thanks
 
Jm

QuestionLegal for commerical Application?memberOmnicoder31 May '08 - 8:58 
Is this legal in a commercial application?
QuestionThe control looks very good. Is there any way to implement the same look and feel in web pages of asp.netmemberMember 314494524 Feb '08 - 17:34 
The control looks very good. Is there any way to implement the same look and feel in web pages of asp.net Wink | ;)
AnswerRe: The control looks very good. Is there any way to implement the same look and feel in web pages of asp.netmemberThe Dogcow Farmer26 Jun '08 - 11:36 
Use images. No fading, but it works.
 
"Cookies are delicious delicacies." - Mozilla Foundation

QuestionLicencememberJulian-w30 Dec '07 - 23:26 
Hello,
first: your project is great!!!
 
My question:
I program an openSource control library, them all controls of Windows Vista contains and a library for playing media files. I would use your sourcecode with a little modification (only namespace and class-name) in these projects.
Can I do this?
 
My Site: julian-w.de
 
(Sorry for my bad English, I'm from Germany)

GeneralFighting the toolsmemberMount30 Dec '07 - 22:57 
Very good work, at least showing that winforms was underused when it came to graphics. The big problem is that
1) it takes a lot of code to do compared with WPF
2) it gets even harder when you try skin all the other controls like the datagridview, panels etc which are essential if you want a complete application. Look at http://www.componentfactory.com/[^] and see the amount of work it takes to create complete applicatios in winforms. This is a project by 1 guy
QuestionHow do I do this with just the command buttons?memberJonathanVQP19 Dec '07 - 6:55 
Can I do this with just a form that has command button instead of the toolstrip?
AnswerRe: How do I do this with just the command buttons?memberThe Dogcow Farmer27 Jun '08 - 5:10 
Set the ToolStrip with a button's Dock property to none and move it there.
 
"Cookies are delicious delicacies." - Mozilla Foundation

GeneralTranslation to vb.net [modified]membervirtual.aussie26 Nov '07 - 10:13 
I made a translation in vb.net. Can I upload it or send it on email to you do it?
 
-- modified at 5:56 Tuesday 27th November, 2007
Generaljust move to WPFmemberdimzon21 Nov '07 - 6:48 
actually if you need sexy and fine UI just move to WPF instead of WinForms
AnswerRe: just move to WPFmembermofle21 Nov '07 - 11:43 
Yes, but then i have to learn XAML.
GeneralRe: just move to WPFmemberdimzon22 Nov '07 - 5:28 

mofle wrote:
Yes, but then i have to learn XAML.

 
no, you can do it in code Wink | ;) but yes, XAML make this much more easy Wink | ;)
actualy WPF and XAML is our future so we MUST learn it...
GeneralRe: just move to WPFmembermofle22 Nov '07 - 5:52 
Yes, but most people still use Windows XP and only have .NET 2.0, so if we want to get to the biggest crowd, we still have to use .NET 2.0 and WinForms.
GeneralRe: just move to WPFmemberThe_Mega_ZZTer23 Nov '07 - 5:12 
.NET 3.5 installs just fine on XP.
 
But you're right, not many people are going to have it yet; at least until Microsoft pushes it out over Automatic Updates, then we can start developing applications that use it.
 
Actually, did they do it already? I can't tell since I just downloaded and installed it the day it came out.
GeneralRe: just move to WPFmembermofle23 Nov '07 - 5:18 
Yes, you're right.
 
Anyway, i haven't used XP in over a year, so i can't answer you on that one.
GeneralRe: just move to WPFmemberSimon Capewell23 Nov '07 - 5:44 
And what if you've been asked to spruce up and existing application? Porting the entire application would be a huge and risky job.
 

GeneralRe: just move to WPFmemberUser of Users Group23 Nov '07 - 8:55 
Not only that, it has been widely ack-ed by even MSFT themselves that WPF in fact makes your apps by default look very ugly. To make an awesome WPF app you really need designers involved.
 
And of course unless you are satisfied with an additional 5-fold (that is 500% for those that love diminishing returns) hit on CPU, and naturally an additional 30 MB of runtime bloat at the very least.
 
WPF has its purposes sure, and it has some great features (mainly enhanced experience from JS/DOM, markup history and plenty of 'nickage' from SVG too), but it is long shot away from being widespread, stable, efficient or a tech you want to bet your company on.
 
Go on then, adopt it so our customers can be happier Smile | :)
 

QuestionTextbox problemmembermofle20 Nov '07 - 8:56 
When i add a TextBox to the ToolStrip the text isn't visible.
 
I can't find a solution to turn the text to black.
 
Can somebody help me?
AnswerRe: Textbox problemmemberJose M. Menendez Poo21 Nov '07 - 3:50 
Hi!
 
The new version of the renderer now supports ToolStrip and ComboBox on the toolbar.
 
I submited the update of the article, but in the meanwhile you can download it from my site:
http://menendezpoo.com/VistaRenderer3.zip[^]
 
Thanks a lot!
 
Jm

GeneralRe: Textbox problemmembermofle21 Nov '07 - 4:10 
Great, but theres still some problems with the ComboBox.
 
It flickers when you hover over it and out and the style is different from the default style.
 
Try adding a ComboBox to the ToolStrip and one to the form and you'll see the difference.
 

Theres a small bug in the round corners, take a look at the image.
 

 

ComboBox
http://www.divshare.com/download/2838797-7c2
 
Corner
http://www.divshare.com/download/2838798-092
QuestionRe: Textbox problemmembermofle22 Nov '07 - 8:30 
??
QuestionRe: Textbox problemmembermofle28 Nov '07 - 23:51 
Anybody ??
GeneralToolStripComboBox IssuememberMichal Brylka13 Nov '07 - 5:12 
Hi,
 
After applying your renderer, ToolStripComboBox ceased to render it's items properly. Items are only redrawn when hovered. I'm using Windows Vista with default theme.

 
Michał Bryłka
 
Theory is when you know something, but it doesn't work.
Practice is when something works, but you don't know why.
Programmers combine theory and practice: Nothing works and they don't know why.

AnswerRe: ToolStripComboBox IssuememberJose M. Menendez Poo21 Nov '07 - 3:49 
Hi!
 
The new version of the renderer now supports ToolStrip and ComboBox on the toolbar.
 
I submited the update of the article, but in the meanwhile you can download it from my site:
http://menendezpoo.com/VistaRenderer3.zip[^]
 
Thanks a lot!
 
Jm

GeneralMenuStrip ProblemmemberSk8tz27 Oct '07 - 0:55 
Hi there..
 
Where setting the renderer for a menustrip, the menu does not draw properly, using XP SP2 cleartype. Can you please look into that, otherwise a great control( you got my 5).
 


 
Sk8tZ

AnswerRe: MenuStrip ProblemmemberStaffanW21 Nov '07 - 3:40 
When setting the WindowsVistaRenderer to the static ToolstripManager.Renderer property, all ToolStrips are rendered, including StatusStrip and MenuStrip. This is great, but the MenuStrip is not rendered correctly, so to fix the MenuStrip rendering, I had to change the following code. Maybe there's a more correct solution, but this works.
 
A separator line should not be drawn:
 
    private void DrawVistaMenuBackground(Graphics g, Rectangle r, bool highlighted, bool mainMenu) {
      //g.Clear(ColorTable.MenuBackground);

      int margin = 2;
 
      #region IconSeparator
 
      if (!mainMenu) {
        int left = 22;
 
        using (Pen p = new Pen(ColorTable.MenuDark)) {
          g.DrawLine(p,
                      new Point(r.Left + left, r.Top),
                      new Point(r.Left + left, r.Height - margin));
        }
 
        using (Pen p = new Pen(ColorTable.MenuLight)) {
          g.DrawLine(p,
                      new Point(r.Left + left + 1, r.Top),
                      new Point(r.Left + left + 1, r.Height - margin));
        }
      }
      #endregion
 
      if (highlighted) {
        #region Draw Rectangle
 
        using (GraphicsPath path = GraphicsTools.CreateRoundRectangle(
            new Rectangle(r.X + margin, r.Y + margin, r.Width - margin * 2, r.Height - margin * 2), 3)) {
 
          using (Brush b = new LinearGradientBrush(
              new Point(0, 2), new Point(0, r.Height - 2),
              ColorTable.MenuHighlightNorth,
              ColorTable.MenuHighlightSouth)) {
            g.FillPath(b, path);
          }
 
          using (Pen p = new Pen(ColorTable.MenuHighlight)) {
            g.DrawPath(p, path);
          }
        }
 
        #endregion
      }
 
    }
 
When calling the above method:
 
      DrawVistaMenuBackground(e.Graphics,
          new Rectangle(Point.Empty, e.Item.Size),
          e.Item.Selected, (e.Item.Owner is MenuStrip));
 
Also, the text of them should not be drawn as the items:
 
    protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) {
 
      e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
 
      if (!(e.Item.Owner is MenuStrip) && e.Item is ToolStripMenuItem) {
        Rectangle r = new Rectangle(e.TextRectangle.Location, new Size(e.TextRectangle.Width, 24));
        e.TextRectangle = r;
        e.TextColor = ColorTable.MenuText;
      }
 
      base.OnRenderItemText(e);
    }

 
/WeBe
GeneralRe: MenuStrip ProblemmemberSk8tz21 Nov '07 - 3:57 
Thanks alot..
 
Will Test..
 
Sk8tZ

GeneralRe: MenuStrip ProblemmemberSk8tz22 Nov '07 - 20:33 
Cool, it works..and I say thanks
 
But the way it renders, is not exactly correct, but I'm sure it can get there.
 
1) When mouse is over the menu, it should render like the the tool strip buttons.
2) When clicked, the menu should be rendered with the menu items like a tab on a tab control.
 
A good example would be to look, at the office 2007 renderer in codeproject I would assume. Would be nice to hear Mr Jose M. Menendez suggestions on these changes.
 
Thanks again.
 

 
Sk8tZ

AnswerRe: MenuStrip ProblemmemberJose M. Menendez Poo23 Nov '07 - 4:14 
Hey guys, sorry for the delay.
 
I've fixed the renderer so menus on the MenuStrip are rendered the same way that
ToolStrip buttons.
 
While the article is updated by the CodeProject people, you can download it from
my site:
 
http://www.menendezpoo.com/VistaRenderer4.zip[^]
 
Thanks a lot for the feedback.
 

 
Jm

GeneralRe: MenuStrip ProblemmemberSk8tz23 Nov '07 - 4:32 
Thanks will test
 
Sk8tZ

GeneralRe: MenuStrip ProblemmemberSk8tz26 Nov '07 - 19:34 
Hi JM
 
The mouse over the menu strip works great, but when you click on the menustrip, the style it renders is not exactly right. I'm not to sure how fix this.
 
I can point you to the office 2007 render if you can assist, to see the behavior of the click on the menustrip.
 
http://www.codeproject.com/cs/menu/Office2007Renderer.asp
 
Thanks for your changes so far.
 


 
Sk8tZ

GeneralToolstripSplitButton Problemmembergrouperb126 Oct '07 - 4:20 
There seems to be a problem rendering the text on the ToolStripSplitButton.
 
When you get the mouse over the toolstripsplitbutton the text size increases and when you resize the toolstrip the text decreases back to its original state!
 
It is a bit weard!!! Please take a look into it.
 
All in all a great piece of code!
GeneralRe: ToolstripSplitButton ProblemmemberJose M. Menendez Poo26 Oct '07 - 4:28 
I've noticed that problem too, when running on XP, it seems to be the ClearType text rendering, I will run more tests and solve it, soon.
 
Thanks!
 
Jm

GeneralRe: ToolstripSplitButton Problem Possible Solutionmembergrouperb126 Oct '07 - 21:33 
I found a way of solving this issue:
 
//The problem is fixed by the commented lines.
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
 
//if (e.Item is ToolStripButton)
//{
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
//}
 
if (e.Item is ToolStripMenuItem)
{
Rectangle r = new Rectangle(e.TextRectangle.Location, new Size(e.TextRectangle.Width, 24));
e.TextRectangle = r;
e.TextColor = ColorTable.MenuText;
}
 
base.OnRenderItemText(e);
}
GeneralMuy buen trabajo Jose!memberPakT25 Oct '07 - 22:14 
Felicidades por este trabajo!
 
Un saludo

GeneralRe: Muy buen trabajo Jose!membersk8er_boy28711 Dec '08 - 1:28 
Nice article! Can I have a taco? Big Grin | :-D
GeneralPretty coolmemberBen Daniel25 Oct '07 - 18:10 
Though this line made me LOL:
"Don't you miss the days when a button was drawn with a couple of lines to show a 3D effect"
 
Thanks,
Ben Smile | :)

GeneralSuggestionmemberMCAST7625 Oct '07 - 7:55 
Hi, first of all let me say that I love this work.
 
I have a suggestion that I like to put it on your consideration.
 
Can you add more color palettes by default to this control, for example, Blue and Silver.
 
I know that you have WindowsVistaColorTable, but you have the experience, and I know you can make this quickly. Smile | :)
 
Thanks anyway!
 
Best regards,
 


 

Mike Cast

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 23 Nov 2007
Article Copyright 2007 by Jose Menendez Póo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid