利用PB数据窗口特征制作进度条

PowerBuilder的6.5以前版本没有提供进度条,因此大多数程序员总是自己在程序中利用两个statictext控件进行编程产生进度效果。但是由于包括statictext控件在内的所有窗口内使用的控件没有透明色即transparent,进度条的文字显示无法与进度条进行重叠,只得在进度条外的其它位置放置一statictext控件用以显示进度情况,这样即浪费了空间,又不美观。

但我们知道,数据窗口内的静态文本的字体属性是有transparent(透明)的,笔者通过研究PowerBuilder的数据窗口控件特征,摸索出一利用数据窗口等制作进度条的方法,充分解决了以上难题,还能任意改变宽度及背景、伸缩块及文字的颜色,并封装成了用户对象以方便调用。下面就介绍这一方法。

1、首先建立一数据源为Enternal(外部)、风格为Freefrom的数据窗口d_schedule_bar,输入一个字段。确定后在数据窗口画板中删除该字段,再增加两个statictext控件,这样数据窗口加上字段标识的那个一共三个statictext控件,分别命名为:st_back、st_move、st_text,调整使其宽、高度相等,设st_back的background.color为ButtonFace,st_move的background.color为Teal,st_text的background.color为transparent,st_text的color为black。表述如下:

{$$ps$$}

控件名 背景色 文字色 x y 高度 宽度
St_back ButtonFace ---- 5 8 76 969
St_move Teal ---- 5 8 76 969
St_test Transparent black 5 8 76 969
{$$pe$$}

保存之前选择st_move,从Edit菜单中选择Bring to Front,再选择st_text重复上述操作,使三个 statictext控件的位置层从外向里按st_text、st_move、st_back排序,以防被显示屏蔽(这一点非常重要!)。

2、新建一可视定制用户对象uo_ schedule_bar,以其中放置一数据窗口dw_1,数据源为d_schedule_bar。定义两个int实例变量 ii_percent、ii_width。

下面就开始骗程吧!

(1)编写函数uf_setwidth(integer ai_width)

参数及类型:integer ai_width

作用:改变进度条宽度:

返回值:无

函数体:

IF ai_width > 0 THEN

ii_width = ai_width

dw_1.resize (ai_width + 15,dw_1.height)

dw_1.modify ( "st_back.width=" + string(ai_width) )

dw_1.modify ( "st_text.width=" + string(ai_width) )

this.resize ( ai_width + 50, this.height )

END IF

(2)编写函数uf_schedule_move(integer ai_schedule)

参数及类型:integer ai_schedule

作用:显示进度

返回值:无

函数体:

int li_AllWidth,li_schedule

IF ai_schedule > 100 THEN

ai_schedule = 100

ii_percent = 100

END IF

IF ai_schedule <0 THEN ai_schedule="0" END IF ii_percent="ai_schedule" li_AllWidth="integer(dw_1.describe" ( "st_back.width" )) IF ai_schedule>= 0 THEN

li_schedule = li_AllWidth * (ai_schedule / 100)

END IF

dw_1.modify ( "st_move.width=" + string(li_schedule) )

dw_1.modify ( "st_text.text='" + string (ai_schedule) + "%'" )

(3)编写三个颜色控制函数以改变背景、伸缩条、文字的颜色。

函数名:uf_change_back_color

参数:double a_color

函数体:

dw_1.modify ( "st_back.background.color = "+string(a_color))

函数名:uf_change_move_bar_color

参数:double a_color

函数体:

dw_1.modify ( "st_move.background.color = "+string(a_color))

函数名:uf_change_text_color

参数:double a_color

函数体:

dw_1.modify ( "st_text.color = "+string(a_color))

(4)在constructor事件中输入以下代码:

dw_1.insertrow (0)

this.uf_schedule_move ( 0 )

ii_width = this.width

好了,一个标准的进度条用户对象就封象好了!在程序中任意地使用吧!