Vista style button in C #

Source: Internet
Author: User
This article from http://www.codeproject.com/KB/buttons/VistaButton.aspx A Vista style button in. NET 1.1

 

 

    • Download Demo project-70.65 KB
    • Download source files-86.85 KB

Contents
    • Introduction
    • Code
    • Using the code
    • Future Development
    • History
Introduction

I have a habit of using the standard Windows controls as little as possible. if I can write my own control to do it then I will. it's kinda like reinventing the wheel but adding spinners. anyways this is an article on a vista style button that I made. unlike the progress bar control I wrote, I didn't create it in Photoshop first. I just designed it in code, As a result it's not quite as nice looking as the actual Vista buttons. the actual button itself can be made very un-Vista-ish If You Want To by changing the colours but the default look for the button is the one in the top left.

Code

The actual concept for these buttons came from the Vista taskbar 'buttons' like this:

As you can see, my control vaguely resembles it if you squint and Tip your head to the side. I hear that closing your eyes can sometimes make the similarities clearer too. like the progress bar control I wrote, I organised my paint event into layers like this:

Collapse | Copy code
 
Private VoidVistabutton_paint (ObjectSender, painteventargs e) {e. graphics. smoothingmode = smoothingmode. antialias; E. graphics. interpolationmode = interpolationmode. highqualitybicubic; drawbackground (E. graphics); drawhighlight (E. graphics); drawimage (E. graphics); drawtext (E. graphics); drawglow (E. graphics); drawouterstroke (E. graphics); drawinnerstroke (E. graphics );}

Let's have a look at each of the Methods individually, from top to bottom startingDrawbackground:

