[英]Custom GroupBox Control with colored border
我發現了這個從 GroupBox 派生的自定義控件,它允許更改其邊框的顏色。
我知道代碼最初來自 StackOverflow,雖然我找不到它。
由於某種原因,在設置 GroupBox 的 Text 屬性時,最后一個字母總是被剪掉。
任何比我更有經驗的人都可以在代碼中看到導致這種情況的任何內容嗎?
Public Class myGroupBox
Inherits GroupBox
Private borderColor As Color
Public Sub New()
MyBase.New
Me.borderColor = Color.Blue
End Sub
Public Property BorderColour() 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 = e.ClipRectangle
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 = e.ClipRectangle
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
不要混合使用TextRenderer
方法和Graphics
方法進行字符串測量和繪圖。 當你有顯卡 object 時,你應該使用它。
使用Graphics.MeasureString()
和Graphics.DrawString()
或TextRenderer.MeasureText()
和TextRenderer.DrawText()
。
該代碼存在一些問題:
TextRenderer.MeasureText
測量文本,但當前圖形 ( IdeviceContext
) object 未傳遞給方法。e.ClipRectangle
作為邊框的度量:更好地使用Control.ClientRectangle
Graphics.DrawString
用於繪制文本,當使用另一個工具測量它時Option Strict Off
構建的 ► 注意:這不是框架繪制 GroupBox 邊框的方式。 我們應該畫線,否則文本不能呈現透明:因為它是隱藏由ControlPaint.DrawBorder
繪制的線的繪制文本,文本背景不能是透明的。
這是該控件的重新訪問版本,其中一些調整可能在其他場合有用:
如果您認為 Text 繪制得太靠近左側,只需根據需要進行偏移即可。 您還可以添加一個屬性來定義 alignment。
SystemColors.Window
:使用 PropertyGrid 設置另一種顏色。Public Class myGroupBox
Inherits GroupBox
Private ReadOnly flags As TextFormatFlags =
TextFormatFlags.Top Or TextFormatFlags.Left Or
TextFormatFlags.LeftAndRightPadding Or TextFormatFlags.EndEllipsis
Private m_BorderColor As Color = SystemColors.Window
Public Property BorderColor As Color
Get
Return m_BorderColor
End Get
Set
m_BorderColor = Value
Me.Invalidate()
If DesignMode Then Me.Parent?.Invalidate(Me.Bounds)
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim midText = TextRenderer.MeasureText(e.Graphics, Text, Font, ClientSize).Height \ 2 + 2
Dim rect = New Rectangle(0, midText, ClientSize.Width, ClientSize.Height - midText)
ControlPaint.DrawBorder(e.Graphics, rect, BorderColor, ButtonBorderStyle.Solid)
Dim textRect = Rectangle.Inflate(ClientRectangle, -4, 0)
TextRenderer.DrawText(e.Graphics, $" {Me.Text} ", Font, textRect, ForeColor, BackColor, flags)
End Sub
End Class
我嘗試了 Jimi 的代碼並成功運行它。 但是,當所說的 groupbox Text 為空時,它會有一個小的間隙(邊框沒有完全關閉)。 我不是 VB.Net 的專家,並且發現很難測試代碼。
幸運的是,使用 Ian Barber 發布的原始代碼,經過反復試驗,通過向文本大小寬度添加一個常量值 (textRect.Width = tSize.Width + 2) 解決了這個問題。
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim tSize As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font) 'TextRenderer.MeasureText(Me.Text, Me.Font)
Dim borderRect As Rectangle = e.ClipRectangle
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 = e.ClipRectangle
textRect.X = textRect.X + 6
textRect.Width = tSize.Width + 2
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.