Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / C#
Article

Making Office2007 panels with fading and opacity

Rate me:
Please Sign up or sign in to vote.
4.59/5 (29 votes)
19 Mar 2007CPOL3 min read 62.9K   2.4K   92   12
An easy way to add Office 2007 panels style

Screenshot - Article.png

Introduction

I have seen some OF07 panels with a good appearance that fail when they have controls inside and try to solve it making code in the parent form, that makes the project code too complicated. Another matter is opacity inside other panels with buttons, it works correctly and other thing I wanted in an Office 2007 style panel is fading from one base color to another. I have created an app that makes templates for a project. Let's see first how to put one in your project.

Using the code

In a new project add the class "off_panel.cs" (Solution Explorer->RightClick->Add Existing Item...). Then , you should drag a typical Forms.Panel to the form, then edit the code of the Form Designer (In the solution explorer view the subforms of the Form) changing the next:

C#
this.panel1 = new Off_Style.off_panel(); 
        //Instead of new System.Windows.Forms.Panel();
 ...
private Off_Style.off_panel panel1; //Instead of Panel panel1;

Now you will see a form like this (Sometimes the designer will not refresh well, you can reopen the project or delete the new lines and drag from the toolbox):

Screenshot - img_1.png

Changing the Base Color

I make a template app (Download the demo project) to choose easily the Base Color and the On Color, run it and select:

Screenshot - img_2.png

(The trackbars have their own limits to make the style consistently). After choosing the Base Color you want (Find that in the template app, I put a Form BackColor if you want to use it) and the OnColor. Return to your project and apply it in the Properties Window.

Custom Properties

I have added three interesting Properties:

  • Caption: It puts using the panel Font the text you want (I recommend don't use big fonts).
  • Speed: When the color distance between the base to the on color is big, if you want a faster transition you can increase this parameter if you want it faster (Take a Look in the demo project).
  • Opacity: As typical you can choose the opacity level.

Points of Interest

The design of the button

The fist time I cut the Office panel bitmap in 8 parts to make the panel as a bitmap, but I thought that if I put the cursor on I had to make another 8 parts, and what if I want to change the colors? At the end I decided to render it. You can see that the methods to draw the button are:

C#
public void DrawArc()
{
    X = X0; Y = Y0; i_Zero = 180; D++;
    path = new GraphicsPath();
    path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep); i_Zero += 90; X += XF;
    path.AddArc(X - D, Y + D, T, T, i_Zero, i_Sweep); i_Zero += 90; Y += YF;
    path.AddArc(X - D, Y - D, T, T, i_Zero, i_Sweep); i_Zero += 90; X -= XF;
    path.AddArc(X + D, Y - D, T, T, i_Zero, i_Sweep); i_Zero += 90; Y -= YF;
    path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep);
}

public void DrawArc2(int OF_Y, int SW_Y)
{
    X = X0; Y = Y0 + OF_Y; i_Zero = 180;
    path = new GraphicsPath();
    path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep); i_Zero += 90; X += XF;
    path.AddArc(X - D, Y + D, T, T, i_Zero, i_Sweep); i_Zero += 90; Y += SW_Y;
    path.AddArc(X - D, Y - D, T, T, i_Zero, i_Sweep); i_Zero += 90; X -= XF;
    path.AddArc(X + D, Y - D, T, T, i_Zero, i_Sweep); i_Zero += 90; Y -= SW_Y;
    path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep);
}

You can see two Parameters:

  • D: It controls the distance between borders (there are three borders).
  • T: It controls how big the radius is.

The speed of fading

To make the transition faster, I put a perColor speed that changes to 1 when the Color Component is near the Color on Component.

C#
if (System.Math.Abs(_BaseColorOn.R - R0) > i_factor)
{
    i_fR = i_factor;
}
else
{
    i_fR = 1;
}
if (System.Math.Abs(_BaseColorOn.G - G0) > i_factor)
{
    i_fG = i_factor;
}
else
{
    i_fG = 1;
}
if (System.Math.Abs(_BaseColorOn.B - B0) > i_factor)
{
    i_fB = i_factor;
}
else
{
    i_fB = 1;
}

