Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ChooseColor API function instead of Clarion ColorDialog()
!Clarion's ColorDialog() does not have the deatures of the API ChooseColor like saving the 16 custom colors
!From https://www.icetips.com/showarticle.php?articleid=272
!A few changes to Larry's code by Carl:
! Set cc.rgbResult = W{PROP:Color} before call. Should CC_RGBINIT be conditional?
! Check for ChooseColor returning True with IF ccBool THEN .
! Comment UnlockThread.
! Prototype a (LONG cc) so no need for type at Map level. Add DLL(1)
!-----------------------------------------
!Windows API: Saving and restoring custom colors for ColorDialog
!2003-09-10 -- Larry Sand
!Newsgroups: softvelocity.products.c55ee
!============================================
! Using the ChooseColor common dialog in Clarion
! Written by Larry Sand
!============================================
PROGRAM
CC_RGBInit EQUATE(00000001h) !Use the color specified in the rgbResult member as the initial color selection.
CC_FullOpen EQUATE(00000002h) !Display the additional controls that allow the user to create custom colors. If this flag is not set, the user must click the Define Custom Color button to display the custom color controls.
CC_PreventFullOpen EQUATE(00000004h) !Disables the Define Custom Color button.
CC_ShowHelp EQUATE(00000008h) !Dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button.
CC_EnableHook EQUATE(00000010h) !Enables the hook procedure specified in the lpfnHook member of this structure. This flag is used only to initialize the dialog box.
CC_EnableTemplate EQUATE(00000020h) !The hInstance and lpTemplateName members specify a dialog box template to use in place of the default template. This flag is used only to initialize the dialog box.
CC_EnableTemplateHandle EQUATE(00000040h) !The hInstance member identifies a data block that contains a preloaded dialog box template. The system ignores the lpTemplateName member if this flag is specified. This flag is used only to initialize the dialog box.
CC_SolidColor EQUATE(00000080h) !Causes the dialog box to display only solid colors in the set of basic colors.
CC_AnyColor EQUATE(00000100h) !Display all available colors in the set of basic colors.
_CHOOSECOLOR GROUP,TYPE
lStructSize UNSIGNED !The length, in bytes, of the structure. =SIZE()
hWndOwnder UNSIGNED !A handle to the window that owns the dialog box. This member can be any valid window handle, or it can be NULL if the dialog box has no owner.
hInstance UNSIGNED !If the CC_ENABLETEMPLATEHANDLE flag is set in the Flags member, hInstance is a handle to a memory object containing a dialog box template.
rgbResult LONG !If the CC_RGBINIT flag is set, rgbResult specifies the color initially selected when the dialog box is created. If the specified color value is not among the available colors, the system selects the nearest solid color available. If rgbResult is zero or CC_RGBINIT is not set, the initially selected color is black. If the user clicks the OK button, rgbResult specifies the user's color selection.
lpCustColors LONG !A pointer to an array of 16 values that contain red, green, blue (RGB) values for the custom color boxes in the dialog box. If the user modifies these colors, the system updates the array with the new RGB values. To preserve new custom colors between calls to the ChooseColor function, you should allocate static memory for the array
Flags UNSIGNED !A set of bit flags that you can use to initialize the Color dialog box. When the dialog box returns, it sets these flags to indicate the user's input. This member can be a combination of CC_Xxxx flags.
lCustData LONG !Application-defined data that the system passes to the hook procedure identified by the lpfnHook member. When the system sends the WM_INITDIALOG message to the hook procedure, the message's lParam parameter is a pointer to the CHOOSECOLOR structure specified when the dialog was created. The hook procedure can use this pointer to get the lCustData value.
lpfnHook LONG !A pointer to a CCHookProc hook procedure that can process messages intended for the dialog box. This member is ignored unless the CC_ENABLEHOOK flag is set in the Flags member.
lpszTemplateName LONG !The name of the dialog box template resource in the module identified by the hInstance member. This template is substituted for the standard dialog box template. For numbered dialog box resources, lpTemplateName can be a value returned by the MAKEINTRESOURCE macro. This member is ignored unless the CC_ENABLETEMPLATE flag is set in the Flags member.
END
NCustomColors EQUATE(16) !Array of Custom Colors DIM(16)
MAP
MODULE('Win32Api')
ChooseColor(*_CHOOSECOLOR lpcc),BOOL,PASCAL,RAW,NAME('ChooseColorA'),PROC,DLL(1)
ChooseColor(LONG Address_CHOOSECOLOR),BOOL,PASCAL,RAW,NAME('ChooseColorA'),PROC,DLL(1)
END
END
W WINDOW('Using the ChooseColor dialog Win32'),AT(,,277,179),SYSTEM,FONT('Segoe UI',10)
BUTTON('Choose Color'),AT(9,12,,14),USE(?ChooseColorButton)
PROMPT('This example saves the 16 custom colors between calls to an array. To make this real' & |
'ly work you would need to save the Custom Colors to the Registry or an INI.'), |
AT(9,38,217,38),USE(?Prompt1:FYI)
END
cc LIKE(_CHOOSECOLOR)
ccBool BOOL
aCustomColors LONG,DIM(NCUSTOMCOLORS),AUTO
i LONG,AUTO
hwndW UNSIGNED,AUTO
Code
Open(W)
hwndW = W{PROP:Handle}
!------------------------------------------------------
!First inintialize the custom color array to all White.
!Or load values you saved in the Registry or INI.
!------------------------------------------------------
LOOP i = 1 TO NCUSTOMCOLORS
aCustomColors[i] = COLOR:White
END
ACCEPT
IF FIELD() = ?ChooseColorButton
IF EVENT() = EVENT:Accepted
!-------------------------------------------
! Initialize the CHOOSECOLOR structure. See
! MSDN for all of the options. Here, I'm telling
! the color dialog to select and display the color
! specified in cc.rgbResult and show the expanded
! dialog for the custom color picker.
!-------------------------------------------
CLEAR(cc)
cc.lStructSize = SIZE(cc)
cc.hwndOwnder = hwndW
IF W{PROP:Color} <> COLOR:None THEN !Carl Added
cc.rgbResult = W{PROP:Color}
END
cc.Flags = BOR(CC_RGBINIT, CC_FULLOPEN )
cc.lpCustColors = ADDRESS(aCustomColors)
!-------------------------------------------
! Before calling the common dialog you must call unlockthread in Clarion versions < C6
!-------------------------------------------
!UNLOCKTHREAD()
ccBool=ChooseColor(cc) !ChooseColor returns 0 when you click the Cancel button
!LOCKTHREAD()
! Show the result by changing the client area to the selected color
IF ccBool = True THEN
W{PROP:Color} = cc.rgbResult
END
END
END
END
RETURN
!Exact code from https://www.icetips.com/showarticle.php?articleid=272
!Larry's original post
!============================================
! Using the ChooseColor common dialog in Clarion
! Written by Larry Sand
!============================================
PROGRAM
CC_RGBINIT EQUATE(00000001h)
CC_FULLOPEN EQUATE(00000002h)
CC_PREVENTFULLOPEN EQUATE(00000004h)
CC_SHOWHELP EQUATE(00000008h)
CC_ENABLEHOOK EQUATE(00000010h)
CC_ENABLETEMPLATE EQUATE(00000020h)
CC_ENABLETEMPLATEHANDLE EQUATE(00000040h)
CC_SOLIDCOLOR EQUATE(00000080h)
CC_ANYCOLOR EQUATE(00000100h)
_CHOOSECOLOR GROUP,TYPE
lStructSize UNSIGNED
hwndOwnder UNSIGNED
hInstance UNSIGNED
rgbResult LONG
lpCustColors LONG
Flags UNSIGNED
lCustData LONG
lpfnHook LONG
lpszTemplateName LONG
END
NCUSTOMCOLORS EQUATE(16)
MAP
MODULE('Win32Api')
ChooseColor(*_CHOOSECOLOR lpcc|
), BOOL, PASCAL, RAW, NAME('ChooseColorA'), PROC
END
END
W WINDOW('Using the ChooseColor dialog Win32'),AT(,,277,179),|
FONT('MS Sans Serif',8,,),SYSTEM
BUTTON('Choose Color'),AT(9,12,55,14),USE(?ChooseColorButton)
END
cc LIKE(_CHOOSECOLOR)
aCustomColors LONG,DIM(NCUSTOMCOLORS),AUTO
i LONG,AUTO
hwndW UNSIGNED,AUTO
Code
Open(W)
hwndW = W{PROP:Handle}
!-------------------------------------------
!First inintialize the custom color array to
!all white. Or use your serialized values.
!-------------------------------------------
LOOP i = 1 TO NCUSTOMCOLORS
aCustomColors[i] = COLOR:White
END
ACCEPT
IF FIELD() = ?ChooseColorButton
IF EVENT() = EVENT:Accepted
!-------------------------------------------
! Initialize the CHOOSECOLOR structure. See
! MSDN for all of the options. Here, I'm telling
! the color dialog to select and display the color
! specified in cc.rgbResult and show the expanded
! dialog for the custom color picker.
!-------------------------------------------
cc.lStructSize = SIZE(cc)
cc.hwndOwnder = hwndW
cc.Flags = BOR(CC_RGBINIT, CC_FULLOPEN )
cc.lpCustColors = ADDRESS(aCustomColors)
!-------------------------------------------
! Before calling the common dialog you must
! call unlockthread in Clarion versions < C6
!-------------------------------------------
UNLOCKTHREAD()
ChooseColor(cc) !ChooseColor returns 0 when you click the Cancel button
LOCKTHREAD()
! Show the result by changing the client area to the selected color
W{PROP:Color} = cc.rgbResult
END
END
END
RETURN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment