C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Xp Style Button


Posted Date: 26 Aug 2006    Resource Type: Articles    Category: .NET Framework
Author: sailavanyaMember Level: Silver    
Rating: Points: 10



open new project add a usercontrol to the project specifiy the width and height of the usercontrol,place a picture box which fits neatly to the left in usercontrol and write this code. Build it and use the dll in your vb.net windows applications.


Imports System.Drawing.Drawing2D
System.ComponentModel.DefaultProperty("Text")> _
Public Class buttonXp
Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

'setting initial values for the control

xStartColor = Color.FromArgb(221, 234, 254)
xEndColor = Color.FromArgb(129, 169, 226)
brdrcolor = Color.FromArgb(129, 169, 226)
fontcolor = Color.FromKnownColor(KnownColor.ControlDark)

End Sub

'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Picture As System.Windows.Forms.PictureBox
Private Sub InitializeComponent()
Me.Picture = New System.Windows.Forms.PictureBox
Me.SuspendLayout()
'
'Picture
'
Me.Picture.BackColor = System.Drawing.SystemColors.Control
Me.Picture.Location = New System.Drawing.Point(5, 4)
Me.Picture.Name = "Picture"
Me.Picture.Size = New System.Drawing.Size(18, 18)
Me.Picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.Picture.TabIndex = 0
Me.Picture.TabStop = False
Me.Picture.Visible = False
'
'buttonXp
'
Me.AccessibleRole = System.Windows.Forms.AccessibleRole.HotkeyField
Me.Controls.Add(Me.Picture)
Me.ForeColor = System.Drawing.Color.Brown
Me.Name = "buttonXp"
Me.Size = New System.Drawing.Size(75, 23)
Me.ResumeLayout(False)

End Sub

#End Region

#Region "Style"

'This is the different Styles the button Can have
Enum ButtonStyle
Flat = 0
Popup = 1
Normal = 2
End Enum

Private btnStyle As ButtonStyle = ButtonStyle.Normal 'Holds the Style State

Property Style() As ButtonStyle
Get
Style = btnStyle 'Retrieve Style
End Get
Set(ByVal Value As ButtonStyle)
btnStyle = Value 'Set the style
Me.Invalidate() 'Refresh the view
End Set
End Property

#End Region

#Region "Image Position"

Enum ImagePosition
[none] = 0
Left = 1
Right = 2
End Enum
Private imAlign As ImagePosition
Private imgRect As New RectangleF(0, 0, 0, 0)
Private txtRect As New RectangleF(0, 0, Me.Width - 6, Me.Height - 6)
Private imgSize As New Size(38, 38)

Property ImageAlign() As ImagePosition
Get
ImageAlign = imAlign
End Get
Set(ByVal Value As ImagePosition)
imAlign = Value
SetImagePosition()
Me.Invalidate()
End Set
End Property

Property ImageSize() As Size
Get
ImageSize = imgSize
End Get
Set(ByVal Value As Size)
imgSize = Value
SetImagePosition()
Me.Invalidate()
End Set
End Property
Private Sub SetImagePosition()
Select Case ImageAlign
Case ImagePosition.none
txtRect = New RectangleF(3, 3, Me.Width - 6, Me.Height - 6)
imgRect = New RectangleF(0, 0, Me.Width, Me.Height)
Picture.Location = New Point(imgRect.Width, imgRect.Height)
Case ImagePosition.Right
Picture.Visible = True
imgRect = New RectangleF(2, 3, Me.Width - 6, Me.Height - 5)
txtRect = New RectangleF(10, 3, (Me.Width - imgSize.Width) - 6, Me.Height - 6)
Picture.Location = New Point((imgRect.Width * 72) / 100, (imgRect.Height * 20) / 100)
Case ImagePosition.Left
Picture.Visible = True
imgRect = New RectangleF(0, 0, Me.Width - 6, Me.Height - 5)
txtRect = New RectangleF(imgSize.Width - 10, 3, (Me.Width - imgSize.Width), Me.Height - 6)
Picture.Location = New Point((imgRect.Width * 6) / 100, (imgRect.Height * 20) / 100)
End Select
Picture.Size = New Size((imgRect.Width * 24) / 100, (imgRect.Height * 78) / 100)
End Sub
#End Region

#Region "DialogResult"

Private dlgResult As DialogResult = DialogResult.None

Property DialogResult() As DialogResult
Get
DialogResult = dlgResult
End Get
Set(ByVal Value As DialogResult)
dlgResult = Value
End Set
End Property

#End Region

#Region "Image and text"


