如何绘制带有标题的水平分隔线?
How can I draw a horizontal separator line with a title?
我需要在视觉上分隔表单上的各个区域,GroupBox
控件有点 'heavy' 符合我想要的外观。我还需要用标题标记每个分隔符。
我发现了多种方法来部分执行此操作,最常用的方法是使用 something like this 和 Label
控件:
- 向表单添加标签控件
- 将标签文本设置为空
- 将 BorderStyle 设置为 Fixed3D
- 将自动调整大小设置为 false
- 将高度设置为 2
但是,像这样的技术不允许我们指定任何文本用作分隔符标题。
如何创建一个分隔符控件,同时显示其 Text
属性 的值?
我已经能够使用自定义控件完成此操作。在设计时和 运行 时看起来都是这样:
代码如下:
Partial Public Class Line
Inherits Control
Private Sub Line_Paint(Sender As Line, e As PaintEventArgs) Handles Me.Paint
Dim oBackground As Rectangle
Dim oTextSize As Size
Dim _
iX,
iY As Integer
oTextSize = TextRenderer.MeasureText(Me.Text, Me.Font)
Me.Height = oTextSize.Height + 3
iX = 1
iY = Me.Height / 2
Using oPen As New Pen(Me.LineColor)
e.Graphics.DrawLine(oPen, iX, iY, Me.Width - iX - 1, iY)
End Using
Using oPen As New Pen(Color.White)
e.Graphics.DrawLine(oPen, iX + 1, iY + 1, Me.Width - iX, iY + 1)
End Using
If oTextSize.Height > 0 Then
Using oBrush As New SolidBrush(Me.BackColor)
oBackground = New Rectangle(7, 1, oTextSize.Width - 2, oTextSize.Height)
e.Graphics.FillRectangle(oBrush, oBackground)
End Using
Using oBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, oBrush, 7, 1)
End Using
End If
End Sub
<DefaultValue(GetType(Color), "System.Drawing.SystemColors.ActiveBorder")>
<Description("The color of the line.")>
<Browsable(True)>
<Category("Appearance")>
Public Property LineColor As Color
Get
Return Me._LineColor
End Get
Set(Value As Color)
Me._LineColor = Value
Me.Invalidate()
End Set
End Property
Private Property _LineColor As Color = SystemColors.ActiveBorder
End Class
我需要在视觉上分隔表单上的各个区域,GroupBox
控件有点 'heavy' 符合我想要的外观。我还需要用标题标记每个分隔符。
我发现了多种方法来部分执行此操作,最常用的方法是使用 something like this 和 Label
控件:
- 向表单添加标签控件
- 将标签文本设置为空
- 将 BorderStyle 设置为 Fixed3D
- 将自动调整大小设置为 false
- 将高度设置为 2
但是,像这样的技术不允许我们指定任何文本用作分隔符标题。
如何创建一个分隔符控件,同时显示其 Text
属性 的值?
我已经能够使用自定义控件完成此操作。在设计时和 运行 时看起来都是这样:
代码如下:
Partial Public Class Line
Inherits Control
Private Sub Line_Paint(Sender As Line, e As PaintEventArgs) Handles Me.Paint
Dim oBackground As Rectangle
Dim oTextSize As Size
Dim _
iX,
iY As Integer
oTextSize = TextRenderer.MeasureText(Me.Text, Me.Font)
Me.Height = oTextSize.Height + 3
iX = 1
iY = Me.Height / 2
Using oPen As New Pen(Me.LineColor)
e.Graphics.DrawLine(oPen, iX, iY, Me.Width - iX - 1, iY)
End Using
Using oPen As New Pen(Color.White)
e.Graphics.DrawLine(oPen, iX + 1, iY + 1, Me.Width - iX, iY + 1)
End Using
If oTextSize.Height > 0 Then
Using oBrush As New SolidBrush(Me.BackColor)
oBackground = New Rectangle(7, 1, oTextSize.Width - 2, oTextSize.Height)
e.Graphics.FillRectangle(oBrush, oBackground)
End Using
Using oBrush As New SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Text, Me.Font, oBrush, 7, 1)
End Using
End If
End Sub
<DefaultValue(GetType(Color), "System.Drawing.SystemColors.ActiveBorder")>
<Description("The color of the line.")>
<Browsable(True)>
<Category("Appearance")>
Public Property LineColor As Color
Get
Return Me._LineColor
End Get
Set(Value As Color)
Me._LineColor = Value
Me.Invalidate()
End Set
End Property
Private Property _LineColor As Color = SystemColors.ActiveBorder
End Class