PB中所有的单位都是以PBU为单位,唯独数据窗口提供了4种单位,分别为:PBU、像素1/1000逻辑英寸、1/1000逻辑厘米,相信所有PB的开发人员都知道这一点,但很少人去处理它。
为什么要进行单位转换?就是要让开发人员,不管该数据是什么单位类型的,处理过程中都是一样的,例如自定义报表,有些报表用到的PBU,有些报表用到的是PX;不管理什么类型的数据窗口,用户看到的效果都是一样的,例如用户打印,有些用户想要使用CM为单位,有些用户喜欢使用Inch为单位。有人会说,开发时统一一种单位就可以了,是的,那是在一种理想的情况下,然而很多时候都是不理想的。
我曾经看过好多系统,就连一些称之为框架的,既然称之为框架那就得做得通用一点,但是我在用它做开发的时候就处处受阻了,最多的还是用户的报表方面。
下面我就说说我所做的开发中需要使用到单位转换的方:
1、各种工具界面,例如我之前发表过的《纯PB制作dw按钮》、《纯PB制作dw工具栏》、《纯PB制作dwTab控件》等等,因为涉及的图片的切片,宽度、高度的计算,我比较喜欢使用PX为单位进行开发,这样会方便很多,这是大部分的,还而还有一些小部分,脱离了窗口,那就只能进行单位转换了。
2、自定义报表,如果做了单位转换,那么客户或者实施人员在调整报表、单据的时候就不是出现拖动控件、区带时出现走位了,因为开发人员总是用了不同单位来设计。之前有几次自己帮客户调单据,自己都调得想哭了。
3、打印,用户打印时设置纸张、边距时一般都会用到CM和Inch,因为他们根本就不懂得PBU是什么、像素有多大,无论用户选择什么单位,都可以让它正确地显示和打印出来。
4、临时还想不了那么多。
如何转换,不说详解,直接上代码。
注意:以下算法是在Windows、96DPI下的转换,一般情况下不会出现任何问题。如果需要适应多DPI那就要使用API去处理了。
个人觉得已经足够使用了,适应多DPI的就不放出了。
01 |
//两个方向的常量 |
02 |
constant integer II_X_HORIZONTAL = 0 //横向 |
03 |
constant integer II_Y_VERTICAL = 1 //竖向 |
01 |
//PBU转换成其他 |
02 |
of_pbu2other(al_from, ai_unittype, ai_xy) |
03 |
choose case ai_unittype |
04 |
case 0 //PBU |
05 |
return al_from |
06 |
case 1 //像素 |
07 |
if ai_xy = II_X_HORIZONTAL then |
08 |
return UnitsToPixels(al_from, XUnitsToPixels!) |
09 |
else |
10 |
return UnitsToPixels(al_from, YUnitsToPixels!) |
11 |
end if |
12 |
case 2 //1/1000逻辑英寸 |
13 |
if ai_xy = II_X_HORIZONTAL then |
14 |
return round(UnitsToPixels(al_from, XUnitsToPixels!) * 0.3937007 / 0.0377953, 0) |
15 |
else |
16 |
return round(UnitsToPixels(al_from, YUnitsToPixels!) * 0.3937007 / 0.0377953, 0) |
17 |
end if |
18 |
case 3 //1/1000逻辑厘米 |
19 |
if ai_xy = II_X_HORIZONTAL then |
20 |
return round(UnitsToPixels(al_from, XUnitsToPixels!) / 0.0377953, 0) |
21 |
else |
22 |
return round(UnitsToPixels(al_from, YUnitsToPixels!) / 0.0377953, 0) |
23 |
end if |
24 |
end choose |
25 |
26 |
return 0 |
01 |
//像素转换成其他 |
02 |
of_px2other(al_from, ai_unittype, ai_xy) |
03 |
choose case ai_unittype |
04 |
case 0 //PBU |
05 |
if ai_xy = II_X_HORIZONTAL then |
06 |
return PixelsToUnits(al_from, XPixelsToUnits!) |
07 |
else |
08 |
return PixelsToUnits(al_from, YPixelsToUnits!) |
09 |
end if |
10 |
case 1 //像素 |
11 |
return al_from |
12 |
case 2 //1/1000逻辑英寸 |
13 |
return round(al_from * 0.3937007 / 0.0377953, 0) |
14 |
case 3 //1/1000逻辑厘米 |
15 |
return round(al_from / 0.0377953, 0) |
16 |
end choose |
17 |
18 |
return 0 |
01 |
//1/1000逻辑英寸转换成其他 |
02 |
of_inch2other(al_from, ai_unittype, ai_xy) |
03 |
choose case ai_unittype |
04 |
case 0 //PBU |
05 |
if ai_xy = II_X_HORIZONTAL then |
06 |
return PixelsToUnits(round(al_from * 2.5399998 * 0.0377953, 0), XPixelsToUnits!) |
07 |
else |
08 |
return PixelsToUnits(round(al_from * 2.5399998 * 0.0377953, 0), YPixelsToUnits!) |
09 |
end if |
10 |
case 1 //像素 |
11 |
return round(al_from * 2.5399998 * 0.0377953, 0) |
12 |
case 2 //1/1000逻辑英寸 |
13 |
return al_from |
14 |
case 3 //1/1000逻辑厘米 |
15 |
return round(al_from * 2.5399998, 0) |
16 |
end choose |
17 |
18 |
return 0 |
01 |
//1/1000逻辑厘米 |
02 |
of_cm2other(al_from, ai_unittype, ai_xy) |
03 |
choose case ai_unittype |
04 |
case 0 //PBU |
05 |
if ai_xy = II_X_HORIZONTAL then |
06 |
return PixelsToUnits(round(al_from * 0.0377953, 0), XPixelsToUnits!) |
07 |
else |
08 |
return PixelsToUnits(round(al_from * 0.0377953, 0), YPixelsToUnits!) |
09 |
end if |
10 |
case 1 //像素 |
11 |
return round(al_from * 0.0377953, 0) |
12 |
case 2 //1/1000逻辑英寸 |
13 |
return round(al_from * 0.3937007, 0) |
14 |
case 3 //1/1000逻辑厘米 |
15 |
return al_from |
16 |
end choose |
17 |
18 |
return 0 |
另外还有几个of_other2pbu,of_other2px,of_other2inch,of_other2cm,of_other2other就不写了。
此文为《纯PB制作dw按钮》、《纯PB制作dw工具栏》、《纯PB制作dwTab控件》等等的制作提供基础。
==================================================
语言表达得非常不好,从小语文、写作就不好,到现在也不怎么提高,希望能让大家看得过去。