Private imgOriginal As Image
Dim imgimage As Image
Private strText As String
'sets text drawn on user control,browsable=true(can modify text at design time),System.ComponentModel.DesignerSerializationVisibility.Visible=code generator produes code for the object
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible), _
System.ComponentModel.DefaultValue("")> _
Public Shadows Property Text() As String
Get
Text = strText
End Get
Set(ByVal Value As String)
strText = Value
Me.Invalidate()
End Set
End Property
'gets the image as default type specified
_
Public Property Image() As Image
Get
Image = imgOriginal
End Get
Set(ByVal Value As Image)
imgOriginal = Value
Me.Invalidate()
End Set
End Property

#End Region

#Region "ColorProperties"

Private xStartColor As Color
Private xEndColor As Color
Private Movercolor As Color
Private brdrcolor As Color
Private fntcolor As Color
'sets color from top positon of control till middle
Public Property StartColor() As Color
Get
Return xStartColor
End Get
Set(ByVal Value As Color)
xStartColor = Value
ReCreateBrushes()
Me.Refresh()
End Set
End Property
'sets color when mouse over
Public Property MouseOvercolor() As Color
Get
Return Movercolor
End Get
Set(ByVal Value As Color)
If Value.Equals(Color.Empty) Then Movercolor = Color.DarkGray
Movercolor = Value
Invalidate()
End Set

End Property
'sets color from bottom of the control till middle
Public Property EndColor() As Color
Get
Return xEndColor
End Get
Set(ByVal Value As Color)
xEndColor = Value
ReCreateBrushes()
Me.Refresh()
End Set
End Property
'sets border color property
Public Property bordercolor() As Color
Get
Return brdrcolor
End Get
Set(ByVal Value As Color)
brdrcolor = Value
ReCreateBrushes()
Me.Refresh()
End Set
End Property
' sets font color property
Public Property fontcolor() As Color
Get
Return fntcolor
End Get
Set(ByVal Value As Color)
fntcolor = Value
ReCreateBrushes()
Me.Refresh()
End Set
End Property

'to initialise brushes and called in color properties to paint with the initialised brushes
Private Sub ReCreateBrushes()
Dim c As Color
Dim ar As New Rectangle(0, 0, Me.Width, Me.Height)
Dim ButtonBrush As New LinearGradientBrush(ar, xStartColor, xEndColor, LinearGradientMode.ForwardDiagonal)

c = Color.FromArgb(255, 255 - xStartColor.R, 255 - xStartColor.G, 255 - xStartColor.B)
If xStartColor.Name = xEndColor.Name Then
Dim FocusBrush As New SolidBrush(c)
Else
Dim FocusBrush As New LinearGradientBrush(ar, xEndColor, xStartColor, LinearGradientMode.ForwardDiagonal)
End If
End Sub

#End Region

#Region "OnPaint"
Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)
Dim g As Graphics = pe.Graphics
Dim intX() As Integer = {2, 0, 0, 2, Me.Width - 3, Me.Width - 1, Me.Width - 1, Me.Width - 3, 2}
Dim intY() As Integer = {Me.Height - 1, Me.Height - 3, 2, 0, 0, 2, Me.Height - 3, Me.Height - 1, Me.Height - 1}
Dim intLoop As Integer
Dim gpath As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
Dim tpath As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
Dim bpath As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)

For intLoop = 0 To 6
gpath.AddLine(intX(intLoop), intY(intLoop), intX(intLoop + 1), intY(intLoop + 1))
Next
For intLoop = 0 To 3
tpath.AddLine(intX(intLoop) + 1, intY(intLoop) + 1, intX(intLoop + 1) + 1, intY(intLoop + 1) + 1)
Next
For intLoop = 4 To 7
bpath.AddLine(intX(intLoop) - 1, intY(intLoop) - 1, intX(intLoop + 1) - 1, intY(intLoop + 1) - 1)
Next

gpath.CloseFigure()
Try
If Image Is Nothing Then
Picture.Visible = False
Else
Picture.Visible = True
Picture.Image = imgOriginal
End If
Catch
End Try
Dim tbrush As SolidBrush
Dim bbrush As SolidBrush
bbrush = New SolidBrush(Color.FromArgb(150, 0, 0, 0))
tbrush = New SolidBrush(Color.FromArgb(150, 255, 255, 255))
Dim borderbrush As New SolidBrush(Me.bordercolor)
Dim grect As New Rectangle(0, 0, Me.Width, Me.Height)
Dim gbrush As Drawing2D.LinearGradientBrush
gbrush = New Drawing2D.LinearGradientBrush(grect, xStartColor, xEndColor, LinearGradientMode.Vertical)
g.FillPath(gbrush, gpath)

