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.運行看看效果吧。