Win64Visual Basic.NET 7.x (2002/03)Visual Basic 10Visual Studio .NET 2002Visual Basic 9 (2008)Visual Basic 8 (2005).NET 1.0Win32Visual Studio 2008.NET 1.1.NET 3.0Visual Studio 2005Visual Studio 2010.NET 2.0.NET 3.5初学者中级Visual Studio.NETVisual Basic
可定制的 WinForms 进度条控件 VB.NET






4.56/5 (6投票s)
一个可定制的进度条控件
引言
当前的 WinForms 进度条控件不包含任何可定制的选项或属性,并且无法继承该控件,因此我们必须自己编写代码。
我参考的 MSDN 示例是 https://support.microsoft.com/en-us/kb/323088。
上面的示例使用了用户控件,但我希望使用类控件,如果您决定使用,当然可以在您的项目中创建用户控件。
我想要一个可以自定义但仍然是进度条的进度条控件,而不是通常不用于应用程序(至少在专业情况下)的随机形状和样式。
Using the Code
该控件是使用 .NET Framework 4.0 创建的。
好的,开始吧。启动一个新的 Visual Studio 项目,并选择“类库”,然后适当地命名它。
现在在解决方案资源管理器中,右键单击项目名称并选择“添加引用”,然后从“.NET”选项卡中,添加以下内容
系统
System.Drawing
System.Windows.Forms
现在让我们导入并开始编码。添加以下内容
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Public Class Progress_bar : Inherits Control
'#Region "Variables" ' UnComment the '#Region lines to make the code collapsible
Private min As Integer = 0
Private max As Integer = 100
Private val As Integer = 0
Public usegrad As Boolean = False
Public bkColor As Color = Color.Transparent
Public enBackColor As Boolean = False
Public BarColor As Color = Color.Blue
Public gradcolor1 As Color = Color.DeepSkyBlue
Public gradcolor2 As Color = Color.Blue
Dim HColor As Color = Color.WhiteSmoke
Dim hStyle As HatchStyle
Dim drawhBrush As Boolean = False
Dim hatchBackColor As Color = Color.Transparent '?
Public drwLabel As Boolean = False
Public draw3D As Boolean = False
'#End Region
'#Region "New/Create Control"
Public Sub New()
MyBase.New()
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
End Sub
Protected Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
End Sub
'#End Region
'#Region"Properties"
Public Property Minimum() As Integer
Get
Return min
End Get
Set(ByVal Value As Integer)
'
If (Value < 0) Then
min = 0
End If
'
If (Value > max) Then
min = Value
min = Value
End If
'
If (Value < min) Then
val = min
End If
'
Me.Invalidate()
'
End Set
End Property
Public Property Maximum() As Integer
Get
Return max
End Get
Set(ByVal Value As Integer)
If (Value < min) Then
min = Value
End If
max = Value
If (val > max) Then
val = max
End If
Me.Invalidate()
End Set
End Property
Public Property Value As Integer
'
Get
Return val
End Get
'
Set(ByVal Value As Integer)
Dim oldvalue As Integer = val
'
If (Value < min) Then
val = min
ElseIf (Value > max) Then
val = max
Else
val = Value
End If
'
Dim newValueRect As Rectangle = Me.ClientRectangle
Dim oldValueRect As Rectangle = Me.ClientRectangle
Dim percent As Decimal
'
percent = (val - min) / (max - min)
newValueRect.Width = newValueRect.Width * percent
'
percent = (oldvalue - min) / (max - min)
oldValueRect.Width = oldValueRect.Width * percent
'
Dim updateRect As Rectangle = New Rectangle()
'
If (newValueRect.Width > oldValueRect.Width) Then
updateRect.X = oldValueRect.Size.Width
updateRect.Width = newValueRect.Width - oldValueRect.Width
Else
updateRect.X = newValueRect.Size.Width
updateRect.Width = oldValueRect.Width - newValueRect.Width
End If
'
updateRect.Height = Me.Height
'
Me.Invalidate(updateRect.X)
End Set
'
End Property
Public Property ProgressBarColor() As Color
Get
Return BarColor
End Get
Set(ByVal Value As Color)
BarColor = Value
Me.Invalidate()
End Set
End Property
Public Property GradientColor1() As Color
Get
Return gradcolor1
End Get
Set(ByVal value As Color)
gradcolor1 = value
Me.Invalidate()
End Set
End Property
Public Property GradientColor2() As Color
Get
Return gradcolor2
End Get
Set(ByVal value As Color)
gradcolor2 = value
Me.Invalidate()
End Set
End Property
Public Property UseHatchBrush() As Boolean
Get
Return drawhBrush
End Get
Set(ByVal value As Boolean)
drawhBrush = value
Me.Invalidate()
End Set
End Property
Public Property HatchColor() As Color
Get
Return HColor
End Get
Set(ByVal value As Color)
HColor = value
Me.Invalidate()
End Set
End Property
Public Property HatchStyle() As HatchStyle
Get
Return hStyle
End Get
Set(ByVal value As HatchStyle)
hStyle = value
Me.Invalidate()
End Set
End Property
Public Property UseGradient() As Boolean
Get
Return usegrad
End Get
Set(ByVal value As Boolean)
usegrad = value
Me.Invalidate()
End Set
End Property
Public Property DrawLabel() As Boolean
Get
Return drwLabel
End Get
Set(ByVal value As Boolean)
drwLabel = value
Me.Invalidate()
End Set
End Property
Public Property Draw3DBorder() As Boolean
Get
Return draw3D
End Get
Set(ByVal value As Boolean)
draw3D = value
Me.Invalidate()
End Set
End Property
Public Shadows Property BackColor() As Color
Get
Return MyBase.BackColor
End Get
Set(ByVal value As Color)
MyBase.BackColor = value
Me.Invalidate()
End Set
End Property
'#End Region
Private Sub Draw_3D_Border(ByVal g As Graphics)
'
Dim penWidth As Integer = Pens.White.Width
g.DrawLine(Pens.DarkGray, New Point(Me.ClientRectangle.Left, _
Me.ClientRectangle.Top), New Point(Me.ClientRectangle.Width - _
penWidth, Me.ClientRectangle.Top))
g.DrawLine(Pens.DarkGray, New Point(Me.ClientRectangle.Left, _
Me.ClientRectangle.Top), New Point(Me.ClientRectangle.Left, _
Me.ClientRectangle.Height - penWidth))
g.DrawLine(Pens.White, New Point(Me.ClientRectangle.Left, _
Me.ClientRectangle.Height - penWidth), New Point(Me.ClientRectangle.Width - _
penWidth, Me.ClientRectangle.Height - penWidth))
g.DrawLine(Pens.White, New Point(Me.ClientRectangle.Width -_
penWidth, Me.ClientRectangle.Top), New Point(Me.ClientRectangle.Width - _
penWidth, Me.ClientRectangle.Height - penWidth))
'
End Sub
Protected Overrides Sub OnResize(ByVal e As EventArgs)
Me.Invalidate() 'Invalidate to resize control
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
'
Dim g As Graphics = e.Graphics
Dim brush As SolidBrush = New SolidBrush(BarColor) ' new solid brush from BarColor
Dim bkbrush As New SolidBrush(MyBase.BackColor) ' new solidbrush from backcolor
Dim hbrush As New HatchBrush(hStyle, _
HColor, Color.Transparent) ' new hatch brush (forecolor, backcolor)
'new lineargradient brush
Dim gbrush As LinearGradientBrush = New LinearGradientBrush_
(Me.ClientRectangle, Color.FromArgb(255, gradcolor1), _
Color.FromArgb(255, gradcolor2), LinearGradientMode.Vertical)
Dim percent As Decimal = (val - min) / (max - min) ' calculate the percent
Dim cliRect As Rectangle = New Rectangle(0, 0, _
Me.ClientRectangle.Width, Me.ClientRectangle.Height) ' Control rectangle
Dim rect As Rectangle = New Rectangle(1, 1, _
Me.ClientRectangle.Width - 2, Me.ClientRectangle.Height - 2) ' Progress rectangle
'
rect.Width = rect.Width * percent
'
' Smoothing and quality mode
g.SmoothingMode = SmoothingMode.AntiAlias
g.CompositingQuality = CompositingQuality.AssumeLinear
'
If UseGradient = True Then
g.FillRectangle(bkbrush, cliRect) ' enable back color
g.FillRectangle(gbrush, rect) ' Use Gradient
If drawhBrush = True Then
g.FillRectangle(hbrush, rect) ' draw hatch brush
End If
End If
'
If UseGradient = False Then
g.FillRectangle(bkbrush, cliRect)
g.FillRectangle(brush, rect)
If drawhBrush = True Then
g.FillRectangle(hbrush, rect)
End If
' g.DrawRectangle(Pens.Black, cliRect) ' Draw a single Border
End If
'
If draw3D = True Then
Draw_3D_Border(g) ' draw the 3d border using g as graphics
End If
'
If DrawLabel = True Then
Dim str As String = CType(val & "%", String) ' convert val into a string
Dim mybrush As New SolidBrush(Me.ForeColor) ' make ForeColor as brush
g.DrawString(str, Me.Font, mybrush, _
New Point((Me.ClientRectangle.Width / 2) - str.Length * 4, _
(Me.ClientRectangle.Height / 2) - (FontHeight / 2))) ' Draw string
End If
'
brush.Dispose()
bkbrush.Dispose()
hbrush.Dispose()
gbrush.Dispose()
g.Dispose()
'
End Sub
End Class
可以实现的一些样式如下所示
待办事项
- 圆角(我拥有的代码无法正常工作)
- 可选择区域
- 动画描线画笔