'水平分隔线 LineH.vb的完整代码
Public Class LineH
Inherits System.Windows.Forms.UserControl
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserControl1 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'LineH
'
Me.Name = "LineH"
Me.Size = New System.Drawing.Size(10, 2)
End Sub
#End Region
Private Sub LineH_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim r As Rectangle = Me.ClientRectangle
Dim darkPen As Pen = New Pen(SystemColors.ControlDark, 1)
Dim LightPen As Pen = New Pen(Color.White)
'用暗色调处理上边缘
g.DrawLine(darkPen, r.Left, r.Top, r.Right, r.Top)
'用亮色调处理下边缘
g.DrawLine(LightPen, r.Left, r.Top + 1, r.Right, r.Top + 1)
End Sub
End Class
------------------------------------------------------
'垂直分隔线 LineV.vb的完整代码
Public Class LineV
Inherits System.Windows.Forms.UserControl
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'LineV
'
Me.Name = "LineV"
Me.Size = New System.Drawing.Size(2, 10)
End Sub
#End Region
Private Sub LineV_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim r As Rectangle = Me.ClientRectangle
Dim darkPen As Pen = New Pen(SystemColors.ControlDark, 1)
Dim LightPen As Pen = New Pen(Color.White)
'用暗色调处理左边缘
g.DrawLine(darkPen, r.Left, r.Top, r.Left, r.Bottom)
'用亮色调处理右边缘
g.DrawLine(LightPen, r.Left + 1, r.Top, r.Left + 1, r.Bottom)
End Sub
新建一个Visual Basic的 Windows 控件库。在项目中添加两个用户控件,一个为:LineH 水平分隔线 ,一个为:LineV垂直分隔线 ,代码如下。经过生成.dll,在其他项目引用,就可以使用。
''''''''水平分隔线 LineH.vb的完整代码
Public Class LineH
Inherits System.Windows.Forms.UserControl
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
''''''''该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
''''''''在 InitializeComponent() 调用之后添加任何初始化
End Sub
''''''''UserControl1 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''''''''Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
''''''''注意: 以下过程是 Windows 窗体设计器所必需的
''''''''可以使用 Windows 窗体设计器修改此过程。
''''''''不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
''''''''
''''''''LineH
''''''''
Me.Name = "LineH"
Me.Size = New System.Drawing.Size(10, 2)
End Sub
#End Region
Private Sub LineH_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim r As Rectangle = Me.ClientRectangle
Dim darkPen As Pen = New Pen(SystemColors.ControlDark, 1)
Dim LightPen As Pen = New Pen(Color.White)
''''''''用暗色调处理上边缘
g.DrawLine(darkPen, r.Left, r.Top, r.Right, r.Top)
''''''''用亮色调处理下边缘
g.DrawLine(LightPen, r.Left, r.Top + 1, r.Right, r.Top + 1)
End Sub
End Class
------------------------------------------------------
''''''''垂直分隔线 LineV.vb的完整代码
Public Class LineV
Inherits System.Windows.Forms.UserControl
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
''''''''该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
''''''''在 InitializeComponent() 调用之后添加任何初始化
End Sub
''''''''UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''''''''Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
''''''''注意: 以下过程是 Windows 窗体设计器所必需的
''''''''可以使用 Windows 窗体设计器修改此过程。
''''''''不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
''''''''
''''''''LineV
''''''''
Me.Name = "LineV"
Me.Size = New System.Drawing.Size(2, 10)
End Sub
#End Region
Private Sub LineV_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim r As Rectangle = Me.ClientRectangle
Dim darkPen As Pen = New Pen(SystemColors.ControlDark, 1)
Dim LightPen As Pen = New Pen(Color.White)
''''''''用暗色调处理左边缘
g.DrawLine(darkPen, r.Left, r.Top, r.Left, r.Bottom)
''''''''用亮色调处理右边缘
g.DrawLine(LightPen, r.Left + 1, r.Top, r.Left + 1, r.Bottom)
End Sub
<Browsable(False)> _
Public Shadows Property Location() As System.Drawing.Point
Get
Return MyBase.Location
End Get
Set(ByVal Value As System.Drawing.Point)
MyBase.Location = Value
End Set
End Property
<Browsable(False)> _
Public Shadows Property Size() As System.Drawing.Size
Get
Return MyBase.Size
End Get
Set(ByVal Value As System.Drawing.Size)
MyBase.Size = Value
End Set
End Property
<Browsable(False)> _
Public Overrides Property ForeColor() As System.Drawing.Color
Get
Return MyBase.ForeColor
End Get
Set(ByVal Value As System.Drawing.Color)
MyBase.ForeColor = Value
End Set
End Property
#End Region
#Region " Overirdes "
Protected Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
If Me._x1 = 0 AndAlso Me._y1 = 0 AndAlso Me._x2 = 0 AndAlso Me._y2 = 0 Then
resetBounds(Me.Left + margin, Me.Top + margin, Me.Left + Me.Width - margin, Me.Top + Me.Height - margin, Me._borderWidth)
End If
End Sub
Protected Overrides Sub OnLocationChanged(ByVal e As System.EventArgs)
MyBase.OnLocationChanged(e)
Dim xOffset As Integer
Dim yOffset As Integer
If Me._x1 > Me._x2 Then
xOffset = Me.Location.X + margin - Me._x2
Else
xOffset = Me.Location.X + margin - Me._x1
End If
If Me._y1 > Me._y2 Then
yOffset = Me.Location.Y + margin - Me._y2
Else
yOffset = Me.Location.Y + margin - Me._y1
End If
If (xOffset = 0) AndAlso (yOffset = 0) Then
Return
End If
Protected Function GetLineWindowRegion(ByVal sender As Control, _
ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, _
ByVal width As Integer, ByVal style As Integer) As Region
' Parameter check
If (sender.Width = 0 Or sender.Height = 0) Then
Return (New Region(New Rectangle(Point.Empty, System.Drawing.Size.Empty)))
End If
' Create bitmap
Dim bmp As System.Drawing.Bitmap
bmp = New System.Drawing.Bitmap(sender.Width, sender.Height, sender.CreateGraphics())
' Get bitmap graphics
Dim gcBmp As System.Drawing.Graphics
gcBmp = Graphics.FromImage(bmp)
' Get drawing text fore color
If sender.BackColor.ToArgb = sender.ForeColor.ToArgb Then
If sender.ForeColor.ToArgb = Color.Blue.ToArgb Then
sender.BackColor = Color.Red
Else
sender.BackColor = Color.Blue
End If
End If
' Draw text on bitmap
gcBmp.FillRectangle(New SolidBrush(sender.ForeColor), 0, 0, sender.Width, sender.Height)
gcBmp.DrawLine(New Pen(New SolidBrush(sender.BackColor), width), x1, y1, x2, y2)
' Get window region
Return GetWindowRegion(New Point(0, 0), bmp, sender.ForeColor)
End Function
Protected Function GetWindowRegion(ByVal location As Point, _
ByVal bitmap As System.Drawing.Bitmap, _
ByVal TransColor As Color) As Region
' Create Window region,initialize region with 0
Dim region As Region = New Region(New Rectangle(location, System.Drawing.Size.Empty))
Dim width As Integer = bitmap.Width
Dim height As Integer = bitmap.Height
Dim iX As Integer = 0
Dim x As Integer
Dim y As Integer
For y = 0 To height - 1
For x = 0 To width - 1
iX = x
' Skip transparent color and find next nontransparent point.
While iX < width
If bitmap.GetPixel(iX, y).ToArgb() = TransColor.ToArgb() Then
iX += 1
' Remember this hori-start point
Dim iLeftX As Integer = iX
' Find next trans-color point
While iX < width
If bitmap.GetPixel(iX, y).ToArgb() <> TransColor.ToArgb() Then
iX += 1
Else
Exit While
End If
End While
' Create a region include these nontrans-color point
If iX <> iLeftX Then
Dim rgnTemp As Region = New Region(New Rectangle(location.X + iLeftX, location.Y + y, iX - iLeftX, 1))
' Combine to main region
region.Union(rgnTemp)
rgnTemp.Dispose()
End If
x = iX
Else
Exit While
End If
End While
Public Class Line
Inherits System.Windows.Forms.UserControl
#Region " Windows フォーム デザイナで生成されたコード "
Public Sub New()
MyBase.New()
' この呼び出しは Windows フォーム デザイナで必要です。
InitializeComponent()
' InitializeComponent() 呼び出しの後に初期化を追加します。
End Sub
'UserControl はコンポーネント一覧を消去するために dispose をオーバーライドします。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer
' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
'Windows フォーム デザイナを使って変更してください。
' コード エディタを使って変更しないでください。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
#Region " Members "
Private _x1 As Integer = 0
Private _x2 As Integer = 0
Private _y1 As Integer = 0
Private _y2 As Integer = 0
Private _borderStyle As Integer
Private _borderWidth As Integer = 1
Private _drawMode As Integer
Private margin As Integer = 0
#End Region
#Region " New Propities "
<Category("Line"), Browsable(True)> _
Public Property BorderColor() As System.Drawing.Color
Get
Return Me.ForeColor
End Get
Set(ByVal Value As System.Drawing.Color)
Me.ForeColor = Value
redraw()
End Set
End Property
<Category("Line"), Browsable(True)> _
Public Property BorderStyle() As Integer
Get
Return Me._borderStyle
End Get
Set(ByVal Value As Integer)
Me._borderStyle = Value
redraw()
End Set
End Property
<Category("Line"), Browsable(True), DefaultValue(1)> _
Public Property BorderWidth() As Integer
Get
Return Me._borderWidth
End Get
Set(ByVal Value As Integer)
resetBounds(Me._x1, Me._y1, Me._x2, Me._y2, Value)
End Set
End Property
<Category("Line"), Browsable(True)> _
Public Property DrawMode() As Integer
Get
Return Me._drawMode
End Get
Set(ByVal Value As Integer)
Me._drawMode = Value
redraw()
End Set
End Property
<Category("Line"), Browsable(True)> _
Public Property X1() As Integer
Get
Return Me._x1
End Get
Set(ByVal Value As Integer)
resetBounds(Value, Me._y1, Me._x2, Me._y2, Me._borderWidth)
Me.Invalidate()
End Set
End Property
<Category("Line"), Browsable(True)> _
Public Property X2() As Integer
Get
Return Me._x2
End Get
Set(ByVal Value As Integer)
resetBounds(Me._x1, Me._y1, Value, Me._y2, Me._borderWidth)
Me.Invalidate()
End Set
End Property
<Category("Line"), Browsable(True)> _
Public Property Y1() As Integer
Get
Return Me._y1
End Get
Set(ByVal Value As Integer)
resetBounds(Me._x1, Value, Me._x2, Me._y2, Me._borderWidth)
Me.Invalidate()
End Set
End Property
<Category("Line"), Browsable(True)> _
Public Property Y2() As Integer
Get
Return Me._y2
End Get
Set(ByVal Value As Integer)
resetBounds(Me._x1, Me._y1, Me._x2, Value, Me._borderWidth)
Me.Invalidate()
End Set
End Property