Collapse | Copy code
 Private   Void Drawbackground (Graphics g ){ If ( This . Buttonstyle = style. Flat && This . Mbuttonstate = state. None ){ Return ;} Int Alpha = (mbuttonstate = state. Pressed )? 204 :127 ; Rectangle r = This . Clientrectangle; R. Width --; R. height --; Using (Graphicspath RR = roundrect (R, cornerradius, cornerradius )){ Using (Solidbrush sb = New Solidbrush ( This . Basecolor) {G. fillpath (SB, RR);} setclip (g ); If ( This . Backimage! = Null ) {G. drawimage ( This . Backimage,This . Clientrectangle);} G. resetclip (); (solidbrush sb = New Solidbrush (color. fromargb (alpha, This . Buttoncolor) {G. fillpath (SB, RR );}}}

The first line stops the background from drawing if the mouse is outside and the button state is set to flat. the second line sets the Alpha value of the background based on whether the button is being pressed or not. the next three lines just draw the base color using a rounded rectangle created usingRoundrectFunction. It accepts five arguments, the first being the bounds of the rectangle and the other four are the radius of the corners starting in the top left and going clockwise.SetclipFunction sets a clip so that when the background is drawn it doesn't go over the edges of the rounded rectangle. The last line just draws the main color over the top.

Collapse | Copy code
 Private   Void Drawhighlight (Graphics g ){ If ( This . Buttonstyle = style. Flat && This . Mbuttonstate = state. None ){ Return ;} Int Alpha = (mbuttonstate = state. Pressed )? 60 : 150 ; Rectangle rect = New Rectangle ( 0 , 0 , This . Width, This . Height/ 2 ); Using (Graphicspath r = roundrect (rect, cornerradius, cornerradius, 0 , 0 )){ Using (Lineargradientbrush lg = New Lineargradientbrush (R. getbounds (), color. fromargb (alpha, This . Highlightcolor), color. fromargb (alpha/ 3 , This . Highlightcolor), lineargradientmode. Vertical) {G. fillpath (LG, R );}}}

This bit draws the lighter gradient over the top half of the button, again using a different opacity depending on whether the button is currently pressed.

Collapse | Copy code
 Private   Void Drawimage (Graphics g ){ If ( This . Image = Null ){ Return ;} Rectangle r = New Rectangle ( 8 , 8 ,This . Imagesize. Width, This . Imagesize. Height ); Switch ( This . Imagealign ){ Case Contentalignment. topcenter: r = New Rectangle ( This . Width/ 2 - This . Imagesize. width/ 2 , 8 , This . Imagesize. Width,This . Imagesize. Height ); Break ; Case Contentalignment. topright: r = New Rectangle ( This . Width- 8 - This . Imagesize. Width, 8 , This . Imagesize. Width, This . Imagesize. Height ); Break ; Case Contentalignment. middleleft: r = New Rectangle ( 8 , This . Height/ 2 - This . Imagesize. Height/ 2 , This . Imagesize. Width, This . Imagesize. Height ); Break ; Case Contentalignment. middlecenter: r = New Rectangle (This . Width/ 2 - This . Imagesize. width/ 2 , This . Height/ 2 - This . Imagesize. Height/ 2 , This . Imagesize. Width, This . Imagesize. Height ); Break ; Case Contentalignment. middleright: r =New Rectangle ( This . Width- 8 - This . Imagesize. Width, This . Height/ 2 - This . Imagesize. Height/ 2 , This . Imagesize. Width, This . Imagesize. Height ); Break ; Case Contentalignment. bottomleft: r =New Rectangle ( 8 , This . Height- 8 - This . Imagesize. height, This . Imagesize. Width, This . Imagesize. Height ); Break ; Case Contentalignment. bottings enter: r = New Rectangle ( This . Width/ 2 -This . Imagesize. width/ 2 , This . Height- 8 - This . Imagesize. height, This . Imagesize. Width, This . Imagesize. Height ); Break ; Case Contentalignment. bottomright: r = New Rectangle ( This . Width- 8 - This . Imagesize. Width, This . Height- 8 - This . Imagesize. height, This . Imagesize. Width, This . Imagesize. Height ); Break ;} G. drawimage ( This . Image, R );}

This bit was tedious and edevil to write and I hope I never have to do it again. It usesImagealignProperty and draws the image in the right place.

Collapse | Copy code
 Private   Void Drawglow (Graphics g ){ If ( This . Mbuttonstate = state. Pressed ){ Return ;} Setclip (g ); Using (Graphicspath glow = New Graphicspath () {glow. addellipse (-5, This . Height/ 2 - 10 ,This . Width + 11 , This . Height + 11 ); Using (Pathgradientbrush GL = New Pathgradientbrush (glow) {Gl. centercolor = color. fromargb (mglowalpha, This . Glowcolor); Gl. surroundcolors = New Color [] {color. fromargb ( 0 , This . Glowcolor)}; G. fillpath (GL, Glow) ;}} G. resetclip ();}

This bit draws the glow at the bottom of the button on mouse over. it stops processing if the button is pressed down. the first line sets the clip to stop the glow going over the edge because it's a radial glow and only the top half shoshould be shown. it usesPathgradientbrushTo setCentercolorAndSurroundcolorTo make a radial gradient that starts off opaque in the center and reaches transparent as it approaches the edge.

Collapse | Copy code
Private VoidDrawouterstroke (Graphics g ){If(This. Buttonstyle = style. Flat &&This. Mbuttonstate = state. None ){Return;} Rectangle r =This. Clientrectangle; R. Width-=1; R. Height-=1;Using(Graphicspath RR = roundrect (R, cornerradius, cornerradius )){Using(Pen P =NewPen (This. Buttoncolor) {G. drawpath (p, RR );}}}

This procedure draws the outer line around the button.

Collapse | Copy code
  private   void  drawinnerstroke (Graphics g) { If  ( This . buttonstyle = style. flat &  This . mbuttonstate = state. none) { return ;} rectangle r =  This . clientrectangle; R. X ++; R. Y ++; R. width-=  3 ; R. height-=  3 ;  using  (graphicspath RR = roundrect (R, cornerradius )) { using  (pen p =  New  pen ( This . highlightcolor) {G. drawpath (p, RR) ;}}

This procedure draws the inner line just inside the outer line.

Using the code

Using this is really just as easy as using any other control. Just addVistabutton. CSFile into your solution and build it. Then select vistabutton from the 'my user controls' in the toolbox.

There are 12 properties with this control, none of them very complex in operation. They are as follows:

  • Basecolor-The color drawn at the back.ButtoncolorProperty never draws with 100% opacity so use this if you want your button to be opaque.
  • Backimage-This is the image that's drawn at the back of the control. It's drawn above the base color.
  • Buttoncolor-This is the main color of the button, the color that appears below the highlight.
  • Buttonstyle-This property sets whether the background of the control is drawn when the mouse is outside of its client area.
  • Cornerradius-The radius of the corners, shouldn't exceed half of the buttons height.
  • Glowcolor-The color of the glow on mouse over.
  • Highlightcolor-The color of the highlight on the top half of the button.
  • Image-This is the image that is drawn on the button, see the tick and cross in the example image at the top.
  • Imagealign-This is the alignment of the image on the button relative to the client area.
  • Imagesize-The size to draw the image.
  • Buttontext-The text to display on the button.
  • Textalign-The alignment of the text, sameImagealign.
Future Development

This bit is pretty much up to you guys. If anyone makes any valid suggestions, I 'd be more than happy to implement them.

History
    • Version 1.0 (23 June, 2007)-Initial Release
    • Version 1.1 (25 June, 2007)-added resource disposal after suggestion

License

This article, along with any associated source code and files, is licensed under the Code project open license (cpol)

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.