Mat*_*ias 3 vb.net groupbox winforms
我使用以下代码创建了带有彩色边框的组合框:
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
Run Code Online (Sandbox Code Playgroud)
问题是,它被放置在一个可滚动的容器内,如果它滚动,边框不会正确重绘:

你可以让它比这更糟糕的行为:

这是错误的,因为您的代码使用e.ClipRectangle.请注意,它在您的代码段中显示两次.该变量并没有给你的边界矩形.它告诉您需要重新绘制多少客户区域.这是一个优化机会,您可以通过省略不需要刷新的客户区的部分来减少绘制.
它通常与显示矩形大小相同,这就是为什么看起来它工作得很好.但是当你把它放在一个可滚动的容器中时,Windows不会通过blitting客户区的可以移动的部分来优化滚动.然后为滚动显示的部件生成一个颜料.用一个小的e.ClipRectangle.您可以在屏幕截图中看到小矩形.
将e.ClipRectangle替换为Me.DisplayRectangle.