Choose a Color, 6 sep 1999

Back Up Next

Author: Eric Aling
Accessed:
Download: Download ChooseColor.zip ( choosecolor 6Kb ):

This PB zipfile allows you to call the standard windows dialog box for choosing a color. Besides that, you can add custom colors which will be stored in an array of 16 long values, and you can start the dialog box with an initial selected color. This functonality is based on the windows API function ChooseColor, which accepts an CHOOSECOLOR structure as argument.

Also the external functions GlobalAlloc, GlobalLock, Globalunlock and GlobalFree are used for stored the custom colors in a piece of memory. Secondly, the RtlMoveMemory function is used ( an alias for CopyMemory ) to move this piece of memory to our array of 16 long values and vice versa. Although this is pretty basic stuff, you can learn a lot of it for other purposes. Especially with the allocation and copymemory functions, you can do many thing like working with pointers to pieces of memory or moving memory to structures. Check out the code for the ChooseColor function ( boolean wf_ChooseColor ( ref long al_rgbresult ):

str_ChooseColor     lstr_ChooseColor
boolean             lb_ok
ulong               lul_hmem

// Set initialul_hMemcolor 
lstr_ChooseColor.rgbresult = al_rgbresult

// Flag = RGBInit 
lstr_choosecolor.flags = 1

// No owner necessary
SetNull(lstr_choosecolor.hwndOwner)

// Allocate memory for custom colors, 64 bytes ( 16 * 4 long values ) */
lul_hMem = globalAlloc(2,64)
lul_hMem = globallock (lul_hMem)

// copy custum colors to memory, il_Cust is defined as long[16] 
CopyMemory(lul_hMem,il_Cust,64)

// and assign to structure for further use by function
lstr_ChooseColor.lpCustColors = lul_hMem

// finally, set structure size
lstr_choosecolor.lStructSize = 36

// call function
lb_ok = ChooseColorA ( lstr_choosecolor )

// copy result into variables
if lb_ok then
    al_rgbresult = lstr_ChooseColor.rgbresult 
    CopyMemory(il_Cust,lul_hMem,64)
end if

// free used resources (custom color reside in instance array 
globalunlock(lul_hMem)
globalfree(lul_hMem)

return lb_ok

Download the example and run it.

Good luck!

 

Back Up Next