Re: Outline Text in a Label
The code you posted is for a custom label. It draws an outline around the text instead of just around the label...
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.BackColor = Color.Teal
Label1.ForeColor = Color.White
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim outline_color As Color = Color.Black
Dim border_thickness As Integer = 5
Dim outlinePen As Pen = New Pen(outline_color, border_thickness)
Dim r As New Rectangle(Label1.Left - 2, Label1.Top - 2, Label1.Width + 4, Label1.Height + 4) ' edited here
e.Graphics.DrawRectangle(outlinePen, r) ' edited here
End Sub
Edit: Slight improvement to the border appearance
2 Attachment(s)
Re: Outline Text in a Label
This is a standard Label with a Black border (as my example shows)
Attachment 196090
This is the custom Label you posted
Attachment 196091
Re: Outline Text in a Label
To use the custom control you have, add the class (as edited below) to your project, then after running (or rebuilding), you’ll find the BorderLabel control at the top of your toolbox, and you can add it to your form and set properties either in the properties window, or in your code…
Code:
Imports System.Drawing.Drawing2D
Public Class BorderLabel
Inherits Label
Public outline_color As Color = Color.Black
Public border_thickness As Integer = 5
Public Sub New()
MyBase.New
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.FillRectangle(New SolidBrush(BackColor), ClientRectangle)
Dim gp As GraphicsPath = New GraphicsPath
Dim outline As Pen = New Pen(Me.outline_color, Me.border_thickness)
Dim sf As StringFormat = New StringFormat
Dim foreBrush As Brush = New SolidBrush(ForeColor)
gp.AddString(Me.Text, Font.FontFamily, CType(Font.Style, Integer), Font.Size, ClientRectangle, sf)
e.Graphics.ScaleTransform(1.3!, 1.35!)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
e.Graphics.DrawPath(outline, gp)
e.Graphics.FillPath(foreBrush, gp)
End Sub
End Class
Note that when you change things in the custom control code, it’ll be that way for every instance of the control you use, so, for example, if you wanted outline_color to be different in 2 different instances of your control, you’d change it to a Property which means you can use the same code with optional colors in more than 1instance of the control.
Re: Outline Text in a Label
Thanks Paul,
I have it working now.