5,702,067 members and growing! (15,973 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Making office2007 buttons with fading and opacity

By Juan Pablo G.C.

An easy way to make new style buttons very compatible.
C# 2.0, C#Windows, .NET, .NET 2.0, Win2K, WinXP, Win2003, Vista, WinForms, VS2005, Visual Studio, Dev

Posted: 12 Mar 2007
Updated: 12 Mar 2007
Views: 20,610
Bookmarked: 66 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 5.55 Rating: 4.42 out of 5
1 vote, 5.6%
1
1 vote, 5.6%
2
1 vote, 5.6%
3
2 votes, 11.1%
4
13 votes, 72.2%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
Screenshot - background.png Screenshot - form_example.png

Introduction

I've been looking for free source code of office 2007 style buttons to use it in my own applications and I've found only trials and complex buttons. Then I tried to design by myself in .net 2.0 (Windows 2000 compatible) and I found many problems with real opacity (not only form opacity, with panels too), but at last I found a great and easy way to make it. Here I explain how to make your own button with fading and opacity levels. I have to recognize that I'm not an expert making own controls and delegates, however the base is here.

Background

If you try to make backcolor transparent and many other tricks as CreateParams, you always have a problem with real opacity and mantain the correct aspect you want in your control, so the best way is to put a background image with a level of opacity. How to do that? The easiest way is using the Gimp. Install it and create a new file of 10x10 transparent (In Create a new image, select advanced options, 'fill with transparent' and if you make zoom you will see this:

Screenshot - image1.png

Now you can make your own color opacity effect selecting fill, choose and downside the icons there is the opacity level, change it at you want and click on the image. You will see something like the second image (2 in opacity level and R212; G228; B242 color). Save it as 'back.png' file, and let's go to the Visual Studio.

Using the off_button class (Ribbon_Style Namespace)

1.- Create a new Windows Application, for instance ribbon_buttons

2.- In the solution explorer, over the name of the project, right click, add-> existing item, and add off_button.cs

3.- Now add a typical button (System.Windows.Forms.Button) from the toolbox to the form and go to the code of Form1.Designer.cs (in the Solution Explorer).

4.- Change this:

this.button1 = new Ribbon_Style.of_boton2(); //Instead of new System.Windows.Forms.Button();
...
private Ribbon_Style.of_boton2 button1; //Instead of System.Windows.Forms.Button button1;

You will see no that the button has only text, now lets add images

Making the office style with fading

You need to add the next images to the project. For instance save in your HD and in properties choose img, img_back...

Screenshot - image3.png

b_click.png, b_on, and for instance b_search.png, background.png (the b_search.png has transparent background not green):

b_click.png: Screenshot - B_click.png b_on.png: Screenshot - B_on.png b_search.png: Screenshot - B_search.png background.png: Screenshot - background.png

(Mark beside left here to see back...) . Now you can try by your own.

Inside Off_Button

Let's see the Main Methods:

public void PaintBackground() 
    { object _img_temp = new object(); // We create the
        temp_image 
    if (i_fad == 1) 
    { _img_temp = _img_on.Clone(); } // That we clone to
        do not overwrite 
    else if (i_fad == 2) 
    { _img_temp = _img_back.Clone(); } // We use the i_value
        to choose the background opacity 
    _img_fad = (Image)_img_temp; 
    Graphics _grf = Graphics.FromImage(_img_fad); 
    SolidBrush brocha = new SolidBrush(Color.FromArgb(i_value, 255, 255, 255)); 
    _grf.FillRectangle(brocha, 0, 0, _img_fad.Width, _img_fad.Height); 
    this.BackgroundImage = _img_fad; 
}

private void timer1_Tick(object sender, EventArgs e) 
{ switch (i_fad) 
{ 
case 1: 
{ if (i_value == 0) 
{ i_value = 255; } 
if (i_value > -1) 
{ PaintBackground(); i_value -= 10; } 
else { i_value = 0; // Thats the only way to be able
    to play with the ticks
PaintBackground(); 
timer1.Stop(); } 
break; } 
case 2: 
{ if (i_value == 0) 
{ i_value = 255; } 
if (i_value > -1) 
{ PaintBackground(); 
i_value -= 10; } 
else { i_value = 0; 
PaintBackground(); 
timer1.Stop(); 
} 
break; } } }

Now you can find out that in the begining of the toolbox you have the new button to drag the times you want, or by code, of course.

Screenshot - image5.png

Points of Interest

Of course, the code is not a perfect class but it wasn't the objective of the article. That's my first control and I'm not an expert with custom controls and events yet. So when I learn a bit more I'll make them better and I will update the article.

History

March,06-2007 This is my first version of the control

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Juan Pablo G.C.


Mvp
I'm an Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE helping at University, and I'm getting ready for Microsoft MCTS.
I live in Canary Islands. At the moment Im developing a CRM software for my company. I'm really interested know people getting the MCTS.
I'm an SQL Server and .net2.0 intermediate expert.

Take a look to my blog Juan Pablo G.C.
Overrider
Occupation: Web Developer
Location: Spain Spain

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralInteresting Method - The Web Waymemberstixoffire12:34 7 Jun '08  
QuestionLicense?memberJulian-w9:51 17 May '08  
GeneralNice articlememberM.K.A. Monster22:02 22 Apr '07  
QuestionNot working when converted in VBmemberB-One23:43 21 Mar '07  
AnswerRe: Not working when converted in VBmemberB-One23:59 21 Mar '07  
GeneralButton click events are not generatingmembervivek_pawar20:56 12 Mar '07  
AnswerRe: Button click events are not generatingmemberJuan Pablo G.C.3:01 13 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Mar 2007
Editor:
Copyright 2007 by Juan Pablo G.C.
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project