If Style = ButtonStyle.Flat Then
g.DrawPath(New Pen(MyBase.FindForm.ForeColor), gpath)
End If

If Style = ButtonStyle.Popup Then
bbrush = New SolidBrush(Color.FromKnownColor(KnownColor.ControlDark))
g.DrawPath(New Pen(bbrush, 2), tpath)
g.DrawPath(New Pen(bbrush, 2), gpath)
End If

If Style = ButtonStyle.Normal Then
g.DrawPath(New Pen(borderbrush, 2), gpath)
g.DrawPath(New Pen(borderbrush, 2), gpath)
End If

If over Then
gbrush = New Drawing2D.LinearGradientBrush(grect, Color.Silver, Movercolor, LinearGradientMode.Vertical)
g.FillPath(gbrush, gpath)
Try
If Image Is Nothing Then
Picture.Visible = False
Else
Picture.Visible = True
Picture.Image = imgOriginal
End If
Catch

End Try
If Style = ButtonStyle.Normal Then
g.DrawPath(New Pen(tbrush, 2), gpath)
g.DrawPath(New Pen(bbrush, 2), gpath)
End If
If Style = ButtonStyle.Flat Then
g.DrawPath(New Pen(Me.FindForm.ForeColor), gpath)
End If
If Style = ButtonStyle.Popup Then
bbrush = New SolidBrush(Color.FromKnownColor(KnownColor.ControlDark))
g.DrawPath(New Pen(bbrush, 2), tpath)
g.DrawPath(New Pen(bbrush, 2), gpath)
End If
End If

If clicked Then
over = False
gbrush = New Drawing2D.LinearGradientBrush(grect, xStartColor, xEndColor, LinearGradientMode.Vertical)
g.FillPath(gbrush, gpath)
Try
If Image Is Nothing Then
Picture.Visible = False
Else
Picture.Visible = True
Picture.Image = imgOriginal
End If
Catch
End Try
If Style = ButtonStyle.Normal Then
If clicked = True Then
g.DrawPath(New Pen(tbrush, 2), gpath)
g.DrawPath(New Pen(tbrush), gpath)
ElseIf clicked = False Then
g.DrawPath(New Pen(bbrush, 2), gpath)
g.DrawPath(New Pen(bbrush, 2), gpath)
End If
End If
If Style = ButtonStyle.Flat Then
If clicked = True Then
ElseIf clicked = False Then
g.DrawPath(New Pen(Me.FindForm.ForeColor), gpath)
End If
End If
If Style = ButtonStyle.Popup Then
If clicked = True Then
g.DrawPath(New Pen(tbrush), gpath)
g.DrawPath(New Pen(tbrush), gpath)
ElseIf clicked = False Then
bbrush = New SolidBrush(Color.FromKnownColor(KnownColor.ControlDark))
g.DrawPath(New Pen(bbrush, 2), tpath)
g.DrawPath(New Pen(bbrush, 2), gpath)
End If
End If
End If

If Not Text Is Nothing Then
Dim myformat As New StringFormat(StringFormatFlags.FitBlackBox)
myformat.Alignment = StringAlignment.Center
myformat.LineAlignment = StringAlignment.Center
If Style = ButtonStyle.Flat Then
g.DrawString(Text, Me.Font, New SolidBrush(MyBase.FindForm.ForeColor), txtRect, myformat)
Else
g.DrawString(Text, Me.Font, New SolidBrush(Me.fontcolor), txtRect, myformat)
End If
End If
End Sub
#End Region

#Region "Other"
Private over As Boolean = False
Private Sub buttonXp_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
SetImagePosition()
Me.Invalidate()
End Sub

Private Sub buttonXp_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.EnabledChanged
Me.Invalidate()
End Sub

Private Sub buttonXp_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseEnter
over = True
Me.Invalidate()
End Sub

Private Sub buttonXp_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
over = False
Me.Invalidate()
End Sub

Private clicked As Boolean = False
Private Sub buttonXp_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
clicked = True
Me.Invalidate()
End Sub

Private Sub buttonXp_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
clicked = False
Me.Invalidate()
End Sub

Private Sub Picture_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture.MouseDown
clicked = True
Me.Invalidate()
End Sub

Private Sub Picture_MouseUp1(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Picture.MouseUp
clicked = False
Me.Invalidate()
End Sub
#End Region

End Class




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Concept of interface in C # with a simple example.
Previous Resource: Boxing & Unboxing internals
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use