Click here to Skip to main content
15,890,882 members
Articles / Desktop Programming / Windows Forms

Making Office 2007 Buttons with Fading and Opacity

Rate me:
Please Sign up or sign in to vote.
4.75/5 (30 votes)
12 Mar 2007CPOL3 min read 61.2K   2.8K   105   10
An easy way to make new style buttons very compatible
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, but with panels too). 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 in making my own controls and delegates, however the base is here.

Background

If you try to make a backcolor transparent and many other tricks as CreateParams, you always have a problem with real opacity and with maintaining 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 by 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 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 as 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 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:
C#
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 that the button has only text, now let's add images.

Making the Office Style with Fading

You need to add the following 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 a 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 on your own.

Inside Off_Button

Let's see the Main methods:

C++
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; // That's 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 that in the beginning 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

The code is not a perfect class but it wasn't the objective of the article. This is 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Expediteapps
Spain Spain
I'm Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE ,Microsoft 3.5 MCTS
I live in Canary Islands ,developing customized solutions

Deeply involved in Xamarin Forms LOB (including Azure Cloud with offline support, custom controls, dependencies) projects, WP8.1 & W10 projects, WPF modern styled projects. Portable libraries like portablePDF, portableOneDrive, portableReports and portablePrinting (using Google Printing API).


Web and apps showcase at:
Expediteapps


Take a look to my blog
Blog

Comments and Discussions

 
GeneralMy vote of 5 Pin
RaviRanjanKr4-Dec-11 7:59
professionalRaviRanjanKr4-Dec-11 7:59 
GeneralGood job Pin
imresoft7-Dec-09 2:26
imresoft7-Dec-09 2:26 
GeneralNice! Pin
iccb101315-Jul-09 20:41
iccb101315-Jul-09 20:41 
GeneralInteresting Method - The Web Way Pin
stixoffire7-Jun-08 11:34
stixoffire7-Jun-08 11:34 
QuestionLicense? Pin
Julian-w17-May-08 8:51
Julian-w17-May-08 8:51 
GeneralNice article Pin
M.K.A. Monster22-Apr-07 21:02
M.K.A. Monster22-Apr-07 21:02 
QuestionNot working when converted in VB Pin
B-One21-Mar-07 22:43
B-One21-Mar-07 22:43 
AnswerRe: Not working when converted in VB Pin
B-One21-Mar-07 22:59
B-One21-Mar-07 22:59 
Hi,
Just wanted to let you know I fixed the problem and now it's working perfect.

this is the complete codebehind on ClassyButton Component.

Imports System
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Namespace ClassyButton
Partial Public Class ClassyButton
Inherits System.Windows.Forms.Button
Private timer1 As System.Windows.Forms.Timer = New System.Windows.Forms.Timer
Private _img_on As Image
Private _img_click As Image
Private _img_back As Image
Private _img As Image
Private _img_fad As Image
Private s_folder As String
Private s_filename As String
Private b_fad As Boolean = True
Private i_fad As Integer = 0
Private i_value As Integer = 255

Public Sub New()
Me.BackColor = Color.Transparent
Me.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
Me.FlatAppearance.BorderSize = 0
Me.TextAlign = ContentAlignment.BottomCenter
Me.ImageAlign = ContentAlignment.TopCenter
timer1.Interval = 10
AddHandler timer1.Tick, AddressOf timer1_Tick
End Sub

Public Property img_on() As Image
Get
Return _img_on
End Get
Set(ByVal value As Image)
_img_on = value
End Set
End Property

Public Property img_click() As Image
Get
Return _img_click
End Get
Set(ByVal value As Image)
_img_click = value
End Set
End Property

Public Property img_back() As Image
Get
Return _img_back
End Get
Set(ByVal value As Image)
_img_back = value
End Set
End Property

Public Property img() As Image
Get
Return _img
End Get
Set(ByVal value As Image)
_img = value
Me.Image = _img
End Set
End Property

Public Property folder() As String
Get
Return s_folder
End Get
Set(ByVal value As String)
If Not (value Is Nothing) Then
If Not (CType(value(value.Length - 1), Char) = "\"c) Then
s_folder = value + "\"
Else
s_folder = value
End If
End If
End Set
End Property

Public Property filename() As String
Get
Return s_filename
End Get
Set(ByVal value As String)
s_filename = value
If Not (s_folder Is Nothing) And Not (s_filename Is Nothing) Then
_img = Image.FromFile(s_folder + s_filename)
Me.Image = _img
End If
End Set
End Property

Public Sub PaintBackground()
Dim _img_temp As Object = New Object
If i_fad = 1 Then
_img_temp = _img_on.Clone
Else
If i_fad = 2 Then
_img_temp = _img_back.Clone
End If
End If
_img_fad = CType(_img_temp, Image)
Dim _grf As Graphics = Graphics.FromImage(_img_fad)
Dim brocha As SolidBrush = New SolidBrush(Color.FromArgb(i_value, 255, 255, 255))
_grf.FillRectangle(brocha, 0, 0, _img_fad.Width, _img_fad.Height)
Me.BackgroundImage = _img_fad
End Sub

Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
Select Case i_fad
Case 1
If i_value = 0 Then
i_value = 255
End If
If i_value > -1 Then
PaintBackground()
i_value -= 10
Else
i_value = 0
PaintBackground()
timer1.Stop()
End If
' break
Case 2
If i_value = 0 Then
i_value = 255
End If
If i_value > -1 Then
PaintBackground()
i_value -= 10
Else
i_value = 0
PaintBackground()
timer1.Stop()
End If
' break
End Select
End Sub

Protected Overloads Overrides Sub OnMouseEnter(ByVal e As EventArgs)
If b_fad Then
i_fad = 1
timer1.Start()
Else
Me.BackgroundImage = _img_on
End If
MyBase.OnMouseEnter(e)
End Sub

Protected Overloads Overrides Sub OnMouseLeave(ByVal e As EventArgs)
If b_fad Then
i_fad = 2
timer1.Start()
Else
Me.BackgroundImage = _img_back
End If
MyBase.OnMouseLeave(e)
End Sub

Protected Overloads Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.MouseEventArgs)
Me.BackgroundImage = _img_click
MyBase.OnMouseDown(mevent)
End Sub

Protected Overloads Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
Me.BackgroundImage = _img_on
MyBase.OnMouseUp(mevent)
End Sub
End Class
End Namespace
GeneralButton click events are not generating Pin
vivek_pawar12-Mar-07 19:56
vivek_pawar12-Mar-07 19:56 
AnswerRe: Button click events are not generating Pin
Juan Pablo G.C.13-Mar-07 2:01
Juan Pablo G.C.13-Mar-07 2:01 

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.