Powerbuilder – Get Color Value of Pixel under Pointer

Posted on Friday, December 17th, 2010 at 8:20 pm in

Here is a sample application which will give you the color value of the pixel the user clicked on on a picture control (this can be used for any object inherited from a dragobject). To see the list of draggable controls, open the Browser. All the objects in the hierarchy below dragobject are draggable. This functionality can be used to ‘prettyfy’ a report selection window by providing an irregular bitmap (sales region map, delivery routes, etc.) within which the user clicks. Depending upon the color they choose, a separate report is produced.

Anyway, the functionality is encapsulated in an nvo and involves three API external functions.

FUNCTION ulong GetPixel(ulong hwnd, long xpos, long ypos) LIBRARY "Gdi32.dll"
// get device context - required for graphic stuff
FUNCTION ulong GetDC(long hwnd) LIBRARY "user32.dll"
FUNCTION long ReleaseDC (long hwnd, ulong hDC) LIBRARY "user32.dll"

The function is called of_get_color. It accepts a dragobject by reference.

long ll_xpos, ll_ypos
ulong ul_rtn
ulong ul_handle, ul_device
// get handle
ul_handle = handle(a_object)
ul_device = GetDC(ul_handle)
// get position of pointer
ll_xpos = UnitsToPixels(a_object.PointerX(), XUnitsToPixels!)
ll_ypos = UnitsToPixels(a_object.PointerY(), YUnitsToPixels!)

ul_rtn = GetPixel(ul_device, ll_xpos, ll_ypos)
ReleaseDC( ul_handle, ul_device)

RETURN ul_rtn

On the window there is an event which is triggered when the user clicks on an appropriate object.

int li_r, li_b, li_g
ulong lul_color
n_pixelcolor lnv_color
lul_color =  lnv_color.of_get_color(a_object)
//max value of unsigned long indicates error
IF lul_color = 4294967295 THEN
	r_1.fillcolor = 0
	st_ul.text = 'Invalid Color'
	st_rgb.text = ''
// get RGB values
li_r = mod(lul_color,256)
li_g = mod((lul_color / 256), 256)
li_b = mod ((lul_color / 65536), 256)

st_ul.text = 'U Long: ' + string(lul_color)
st_rgb.text = 'RGB( ' + string(li_r) + ', ' + string(li_g) + ', ' + string(li_b) + ')'
st_rgb.textcolor = RGB(li_r,li_g,li_b) //just to prove the values are correct
r_1.fillcolor = lul_color

The window displays the color value as well as its derived RGB values.

Pixel Color App

Color clicked on.

The code is in PB11.5.1. A zip file with the .pbl and appropriate exports can be found here.

Special thanks to Roland Smith of TeamSybase.

You might also be interested in

Add your comment

Leave a Reply