Ignore the Panel OnMouseLeave when the Mouse is inside the panel but over a control.

To do that, when the Mouse Enters again:

C#
protected override void OnMouseEnter(EventArgs e)
{
    Point P_EX = Cursor.Position;
    P_EX = this.PointToClient(P_EX);
    if (P_EX.X > 0 | P_EX.X < this.Width |
            P_EX.Y > 0 | P_EX.Y < this.Height)
    {
        i_mode = 0; timer1.Start(); } base.OnMouseEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        Point P_EX = Cursor.Position;
    P_EX = this.PointToClient(P_EX);
    if (P_EX.X < 0 | P_EX.X >= this.Width | P_EX.Y < 0
                            | P_EX.Y >= this.Height)
    {
        i_mode = 1; timer1.Start(); } base.OnMouseLeave(e);
    }

Only if the mouse is outside the panel, it activates the timer. I hope you find it easy to use it.

History

  • Version 1.0: March 07.

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
reza_ali20200011-Feb-13 12:51
reza_ali20200011-Feb-13 12:51 
GeneralRe: My vote of 5 Pin
Juan Pablo G.C.13-Feb-13 2:30
Juan Pablo G.C.13-Feb-13 2:30 
GeneralCatcy Pin
VCSKicks2-Feb-09 10:49
VCSKicks2-Feb-09 10:49 
GeneralCoding Pin
Nedim Sabic25-Nov-08 0:35
Nedim Sabic25-Nov-08 0:35 
QuestionHow to set Opacity of Gradient panel Pin
amitnawale29-May-08 3:13
amitnawale29-May-08 3:13 
GeneralGood Pin
Priyank Bolia15-Jan-08 23:50
Priyank Bolia15-Jan-08 23:50 
GeneralGreat Pin
topcatalpha30-Mar-07 2:32
topcatalpha30-Mar-07 2:32 
GeneralAwesome! [modified] Pin
JoanComasFdz28-Mar-07 5:32
JoanComasFdz28-Mar-07 5:32 
GeneralAbout Voting Pin
Juan Pablo G.C.28-Mar-07 1:36
Juan Pablo G.C.28-Mar-07 1:36 
GeneralConverted it on VB Pin
B-One21-Mar-07 23:22
B-One21-Mar-07 23:22 
Hi,
I really like your controls so I've converted this one (as well as th button) in vb.net.
Here's the code.

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing.Drawing2D
Imports System.Drawing
Imports System.Windows.Forms
Namespace ClassyPanel
Class ClassyPanel
Inherits System.Windows.Forms.Panel
Public Sub New()
timer1.Interval = 1
AddHandler timer1.Tick, AddressOf timer1_Tick
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
End Sub
Private X0 As Integer
Private XF As Integer
Private Y0 As Integer
Private YF As Integer
Private T As Integer = 2
Private i_Zero As Integer = 180
Private i_Sweep As Integer = 90
Private X As Integer
Private Y As Integer
Private path As GraphicsPath
Private D As Integer = -1
Private R0 As Integer = 215
Private G0 As Integer = 227
Private B0 As Integer = 242
Private _BaseColor As Color = Color.FromArgb(215, 227, 242)
Private _BaseColorOn As Color = Color.FromArgb(215, 227, 242)
Private i_mode As Integer = 0
Private i_factor As Integer = 8
Private i_fR As Integer = 1
Private i_fG As Integer = 1
Private i_fB As Integer = 1
Private i_Op As Integer = 255
Private S_TXT As String = ""

Public Property BaseColor() As Color
Get
Return _BaseColor
End Get
Set(ByVal value As Color)
_BaseColor = value
R0 = _BaseColor.R
B0 = _BaseColor.B
G0 = _BaseColor.G
End Set
End Property

