PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
True Gradient Label Control-VBForums
Results 1 to 1 of 1

Thread: True Gradient Label Control

  1. #1

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Smile True Gradient Label Control

    I don't know about you but when I did a "gradient label [vb.net]" search in Google all I got was nothing of the sort. Only controls out there in the first few pages draw the label text with a flat brush, while the background was drawn gradient... This to me is not a "gradient label".

    So I created one myself This is my version of a gradient label in around 120 LOC.

    Because of how short it is, I just pasted the code instead of uploading the entire project. Just create a new User-Control and paste in the code below.

    vbnet Code:
    1. Imports System.Drawing.Drawing2D
    2.  
    3. Public Class GradientLabel
    4.     Inherits System.Windows.Forms.Label
    5.  
    6.     Public Overrides Property Text As String
    7.         Get
    8.             Return MyBase.Text
    9.         End Get
    10.         Set(ByVal value As String)
    11.             MyBase.Text = value
    12.             'TextRender method.
    13.             Me._sf = TextRenderer.MeasureText(value, Me.Font)
    14.             'MeasureString method.
    15.             'Using g As Graphics = Me.CreateGraphics()
    16.             '    Me._sf = g.MeasureString(value, Me.Font)
    17.             'End Using
    18.         End Set
    19.     End Property
    20.  
    21.     Public Overrides Property Font As System.Drawing.Font
    22.         Get
    23.             Return MyBase.Font
    24.         End Get
    25.         Set(ByVal value As System.Drawing.Font)
    26.             MyBase.Font = Value
    27.             Me.Invalidate()
    28.         End Set
    29.     End Property
    30.  
    31.     Private _ColorHigh As Color = Color.WhiteSmoke
    32.     Public Property ColorHigh As Color
    33.         Get
    34.             Return Me._ColorHigh
    35.         End Get
    36.         Set(ByVal value As Color)
    37.             Me._ColorHigh = value
    38.             Me.Invalidate()
    39.         End Set
    40.     End Property
    41.  
    42.     Private _ColorLow As Color = Color.Silver
    43.     Public Property ColorLow As Color
    44.         Get
    45.             Return Me._ColorLow
    46.         End Get
    47.         Set(ByVal value As Color)
    48.             Me._ColorLow = value
    49.             Me.Invalidate()
    50.         End Set
    51.     End Property
    52.  
    53.     Dim _sf As SizeF = New SizeF(0, 0)
    54.     Private ReadOnly Property sf() As SizeF
    55.         Get
    56.             Return Me._sf
    57.         End Get
    58.     End Property
    59.  
    60.  
    61.     Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
    62.         Dim rfb As RectangleF 'Brush area rectangle
    63.         Dim rfa As RectangleF = New RectangleF(0, 0, Me.Width, Me.Height+16) 'String area rectangle.
    64.         Dim pf As PointF
    65.         Dim gp As New GraphicsPath
    66.  
    67.         Using Format As New StringFormat()
    68.  
    69.             Select Case Me.TextAlign
    70.                 Case ContentAlignment.TopLeft
    71.                     Format.LineAlignment = StringAlignment.Near
    72.                     Format.Alignment = StringAlignment.Near
    73.                 Case ContentAlignment.TopCenter
    74.                     Format.LineAlignment = StringAlignment.Near
    75.                     Format.Alignment = StringAlignment.Center
    76.                 Case ContentAlignment.TopRight
    77.                     Format.LineAlignment = StringAlignment.Near
    78.                     Format.Alignment = StringAlignment.Far
    79.                 Case ContentAlignment.MiddleLeft
    80.                     Format.LineAlignment = StringAlignment.Center
    81.                     Format.Alignment = StringAlignment.Near
    82.                 Case ContentAlignment.MiddleCenter
    83.                     Format.LineAlignment = StringAlignment.Center
    84.                     Format.Alignment = StringAlignment.Center
    85.                 Case ContentAlignment.MiddleRight
    86.                     Format.LineAlignment = StringAlignment.Center
    87.                     Format.Alignment = StringAlignment.Far
    88.                 Case ContentAlignment.BottomLeft
    89.                     Format.LineAlignment = StringAlignment.Far
    90.                     Format.Alignment = StringAlignment.Near
    91.                 Case ContentAlignment.BottomCenter
    92.                     Format.LineAlignment = StringAlignment.Far
    93.                     Format.Alignment = StringAlignment.Center
    94.                 Case ContentAlignment.BottomRight
    95.                     Format.LineAlignment = StringAlignment.Far
    96.                     Format.Alignment = StringAlignment.Far
    97.             End Select
    98.  
    99.             gp.AddString(Me.Text, Me.Font.FontFamily, Me.Font.Style, sf.Height, rfa, Format)
    100.  
    101.             Select Format.LineAlignment
    102.                 Case StringAlignment.Near:  pf = New PointF(0, 0)
    103.                 Case StringAlignment.Center:  pf = New PointF(0, CSng(Me.Height/2-(gp.GetBounds.Height+24)/2))
    104.                 Case Stringalignment.Far:  pf = New PointF(0, CSng(Me.Height-(gp.GetBounds.Height+24)))
    105.             End Select
    106.             rfb = New RectangleF(pf, New SizeF(Me.Width, gp.GetBounds.Height+24))
    107.  
    108.             e.Graphics.SmoothingMode = SmoothingMode.HighQuality 'Change the smoothing mode before doing any actual drawing.
    109.             'e.Graphics.FillRectangle(Brushes.Black, rfb) 'Brush indicator
    110.             Using base As New LinearGradientBrush(rfb, ColorHigh, ColorLow, 90),
    111.                 border As New LinearGradientBrush(rfb, ColorHigh, ColorHigh, 90)
    112.                 e.Graphics.FillPath(base, gp)
    113.                 e.Graphics.DrawPath(New Pen(border, 2), gp)
    114.             End Using  
    115.         End Using
    116.     End Sub
    117.  
    118. End Class

    EDIT: I know it's buggy when messing with most fonts. This actually has to do with the trickiest part of this control, which was maintaining a steady gradient of the label. Regardless of it's alignments. So for now, in my example I used arial, as I do with most of my projects. So I cannot be exact with which fonts work or do not work.

    If anyone has another method they'd like to share that could possible fix this please do share.
    Attached Images Attached Images  
    Last edited by DavesChillaxin; Jun 6th, 2012 at 01:23 PM. Reason: Fix: Invalidation when changing the font.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width