Vista Style Label

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I took a few minutes to make a vista-lookalike label, Vista itself is basically crap but the color scheme can look nice if implemented properly.
VB.NET:
Expand Collapse Copy
Option Explicit On
Option Strict On
Option Infer Off

Public Class VistaLabel
    Inherits System.Windows.Forms.Label

    Private _VistaStyleBoolean As Boolean = True
    Private _gradientBegin As Color = Color.FromArgb(4I, 80I, 130I)
    Private _gradientEnd As Color = Color.FromArgb(28I, 120I, 133I)

    Public Sub New()
        MyBase.AutoSize = False
        Me.AutoSize = False
        Me.Size = New Size(110I, 35I)
        Me.MinimumSize = New Size(89I, 25I)
        Me.Font = New Font("Microsoft Sans Serif", 10.0!, FontStyle.Regular)
        Me.ForeColor = Color.WhiteSmoke
        Me.TextAlign = ContentAlignment.MiddleLeft
    End Sub

    Public Property VistaStyle() As Boolean
        Get
            Return _VistaStyleBoolean
        End Get
        Set(ByVal value As Boolean)
            If _VistaStyleBoolean <> value Then
                _VistaStyleBoolean = value
                Me.Refresh()
            End If
        End Set
    End Property

    Private Sub VistaLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Me._VistaStyleBoolean = True Then
            Dim pt As Point
            Dim sz As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font, Me.Width)
            Using b As New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0I, 0I, Me.Width, Me.Height), _gradientBegin, _gradientEnd, Drawing2D.LinearGradientMode.Horizontal)
                e.Graphics.FillRectangle(b, New Rectangle(0I, 0I, Me.Width, Me.Height))
            End Using
            Select Case Me.TextAlign
                Case ContentAlignment.TopLeft
                    pt = New Point(0I, 1I)
                Case ContentAlignment.TopCenter
                    pt = New Point(CInt((Me.Width / 2I) - (sz.Width / 2I)), 1I)
                Case ContentAlignment.TopRight
                    pt = New Point(Me.Width - CInt(sz.Width), 1I)
                Case ContentAlignment.MiddleLeft
                    pt = New Point(0I, CInt((Me.Height / 2I) - (sz.Height / 2I)))
                Case ContentAlignment.MiddleCenter
                    pt = New Point(CInt((Me.Width / 2I) - (sz.Width / 2I)), CInt((Me.Height / 2I) - (sz.Height / 2I)))
                Case ContentAlignment.MiddleRight
                    pt = New Point(Me.Width - CInt(sz.Width), CInt((Me.Height / 2I) - (sz.Height / 2I)))
                Case ContentAlignment.BottomLeft
                    pt = New Point(0I, Me.Height - CInt(sz.Height))
                Case ContentAlignment.BottomCenter
                    pt = New Point(CInt((Me.Width / 2I) - (sz.Width / 2I)), Me.Height - CInt(sz.Height))
                Case ContentAlignment.BottomRight
                    pt = New Point(Me.Width - CInt(sz.Width), Me.Height - CInt(sz.Height))
            End Select
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), pt.X, pt.Y)
        End If
    End Sub
End Class
Usage: Drop it on the form, use it.
To have it revert to a standard label set the VistaLabel's VistaStyle property to false.
 
I have some feedback, for the TextAlign you can use StringFormat. You also forgot to dispose the SolidBrush. A sample code:
VB.NET:
Expand Collapse Copy
If Me._VistaStyleBoolean = True Then
    Using b As New System.Drawing.Drawing2D.LinearGradientBrush(Me.ClientRectangle, _gradientBegin, _gradientEnd, Drawing2D.LinearGradientMode.Horizontal)
        e.Graphics.FillRectangle(b, Me.ClientRectangle)
    End Using

    Dim format As New StringFormat
    Select Case Me.TextAlign
        Case ContentAlignment.BottomCenter, ContentAlignment.BottomLeft, ContentAlignment.BottomRight
            format.LineAlignment = StringAlignment.Far
        Case ContentAlignment.MiddleCenter, ContentAlignment.MiddleLeft, ContentAlignment.MiddleRight
            format.LineAlignment = StringAlignment.Center
        Case ContentAlignment.TopCenter, ContentAlignment.TopLeft, ContentAlignment.TopRight
            format.LineAlignment = StringAlignment.Near
    End Select
    Select Case Me.TextAlign
        Case ContentAlignment.BottomCenter, ContentAlignment.MiddleCenter, ContentAlignment.TopCenter
            format.Alignment = StringAlignment.Center
        Case ContentAlignment.BottomLeft, ContentAlignment.MiddleLeft, ContentAlignment.TopLeft
            format.Alignment = StringAlignment.Near
        Case ContentAlignment.BottomRight, ContentAlignment.MiddleRight, ContentAlignment.TopRight
            format.Alignment = StringAlignment.Far
    End Select

    Using forebrush As New SolidBrush(Me.ForeColor)
        e.Graphics.DrawString(Me.Text, Me.Font, forebrush, Me.ClientRectangle, format)
    End Using
    format.Dispose()