Public Property BaseColorOn() As Color
Get
Return _BaseColorOn
End Get
Set(ByVal value As Color)
_BaseColorOn = value
R0 = _BaseColor.R
B0 = _BaseColor.B
G0 = _BaseColor.G
End Set
End Property

Public Property Caption() As String
Get
Return S_TXT
End Get
Set(ByVal value As String)
S_TXT = value
Me.Refresh()
End Set
End Property

Public Property Speed() As Integer
Get
Return i_factor
End Get
Set(ByVal value As Integer)
i_factor = value
End Set
End Property

Public Property Opacity() As Integer
Get
Return i_Op
End Get
Set(ByVal value As Integer)
If value < 256 Or value > -1 Then
i_Op = value
End If
End Set
End Property

Protected Overloads Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
X0 = 0
XF = Me.Width + X0 - 3
Y0 = 0
YF = Me.Height + Y0 - 3
Dim P0 As Point = New Point(X0, Y0)
Dim PF As Point = New Point(X0, Y0 + YF - 15)
Dim b1 As Pen = New Pen(Color.FromArgb(i_Op, R0 - 18, G0 - 17, B0 - 19))
Dim b2 As Pen = New Pen(Color.FromArgb(i_Op, R0 - 39, G0 - 24, B0 - 3))
Dim b3 As Pen = New Pen(Color.FromArgb(i_Op, R0 + 14, G0 + 9, B0 + 3))
Dim p4 As Pen = New Pen(Color.FromArgb(i_Op, R0 - 8, G0 - 4, B0 - 2))
Dim b5 As Pen = New Pen(Color.FromArgb(i_Op, R0, G0, B0))
Dim b6 As Pen = New Pen(Color.FromArgb(i_Op, R0 - 16, G0 - 11, B0 - 5))
Dim b8 As Pen = New Pen(Color.FromArgb(i_Op, R0, G0 + 4, B0))
Dim b7 As Pen = New Pen(Color.FromArgb(i_Op, R0 - 22, G0 - 10, B0))
e.Graphics.PageUnit = GraphicsUnit.Pixel
Dim B4 As Brush = p4.Brush
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
X = X0
Y = Y0
i_Zero = 180
D = 0
DrawArc()
e.Graphics.DrawPath(b1, path)
DrawArc()
e.Graphics.DrawPath(b2, path)
DrawArc()
e.Graphics.DrawPath(b3, path)
DrawArc2(0, 20)
e.Graphics.FillPath(b5.Brush, path)
Dim brocha As LinearGradientBrush = New LinearGradientBrush(P0, PF, b6.Color, b8.Color)
DrawArc2(15, YF - 15)
e.Graphics.FillPath(brocha, path)
DrawArc2(YF - 18, 18)
Dim P_EX As Point = Cursor.Position
P_EX = Me.PointToClient(P_EX)
Dim ix As Integer = Me.Width / 2 - S_TXT.Length * CType(Me.Font.Size, Integer) / 2
Dim P_TXT As PointF = New PointF(ix, Me.Height - 18)
Dim pen As Pen = New Pen(Me.ForeColor)
e.Graphics.DrawString(S_TXT, Me.Font, pen.Brush, P_TXT)
MyBase.OnPaint(e)
End Sub

Protected Overloads Overrides Sub OnMouseEnter(ByVal e As EventArgs)
Dim P_EX As Point = Cursor.Position
P_EX = Me.PointToClient(P_EX)
If P_EX.X > 0 Or P_EX.X < Me.Width Or P_EX.Y > 0 Or P_EX.Y < Me.Height Then
i_mode = 0
timer1.Start()
End If
MyBase.OnMouseEnter(e)
End Sub

Protected Overloads Overrides Sub OnMouseLeave(ByVal e As EventArgs)
Dim P_EX As Point = Cursor.Position
P_EX = Me.PointToClient(P_EX)
If P_EX.X < 0 Or P_EX.X >= Me.Width Or P_EX.Y < 0 Or P_EX.Y >= Me.Height Then
i_mode = 1
timer1.Start()
End If
MyBase.OnMouseLeave(e)
End Sub

