16,718
社区成员
发帖
与我相关
我的任务
分享
Public Class Form1
Dim WithEvents TestGroupBox As New GroupBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TestGroupBox.Text = "TestGroupBox1"
TestGroupBox.Visible = True
Me.Controls.Add(TestGroupBox)
End Sub
Private Sub TestGroupBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TestGroupBox.Paint
e.Graphics.Clear(TestGroupBox.BackColor)
e.Graphics.DrawString(TestGroupBox.Text, TestGroupBox.Font, Brushes.Red, 10, 1)
e.Graphics.DrawLine(Pens.Blue, 1, 7, 8, 7)
e.Graphics.DrawLine(Pens.Blue, 90, 7, TestGroupBox.Width - 2, 7)
e.Graphics.DrawLine(Pens.Blue, 1, 7, 1, TestGroupBox.Height - 2)
e.Graphics.DrawLine(Pens.Blue, 1, TestGroupBox.Height - 2, TestGroupBox.Width - 2, TestGroupBox.Height - 2)
e.Graphics.DrawLine(Pens.Blue, TestGroupBox.Width - 2, 7, TestGroupBox.Width - 2, TestGroupBox.Height - 2)
End Sub
End Class
Imports System.ComponentModel
Public Class GroupBoxControl
Inherits Panel
Private lbl As Label
Private mBorderColor As Color = Color.Blue
<Browsable(True)> _
Public Overrides Property Text() As String
Get
Return lbl.Text
End Get
Set(ByVal value As String)
lbl.Text = value
Me.Invalidate()
End Set
End Property
Public Property BorderColor() As Color
Get
Return mBorderColor
End Get
Set(ByVal value As Color)
mBorderColor = value
Me.Invalidate()
End Set
End Property
Private Sub GroupBoxControl_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles Me.ControlAdded
If e.Control.Left < 5 Then e.Control.Left = 5
If e.Control.Top < 15 Then e.Control.Top = 15
If e.Control.Right > Me.Width - 10 Then e.Control.Left = Me.Width - 10 - e.Control.Width
If e.Control.Bottom > Me.Height - 10 Then e.Control.Top = Me.Height - 10 - e.Control.Height
End Sub
Private Sub GroupBoxControl_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawRectangle(New Pen(mBorderColor), New Rectangle(2, 8, Me.Width - 5, Me.Height - 12))
End Sub
Public Sub New()
lbl = New Label
lbl.AutoSize = True
lbl.Text = "GroupBox"
Me.Controls.Add(lbl)
End Sub
Private Sub GroupBoxControl_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
lbl.Location = New Point(10, 1)
End Sub
End Class