Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How we can make XP Style Buttons in VB.Net

Author: DevASP
Download Source Code : 428_XP Style Button.zip

In this article I will try to explain the way in which we can make our buttons like XP controls and import them in our project and useing as User Controls.

All we know that Xp Style Buttons Looks Better then the vb.net Controls. Usually client Emphasis That if their Application Will run on XP OS then their Application Should Look Like XP Themes. So In this Article I Tell U the Way In Which We can Make Our Own User Define Controls And Then Use them In Any Project Simply BY Including Them.

 

For Better Understanding Follow these Steps:

 

1-Add New Project and Select Windows Controls library As A new Project.

 

2-Then First Add this Block Of Code.

 

 

#Region "Event Handlers I Created"

    Event ContrastChanged As System.EventHandler       

    Event ClickRelease As System.EventHandler

    Event ClickStart As System.EventHandler

#End Region

Following three event are define for If the Contrast Changed then:

  • Event ContrastChanged As System.EventHandler

When button must be restored then:

  • Event ClickRelease As System.EventHandler         

When button is pressed:

 

  • Event ClickStart As System.EventHandler

 

3- Now Add This region. Simply what this is supose to do is that, if you retrieve an image with getImage it will send an Image with the apropiet contras of colors. Extreemly Useful if you have graystyle images and want to change it to blue or something else.

#Region "Contras of the Color"

 

Private CR As Integer = 100  'Contras of Red

Private CG As Integer = 100  'Contras of Green

Private CB As Integer = 100  'Contras of Blue

 

Property ContrastRed() As Integer

 Get

    ContrastRed = CR 'Retrieve Red persentage

 End Get

 

 Set(ByVal Value As Integer)

 

 If CR <> Value Then  'If the Value Changed

  CR = Value

  Dim e As EventArgs

  RaiseEvent ContrastChanged(Me, e)  'Raise Contras Changed

  Me.Invalidate() 'Redraw Everything

 End If

 

 End Set

End Property

 

Property ContrastGreen() As Integer

 

  Get

    ContrastGreen = CG  'Retrieve Green persentage

  End Get

 

  Set(ByVal Value As Integer)

 

    If CG <> Value Then  'If value Changed

      CG = Value

      Dim e As EventArgs

      RaiseEvent ContrastChanged(Me, e)   'Raise Contras Changed Event

      Me.Invalidate() 'Redraw Everything

   End If

  End Set

 

End Property

 

Property ContrastBlue() As Integer

 

  Get

   ContrastBlue = CB 'Retrieve Blue persentage

  End Get

 

  Set(ByVal Value As Integer)

  

      If CB <> Value Then  'If value Changed

         CB = Value

         Dim e As EventArgs

         RaiseEvent ContrastChanged(Me, e)   'Raise Contras Event

         Me.Invalidate() 'Redraw Everything

      End If

  End Set

 

End Property

 

Public Sub SetContras(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer)

 'Set the different contras and raise Contras Changed Event

 'This is useful if you don't want to change all the properties

 'every time

 

  CR = r

  CG = g

  CB = b

 

  Dim e As EventArgs

  RaiseEvent ContrastChanged(Me, e)

  Me.Invalidate()

 

End Sub

 

#End Region             

 

Public Sub SetContras(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer)

This Routine Set the different contras and raise Contras Changed Event.

This is useful if you don't want to change all the properties every time

 

 

4- Add  Region Below This Region In Fact Relad to Image of Controls And each Image Property.

 

Public Function GetImage(ByVal im As Bitmap) As Image This retrieve the image with the apropiete contrast

If the image is not the Apropiet size return nothing, theres no Image to return

 

Set the new value to the Temp Bitmap

temp.SetPixel(intX, intY, Color.FromArgb(tCol.A, r, g, b))

 

#Region "Image Methods"

  Public Function GetImage(ByVal im As Bitmap) As Image

        Try

            If Not (im.Width > 0 And im.Height > 0) Then

                Return Nothing    

            End If

        Catch                       'Could not mesure the bounds

 

            Return Nothing          'return nothing, theres no Image to return   

        End Try

 

        Dim intX, intY As Integer   'Positions of Pixels

 

        'Create a new Bitmap with the bounds

        Dim temp As New Bitmap(im.Size.Width, im.Size.Height)

 

        Dim tCol As Color           'Temp Color

 

        Dim r, g, b As Integer      'RGB values of pixel

 

        For intX = 0 To im.Size.Width - 1       'X position

            For intY = 0 To im.Size.Height - 1  'Y position

 

                tCol = im.GetPixel(intX, intY)  'Get Color for pixel

 

                r = tCol.R * CR \ 100           'Calculate r component

                If r > 255 Then                 'Can not be heigher than 255

                    r = 255

                End If

                g = tCol.G * CG \ 100           'Calculate g component

                If g > 255 Then                 'Can not be Heigher than 255

                    g = 255

                End If

                b = tCol.B * CB \ 100           'Calculate b component

                If b > 255 Then                 'Can not be Heigher than 255

                    b = 255

                End If

                temp.SetPixel(intX, intY, Color.FromArgb(tCol.A, r, g, b))

            Next

        Next

 

        Return temp     'Return the Temp Bitmap

 

    End Function

Public Function GetGrayImage(ByVal im As Bitmap) As Image

This is very useful Routine. What if your Control is Disabled What if it only dislay an image, How are you going to know if the control is Disabled.

 

Public Function GetGrayImage(ByVal im As Bitmap) As Image

 

Try

 'If image Excist

  If Not (im.Width > 0 And im.Height > 0) Then

           Return Nothing

  End If

 

  Dim intX, intY, av As Integer  'X and Y positions

 'Temp Image

  Dim temp As New Bitmap(im.Size.Width, im.Size.Height)

  Dim tCol As Color  'Temp Color

 

  For intX = 0 To im.Size.Width - 1       'X positions

   For intY = 0 To im.Size.Height - 1  'Y Positions

 

   tCol = im.GetPixel(intX, intY)  'Get Color from image

 

   'Find the Heighest Color and set av to it

   If tCol.G > tCol.R Then         'if Green > red

        av = tCol.G

   If tCol.G < tCol.B Then     'if Green < blue

        av = tCol.B

   End If

   Else                            'Red is > Green

        av = tCol.R

   If tCol.B > tCol.R Then     'if blue > Red

        av = tCol.B

   End If

   End If

 

  If tCol.A < 80 Then             'if Alpha is < 80

  'Set pixel with current alpha

   temp.SetPixel(intX, intY, tCol.FromArgb(tCol.A, av, av, av))

  Else

   'Set pixel with alpha = 80

   temp.SetPixel(intX, intY, tCol.FromArgb(80, av, av, av))

  End If

  Next

 Next

 

Return temp  'Return Temp Bitmap

 

  Catch

    Return Nothing  'Retun Nothing Bitmap does not exist

  End Try

End Function

 

#End Region

 

 

Now Built it .And Include It In Your Project As Add Existing Item.And Then Build Your project and Now The User Defined Controls is appear in Your User defined Controls.Pick Them As Vb.net Controls And Drop Them On Your Form.

Article Comments
It's very Best

Posted on 2/1/2007 5:34:41 AM by Qasim

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Show Icons in the Status Bar

Disclaimer - Privacy
© 2002-2017 DevASP.net