VB编写一个能显示百分比的自定义进度条控件
2024-07-21 02:20:57
供稿:网友
运行效果:
设计方法:
1.在usercontrol中添加一个label控件label1,将它设为平面,用来做外框。添加两个picturebox控件picturebox1做为进度指示,picturebox2控件做为控件背景。
2.加入以下代码
option explicit
'定义私有变量用于存储属性值
private mvarmax as long
private mvarmin as long
private mvarvalue as long
private rate as string
private sub usercontrol_initialize()
'初始化
picture2.backcolor = vbblue
end sub
public property get backcolor() as ole_color
'读取backcolor属性
backcolor = picture1.backcolor
end property
public property let backcolor(byval vnewvalue as ole_color)
'设置backcolor属性
picture1.backcolor = vnewvalue
end property
private sub usercontrol_initproperties()
'初始化属性
max = 100
min = 0
value = 0
end sub
private sub usercontrol_readproperties(propbag as propertybag)
'读取从属性窗体中设置的属性值
mvarmax = propbag.readproperty("max", 100)
mvarmin = propbag.readproperty("min", 0)
'value属性值这里未提供,主要是模仿vb自带的进度条控件
'mvarvalue = propbag.readproperty("value", 0)
end sub
private sub usercontrol_writeproperties(propbag as propertybag)
'保存从属性窗体中设置的属性值
propbag.writeproperty "max", mvarmax, 100
propbag.writeproperty "min", mvarmin, 0
'propbag.writeproperty "value", mvarvalue, 0
end sub
private sub usercontrol_resize()
'resize事件
label1.move 0, 0, usercontrol.width / screen.twipsperpixelx, usercontrol.height / screen.twipsperpixely
picture1.move 1, 1, usercontrol.width / screen.twipsperpixelx - 2, usercontrol.height / screen.twipsperpixely - 2
picture2.move 1, 1, 1, usercontrol.height / screen.twipsperpixely - 2
end sub
public property get max() as long
'读取max属性
max = mvarmax
end property
public property let max(byval vnewvalue as long)
'设置max属性
mvarmax = vnewvalue
if vnewvalue < min then err.raise "1001", , "max必须大于min"
end property
public property get min() as long
'读取min属性
min = mvarmin
end property
public property let min(byval vnewvalue as long)
'设置min属性
if vnewvalue > max then err.raise "1000", , "min必须小于max"
mvarmin = vnewvalue
end property
public property get value() as long
'读取value属性
value = mvarvalue
end property
public property let value(byval vnewvalue as long)
'设置value属性
'原理就是在两个picturebox中以不同颜色打印百分比进度
dim dx as long, dy as long
if vnewvalue > max then err.raise "1002", , "value不能大于max"
mvarvalue = vnewvalue
picture2.width = value / (max - min) * (usercontrol.width / screen.twipsperpixelx - 2)
rate = int(value / (max - min) * 100) & "%"
dx = (picture1.width - picture1.textwidth(rate)) / 2
dy = (picture1.height - picture1.textheight(rate)) / 2
picture1.forecolor = vbblack
picture2.forecolor = vbwhite
if dx < picture2.width then
picture2.cls
picture2.currentx = dx
picture2.currenty = dy
picture2.print rate
else
picture1.cls
picture1.currentx = dx
picture1.currenty = dy
picture1.print rate
end if
end property
3.新建另一个测试工程,加入一个自己的进度条控件和一个系统的进度条控件,加入以下代码:
option explicit
private sub command1_click()
unload me
end sub
private sub timer1_timer()
myprogressbar1.value = myprogressbar1.value + 2
progressbar1.value = progressbar1.value + 2
if myprogressbar1.value = myprogressbar1.max then timer1.enabled = false
end sub
ok.运行看看效果吧。