Vb.net 带彩色边框的Winforms groupbox
我使用以下代码创建了一个带有彩色边框的groupbox:Vb.net 带彩色边框的Winforms groupbox,vb.net,winforms,groupbox,Vb.net,Winforms,Groupbox,我使用以下代码创建了一个带有彩色边框的groupbox: Public Class BorderGroupBox Inherits GroupBox Private _borderColor As Color Private _borderWidth As Integer Private _borderStyle As ButtonBorderStyle ... Protected Overrides Sub OnPaint(ByVal e
Public Class BorderGroupBox
Inherits GroupBox
Private _borderColor As Color
Private _borderWidth As Integer
Private _borderStyle As ButtonBorderStyle
...
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
Dim borderRect As Rectangle = e.ClipRectangle
borderRect.Y = CInt((borderRect.Y + (tSize.Height / 2)))
borderRect.Height = CInt((borderRect.Height - (tSize.Height / 2)))
ControlPaint.DrawBorder(e.Graphics, borderRect, _borderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle, BorderColor, _borderWidth, _borderStyle)
Dim textRect As Rectangle = e.ClipRectangle
textRect.X = (textRect.X + 6)
textRect.Width = tSize.Width + 6
textRect.Height = tSize.Height
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect)
End Sub
End Class
问题是,它被放置在一个可滚动的容器中,如果它被滚动,则边框没有正确地重新绘制:
你可以让它表现得比这更糟糕: 这是错误的,因为您的代码使用e.ClipRectangle。请注意,它在代码段中出现两次。该变量不提供边框矩形。它告诉您需要重新绘制多少客户区域。这是一个优化机会,您可以通过省略客户端区域中不需要刷新的部分来减少消耗 它通常与显示矩形大小相同,这就是为什么它看起来工作得很好的原因。但当你把它放在一个可滚动的容器中时,Windows会通过对客户端区域中可以移动的部分进行点选来优化滚动。然后为卷轴显示的零件生成绘制。带着一个小的e.Clip。你可以在截图中看到,注意小矩形
将e.ClipRectangle替换为Me.DisplayRectangle。此类允许为所有框设置边框,或通过向组框的“属性”选项卡添加边框颜色控件来单独设置边框
Public Class GroupBoxA
Inherits GroupBox
Private _borderColor As Color
Public Sub New()
MyBase.New()
Me._borderColor = Color.OrangeRed
End Sub
Public Property BorderColor() As Color
Get
Return Me._borderColor
End Get
Set(ByVal value As Color)
Me._borderColor = value
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
Dim borderRect As Rectangle = Me.DisplayRectangle
borderRect.Y = (borderRect.Y + (tSize.Height / 2))
borderRect.Height = (borderRect.Height - (tSize.Height / 2))
ControlPaint.DrawBorder(e.Graphics, borderRect, Me._borderColor,
ButtonBorderStyle.Solid)
Dim textRect As Rectangle = Me.DisplayRectangle
textRect.X = (textRect.X + 6)
textRect.Width = tSize.Width
textRect.Height = tSize.Height
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), textRect)
End Sub
End Class
对于boder和文本,必须使用Me.ClientRectangle而不是Me.DisplayRectangle。如果使用旧方法,则闪烁问题无法解决,组框的文本将不会显示。在哪种类型的可滚动容器中。。面板,SplitContainer?这是一个FlowLayoutPanel如何使groupbox内的控制器背景色透明?我用
Me.DisplayRectangle
替换了这两行,但现在它根本并没有绘制任何边框。我在发布之前对它进行了测试,使用DisplayRectangle效果很好。这是应该的。你有没有在代码中更改其他内容?你用的是什么容器?当然,它不可用,所以给了变量固定值并添加了BorderColor。我测试了一个普通面板,上面有一个大的AutoScrollMinSize。它现在可以工作了,谢谢。但子控件放置在x=0、y=0处,并覆盖边界。有没有办法设置容器位置?很抱歉问了一个愚蠢的问题,但是我如何在窗体上使用这个新类来显示为一个新的组框控件?将grpNew设置为新的组框控件。Add(grpNew)Me.ClientRectangle解决了所有问题。Me.DisplayRectangle在错误的位置绘制边框