标签带边框的文字

时间:2018-04-05 16:04:13

标签: vb.net visual-studio colors label border

我有一个标签,其中前景色的文字“UserName”为白色,表格为天蓝色。我想在标签本身添加黑色边框,而不是标签内部的文字。

这可能吗?

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnLogin.Enabled = False
        centrarVentana(Me)
        lblNombreUsuario.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
End Sub

代码显然只是为了表演,因为它没有做我想要的。

编辑最终:非常感谢大家。它终于奏效了!我在这里留下代码,所以每个人都可以重复使用。一旦你明白它实际上很容易。

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(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

enter image description here

注意:此问题与Setting a Font with outline Color in C#不完全相同,因为我在Visual Basic上并且必须对其进行代码更改才能正常工作。

1 个答案:

答案 0 :(得分:0)

查看KeithS answer

您也可以创建自己的标签:

    Public Class CustomLabel
    Inherits Label

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid, Color.Red, 5, ButtonBorderStyle.Solid)
    End Sub
End Class

以下是实施方法

Dim newForm As Form = New Form
Dim newLabel As CustomLabel = New CustomLabel
newForm.Controls.Add(newLabel)
newLabel.BackColor = Color.Black
newLabel.Font = New System.Drawing.Font("Microsoft Arial", 18!, FontStyle.Regular, GraphicsUnit.Point, CType(0,Byte))
newLabel.ForeColor = Color.Crimson
newLabel.Text = "Some text on a topmost transparent form window"
newForm.Show
newForm.TopMost = true
newLabel.AutoSize = true
newLabel.Location = New Point(230, 375)