Public Sub DrawArc()
X = X0
Y = Y0
i_Zero = 180
System.Math.Min(System.Threading.Interlocked.Increment(D), D - 1)
path = New GraphicsPath
path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep)
i_Zero += 90
X += XF
path.AddArc(X - D, Y + D, T, T, i_Zero, i_Sweep)
i_Zero += 90
Y += YF
path.AddArc(X - D, Y - D, T, T, i_Zero, i_Sweep)
i_Zero += 90
X -= XF
path.AddArc(X + D, Y - D, T, T, i_Zero, i_Sweep)
i_Zero += 90
Y -= YF
path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep)
End Sub

Public Sub DrawArc2(ByVal OF_Y As Integer, ByVal SW_Y As Integer)
X = X0
Y = Y0 + OF_Y
i_Zero = 180
path = New GraphicsPath
path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep)
i_Zero += 90
X += XF
path.AddArc(X - D, Y + D, T, T, i_Zero, i_Sweep)
i_Zero += 90
Y += SW_Y
path.AddArc(X - D, Y - D, T, T, i_Zero, i_Sweep)
i_Zero += 90
X -= XF
path.AddArc(X + D, Y - D, T, T, i_Zero, i_Sweep)
i_Zero += 90
Y -= SW_Y
path.AddArc(X + D, Y + D, T, T, i_Zero, i_Sweep)
End Sub
Private timer1 As Timer = New Timer

Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
If i_mode = 0 Then
If System.Math.Abs(_BaseColorOn.R - R0) > i_factor Then
i_fR = i_factor
Else
i_fR = 1
End If
If System.Math.Abs(_BaseColorOn.G - G0) > i_factor Then
i_fG = i_factor
Else
i_fG = 1
End If
If System.Math.Abs(_BaseColorOn.B - B0) > i_factor Then
i_fB = i_factor
Else
i_fB = 1
End If
If _BaseColorOn.R < R0 Then
R0 -= i_fR
Else
If _BaseColorOn.R > R0 Then
R0 += i_fR
End If
End If
If _BaseColorOn.G < G0 Then
G0 -= i_fG
Else
If _BaseColorOn.G > G0 Then
G0 += i_fG
End If
End If
If _BaseColorOn.B < B0 Then
B0 -= i_fB
Else
If _BaseColorOn.B > B0 Then
B0 += i_fB
End If
End If
If _BaseColorOn = Color.FromArgb(R0, G0, B0) Then
timer1.Stop()
Else
Me.Refresh()
End If
End If
If i_mode = 1 Then
If System.Math.Abs(_BaseColor.R - R0) < i_factor Then
i_fR = 1
Else
i_fR = i_factor
End If
If System.Math.Abs(_BaseColor.G - G0) < i_factor Then
i_fG = 1
Else
i_fG = i_factor
End If
If System.Math.Abs(_BaseColor.B - B0) < i_factor Then
i_fB = 1
Else
i_fB = i_factor
End If
If _BaseColor.R < R0 Then
R0 -= i_fR
Else
If _BaseColor.R > R0 Then
R0 += i_fR
End If
End If
If _BaseColor.G < G0 Then
G0 -= i_fG
Else
If _BaseColor.G > G0 Then
G0 += i_fG
End If
End If
If _BaseColor.B < B0 Then
B0 -= i_fB
Else
If _BaseColor.B > B0 Then
B0 += i_fB
End If
End If
If _BaseColor = Color.FromArgb(R0, G0, B0) Then
timer1.Stop()
Else
Me.Refresh()
End If
End If
End Sub

End Class
End Namespace
GeneralRelated Query Pin
Vasudevan Deepak Kumar19-Mar-07 4:05
Vasudevan Deepak Kumar19-Mar-07 4:05 
GeneralRe: Related Query Pin
Tom Lawrance19-Mar-07 4:12
professionalTom Lawrance19-Mar-07 4:12 

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.