PowerBuilder ‘Gotcha’ – PixelsToUnits

Posted on Wednesday, September 7th, 2011 at 8:20 pm in

From the Powerbuilder Help on this method:
Converts pixels to PowerBuilder units. Because pixels are not usually square, you also specify whether you are converting the pixels’ horizontal or vertical measurement.


PixelsToUnits ( pixels, type )

Argument Description
pixels An integer whose value is the number of pixels you want to convert to PowerBuilder units.
type A value of the ConvertType enumerated datatype value indicating how to convert the value:
· XPixelsToUnits! – Convert the pixels in the horizontal direction.
· YPixelsToUnits! – Convert the pixels in the vertical direction.
Return value

Integer. Returns the converted value if it succeeds and -1 if an error occurs. If any argument’s value is null, PixelsToUnits returns null.

Note the return value of integer. If you are using this method to say, calculate the width of a string field, the results can easily exceed the maximum value of a Powerbuilder integer which is 32767. To correct for this you will need to do something like this.

integer li_width, li_width_factor
long ll_width
// calculate the width of a single pixel
li_width_factor = PixelsToUnits(1, XPixelsToUnits!) 
// use the calculated factor to obtain the actual width of a very wide string column
ll_width = li_width * li_width_factor