Monday, August 18, 2014

Make smooth outline for Label

hi, i have this code. (gained from IronRazerz )


so far, the "Outline Text" is running. But when i meet with "M" its look the outline is break the font.




Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LabelEx1.AutoSize = False
LabelEx1.Bounds = New Rectangle(0, 0, Me.ClientSize.Width, 120)
LabelEx1.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right
TrackBar1.Value = 1
TrackBar1.Minimum = 1
TrackBar1.Maximum = 10
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LabelEx1.ForeColor = GetColor(LabelEx1.ForeColor)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
LabelEx1.BackColor = GetColor(LabelEx1.BackColor)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
LabelEx1.OutlineColor = GetColor(LabelEx1.OutlineColor)
End Sub

Private Function GetColor(ByVal currentcolor As Color) As Color
Dim clr As Color = currentcolor
Using cd As New ColorDialog
If clr <> Color.Transparent Then cd.Color = clr
If cd.ShowDialog = DialogResult.OK Then
clr = cd.Color
End If
End Using
Return clr
End Function

Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
LabelEx1.OutlineThickness = TrackBar1.Value
End Sub

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll

End Sub
End Class


Public Class LabelEx
Inherits Label

Private _OutlineColor As Color = Color.Black
Private _OutlineThickness As Integer = 1
Private _OutlinePen As New Pen(Me.OutlineColor, Me.OutlineThickness) 'The pen used for drawing the outline

Public Sub New()
MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True) 'Lets the user set BackColor as transparent
MyBase.SetStyle(ControlStyles.UserPaint, True)
End Sub

Public Property OutlineColor() As Color
Get
Return _OutlineColor
End Get
Set(ByVal value As Color)
_OutlineColor = value
_OutlinePen.Color = value 'reset the outline pen color to the users choice
Me.Refresh()
End Set
End Property

Public Property OutlineThickness() As Integer
Get
Return _OutlineThickness
End Get
Set(ByVal value As Integer)
If value < 1 Then value = 1 'Stops the user from setting it lower than 1
If value > 50 Then value = 50 'Stops the user from setting it higher than 10
_OutlineThickness = value
_OutlinePen.Width = value 'reset the outline pen thickness
Me.Refresh()
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
With e.Graphics
.FillRectangle(New SolidBrush(Me.BackColor), New Rectangle(0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height))
.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Using pth As New Drawing2D.GraphicsPath
Using sf As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
pth.AddString(Me.Text, Me.Font.FontFamily, Me.Font.Style, ((.DpiY * Me.Font.Size) / 72), New Rectangle(0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height), sf)
End Using
.DrawPath(_OutlinePen, pth) 'Use the _OutlinePen to draw the outline
.FillPath(New SolidBrush(Me.ForeColor), pth)


End Using
End With
End Sub

Protected Overrides Sub Dispose(ByVal disposing As Boolean)
_OutlinePen.Dispose() 'Dispose the OutlinePen
MyBase.Dispose(disposing)
End Sub
End Class

any idea to hide that ? so its looks like


thanks.



No comments:

Post a Comment