End If
I also wonder why you denote all integer values with the I sign? I think it makes the code more difficult to read, it also doesn't serve any purpose that I'm aware of. Are you thinking about 64bit migration perhaps? Integer data type is 32bit also on 64bit platform, only IntPtr changes according to 32/64 platform. (Migrating 32-bit Managed Code to 64-bit)
Vista itself is basically crap
It's the lesser crap of all operating systems, though ;)
 
VB.NET:
Expand Collapse Copy
Option Explicit On
Option Strict On
Option Infer Off

Public Class VistaLabel
    Inherits System.Windows.Forms.Label

#Region " Variables "
    Private _VistaStyleBoolean As Boolean = True
    Private _gradientBegin As Color = Color.FromArgb(4I, 80I, 130I)
    Private _gradientEnd As Color = Color.FromArgb(28I, 120I, 133I)
#End Region
#Region " Constructors "
    Public Sub New()
        MyBase.AutoSize = False
        Me.AutoSize = False
        Me.Size = New Size(110I, 35I)
        Me.MinimumSize = New Size(89I, 25I)
        Me.Font = New Font("Microsoft Sans Serif", 10.0!, FontStyle.Regular)
        Me.ForeColor = Color.WhiteSmoke
        Me.TextAlign = ContentAlignment.MiddleLeft
    End Sub
#End Region
#Region " Properties: VistaStyle "
    Public Property VistaStyle() As Boolean
        Get
            Return _VistaStyleBoolean
        End Get
        Set(ByVal value As Boolean)
            If _VistaStyleBoolean <> value Then
                _VistaStyleBoolean = value
                Me.Refresh()
            End If
        End Set
    End Property
#End Region
#Region " Events: Paint "
    Private Sub VistaLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If Me._VistaStyleBoolean = True Then
            Using b As New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(0I, 0I, Me.Width, Me.Height), _gradientBegin, _gradientEnd, Drawing2D.LinearGradientMode.Horizontal)
                e.Graphics.FillRectangle(b, New Rectangle(0I, 0I, Me.Width, Me.Height))
            End Using
            Dim format As New StringFormat
            Select Case Me.TextAlign
                Case ContentAlignment.BottomCenter, ContentAlignment.BottomLeft, ContentAlignment.BottomRight
                    format.LineAlignment = StringAlignment.Far
                Case ContentAlignment.MiddleCenter, ContentAlignment.MiddleLeft, ContentAlignment.MiddleRight
                    format.LineAlignment = StringAlignment.Center
                Case ContentAlignment.TopCenter, ContentAlignment.TopLeft, ContentAlignment.TopRight
                    format.LineAlignment = StringAlignment.Near
            End Select
            Select Case Me.TextAlign
                Case ContentAlignment.BottomCenter, ContentAlignment.MiddleCenter, ContentAlignment.TopCenter
                    format.Alignment = StringAlignment.Center
                Case ContentAlignment.BottomLeft, ContentAlignment.MiddleLeft, ContentAlignment.TopLeft
                    format.Alignment = StringAlignment.Near
                Case ContentAlignment.BottomRight, ContentAlignment.MiddleRight, ContentAlignment.TopRight
                    format.Alignment = StringAlignment.Far
            End Select
            Using forebrush As New SolidBrush(Me.ForeColor)
                e.Graphics.DrawString(Me.Text, Me.Font, forebrush, Me.ClientRectangle, format)
            End Using
            format.Dispose()
        End If
    End Sub
#End Region

End Class
To have it work in VS2005, simply comment the "Option Infer Off' line out and it'll work the same.

John,
It's a habbit to specify the type when I type the value in so I know what's going on with those vars as I debug.
 
John,
It's a habbit to specify the type when I type the value in so I know what's going on with those vars as I debug.
They are integers, which is the default integral data type, you can hover them same as with typed or inferred variables to see the effective data type. There are also other rules for default types of numeric values, for example fractional defaults to Double and large integrals defaults to Long. I think adding the type character here is like using hungarian notation for naming variables, which is not recommended. I'm all in favour of a strong type system and all the options on (strict, explicit and infer), but I've never seen anyone typing out the data values like that before and just thought it was awkward to read.

I also see you have Infer Off, inferring actually supports the explicit type system, for people that don't want to or forget typing a variable inferring helps to strong type where possible at compile time, reducing runtime type problems. New language features like anonymous types also rely on inferring.
 
Back
Top