Winforms 如何绘制带有标题的水平分隔线?
我需要从视觉上分离表单上的各个区域,而Winforms 如何绘制带有标题的水平分隔线?,winforms,controls,Winforms,Controls,我需要从视觉上分离表单上的各个区域,而GroupBox控件对于我想要的外观来说有点“沉重”。我还需要为每个分隔符添加一个标题 我找到了各种方法来部分实现这一点,最常用的方法是使用标签控件: 向窗体中添加标签控件 将标签文本设置为空 将BorderStyle设置为Fixed3D 将AutoSize设置为false 将高度设置为2 然而,像这样的技术不允许我们指定任何文本作为分隔符标题 如何创建同时显示其文本属性值的分隔符控件?我已经能够通过自定义控件实现这一点。在设计时和运行时都是这样: 代码如
GroupBox
控件对于我想要的外观来说有点“沉重”。我还需要为每个分隔符添加一个标题
我找到了各种方法来部分实现这一点,最常用的方法是使用标签控件:
向窗体中添加标签控件
将标签文本设置为空
将BorderStyle设置为Fixed3D
将AutoSize设置为false
将高度设置为2
然而,像这样的技术不允许我们指定任何文本作为分隔符标题
如何创建同时显示其文本属性值的分隔符控件?我已经能够通过自定义控件实现这一点。在设计时和运行时都是这样:
代码如下:
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
部分公共类行
继承控制权
私有子行_Paint(发送方作为行,e作为PaintEventArgs)处理Me.Paint
将背景设置为矩形
Dim Otexsize As Size
暗淡的_
九,,
iY为整数
oTextSize=TextRenderer.MeasureText(Me.Text,Me.Font)
Me.Height=oTextSize.Height+3
iX=1
iY=我的身高/2
使用“作为新笔打开”(Me.LineColor)
e、 图形.抽绳(开放式,iX,iY,Me.宽度-iX-1,iY)
终端使用
使用“作为新笔打开”(颜色:白色)
e、 图形绘制线(开放式,iX+1,iY+1,Me.宽度-iX,iY+1)
终端使用
如果oTextSize.Height>0,则
使用Objrush作为新的SolidBrush(Me.BackColor)
oBackground=新矩形(7,1,oTextSize.Width-2,oTextSize.Height)
e、 图形.圆角矩形(倒圆角,倒圆角)
终端使用
使用oBrush作为新的SolidBrush(Me.ForeColor)
e、 图形.抽绳(Me.Text,Me.Font,oBrush,7,1)
终端使用
如果结束
端接头
公共属性LineColor作为颜色
得到
还给我
结束
设置(值为颜色)
Me.\u LineColor=值
使无效
端集
端属性
私有属性_linecoloras Color=SystemColors.ActiveBorder
末级
Winforms中也有绘图工具控件(类似于VB6 days中的控件)。我记得你必须安装一些PowerPack,这是5年前的事了,现在使用图形对象可能会更好。@Jeremythonpson是的,谢谢,我也看过了。。。但是不幸的是,行
控件没有可以使用的文本
属性。是的,我同意你的解决方案,很好。仅供参考。@Jeremythonpson我喜欢Redux工具,它看起来非常方便。好发现。