自己编写vb进度条控件

简介: 自己编写vb进度条控件

image.png

主程序代码:

Option Explicit
Dim i As Double
Private Sub Command1_Click()
    i = 0
    Timer1.Interval = 50
    Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
    i = i + 0.05
    ProcessBar1.Percent = i
    If i > 1 Then Timer1.Enabled = False
End Sub

image.png

控件代码:

Option Explicit
Public Enum FigureStyle
    eOnlyValueInt
    eOnlyValueDouble
    ePercentInt
    ePercentDouble
End Enum
Private dblMyPercent As Double
Private lngMyValue As Long
Private isShowMsg As Boolean
Private strMsg As String
Private intMsgStyle As FigureStyle
Private Max As Long, Min As Long
Private Sub UserControl_Initialize()
    Min = 0
    Max = 100
'    lngMyValue = 30
    dblMyPercent = lngMyValue / (Max - Min)
    lblValue.Caption = Int(dblMyPercent * 100) & "/100"
    UserControl.Height = lblBase.Height
    intMsgStyle = ePercentInt
    Call setLabelSize
End Sub
Private Sub UserControl_Resize()
    Call setLabelSize
End Sub
Private Sub setLabelSize()
    lblBase.Move 0, 0, UserControl.ScaleWidth, UserControl.ScaleHeight
    lblTop.Move lblBase.Left, lblBase.Top, dblMyPercent * lblBase.Width, lblBase.Height
    lblValue.Move lblBase.Left, (lblBase.Height - lblValue.Height) / 2 + 20, lblBase.Width
End Sub
'set the value
Public Property Let Value(ByVal Value As Long)
    If Value < Min Then
        lngMyValue = Min
    ElseIf Value > Max Then
        lngMyValue = Max
    Else
        lngMyValue = Value
    End If
    dblMyPercent = lngMyValue / (Max - Min)
    lblTop.Width = dblMyPercent * lblBase.Width
    Select Case intMsgStyle
        Case eOnlyValueInt: strMsg = Int(dblMyPercent * 100)
        Case eOnlyValueDouble: strMsg = CStr(dblMyPercent * 100)
        Case ePercentInt: strMsg = Int(dblMyPercent * 100) & "/100"
        Case ePercentDouble: strMsg = CStr(dblMyPercent * 100) & "/100"
    End Select
    lblValue.Caption = strMsg
End Property
'
Public Property Get Value() As Long
    Value = lngMyValue
End Property
'set the percent
Public Property Let Percent(ByVal Percent As Double)
    If Percent < 0 Then
        dblMyPercent = 0
    ElseIf Percent > 1 Then
        dblMyPercent = 1
    Else
        dblMyPercent = Percent
    End If
    lngMyValue = dblMyPercent * (Max - Min)
    lblTop.Width = dblMyPercent * lblBase.Width
    Select Case intMsgStyle
        Case eOnlyValueInt: strMsg = Int(dblMyPercent * 100)
        Case eOnlyValueDouble: strMsg = CStr(dblMyPercent * 100)
        Case ePercentInt: strMsg = Int(dblMyPercent * 100) & "/100"
        Case ePercentDouble: strMsg = CStr(dblMyPercent * 100) & "/100"
    End Select
    lblValue.Caption = strMsg
End Property
Public Property Get Percent() As Double
    Percent = dblMyPercent
End Property
'is show msg
Public Property Let ShowMsg(ByVal ShowMsg As Boolean)
    isShowMsg = ShowMsg
    lblValue.Visible = isShowMsg
End Property
Public Property Get ShowMsg() As Boolean
    ShowMsg = isShowMsg
End Property
'msg style
Public Property Let MsgStyle(ByVal MsgStyle As FigureStyle)
    intMsgStyle = MsgStyle
    Select Case intMsgStyle
        Case eOnlyValueInt: strMsg = Int(dblMyPercent * 100)
        Case eOnlyValueDouble: strMsg = CStr(dblMyPercent * 100)
        Case ePercentInt: strMsg = Int(dblMyPercent * 100) & "/100"
        Case ePercentDouble: strMsg = CStr(dblMyPercent * 100) & "/100"
    End Select
    lblValue.Caption = strMsg
End Property
Public Property Get MsgStyle() As FigureStyle
    MsgStyle = intMsgStyle
End Property

以下为运行状态:

image.png

image.png

整个工程下载:GitHub - sysdzw/vbprocessbar: vb进度条控件

目录
相关文章
零基础VB教程063期:如何用代码直接创建控件?从此不必拖控件了
零基础VB教程063期:如何用代码直接创建控件?从此不必拖控件了
325 0
|
图形学
零基础VB教程046期:如何制作自己的记事本第1节?toolbar控件与imagelist控件
零基础VB教程046期:如何制作自己的记事本第1节?toolbar控件与imagelist控件
110 0
|
SQL Java 数据库
零基础VB教程041期:如何使用滚动条?
零基础VB教程041期:如何使用滚动条?
106 0
VB编程:代码实现窗体居中显示-59
VB编程:代码实现窗体居中显示-59
219 0
VB编程:利用控件数组设置控件状态-38
VB编程:利用控件数组设置控件状态-38
104 0
|
人工智能
VB编程:自定义过程改变窗体颜色-53
VB编程:自定义过程改变窗体颜色-53
190 0
VB编程:代码实现窗体居中显示
VB编程:代码实现窗体居中显示
290 0
VB编程:代码实现窗体居中显示
VB编程:利用控件数组设置控件状态
VB编程:利用控件数组设置控件状态
116 0
VB编程:利用控件数组设置控件状态
|
API 数据安全/隐私保护
VB编程:无标题栏窗体移动和自定义鼠标样式
VB编程:无标题栏窗体移动和自定义鼠标样式
254 0
|
程序员 C语言
Qt编写自定义控件48-面板窗体控件
一、前言 很多时候需要有一个控件,能够替代容器控件,自动容纳多个widget,自适应宽高,然后提供滚动条功能,这就必然需要用到QScrollArea控件,可设置各个子面板的间距等,也在很多系统中用到,比如温湿度设备面板,有几百个温湿度设备,需要一个容器放置,自动产生滚动条,可以设置面板的固定宽高或者自适应拉伸,其实就是放了表格布局+弹簧来设置。
1284 0