Sign in to follow this  
Followers 0
step887

Modified _ChooseColor to include custom colors

1 post in this topic

So I am slowly trying to learn everything and I ran accross _ChooseColor and for some reason it bugged me that the custom colors were always black..So I figured, I will try to learn how to change that .. and this what I came up with.. I used Gary Frost's code from Misc.au3 and just made changes to be able to put in custom colors and get them back out..

It seems to work for me and I though I share in case anyone else was bugged by the custom colors ;)

o I had to move a couple global const under the function because .. I wanted to put my example at the top of the script

Example:

$colors = IniRead('test.ini','test','colors',0)
$out = _ChooseColor(2,4964882,0,0,$colors); will be all black until some are defined
If $out <> -1 Then IniWrite("test.ini","test","colors",$out[2])

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name...........: $tagCHOOSECOLOR
; Description ...: Contains information the _ChooseColor function uses to initialize the Color dialog box
; Fields ........: Size      - Specifies the size, in bytes, of the structure
;                hWndOwner   - Handle to the window that owns the dialog box
;                hInstance   - If the $CC_ENABLETEMPLATEHANDLE flag is set in the Flags member, hInstance is a handle to a memory
;                +object containing a dialog box template. If the $CC_ENABLETEMPLATE flag is set, hInstance is a handle to a module
;                +that contains a dialog box template named by the lpTemplateName member. If neither $CC_ENABLETEMPLATEHANDLE
;                +nor $CC_ENABLETEMPLATE is set, this member is ignored.
;                rgbResult   - If the $CC_RGBINIT flag is set, rgbResult specifies the color initially selected when the dialog
;                +box is created.
;                CustColors  - Pointer to an array of 16 values that contain red, green, blue (RGB) values for the custom color
;                +boxes in the dialog box.
;                Flags       - 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 the following flags:
;                |$CC_ANYCOLOR           - Causes the dialog box to display all available colors in the set of basic colors
;                |$CC_ENABLEHOOK         - Enables the hook procedure specified in the lpfnHook member
;                |$CC_ENABLETEMPLATE     - Indicates that the hInstance and lpTemplateName members specify a dialog box template
;                |$CC_ENABLETEMPLATEHANDLE - Indicates that the hInstance member identifies a data block that contains a preloaded
;                +dialog box template
;                |$CC_FULLOPEN           - Causes the dialog box to display the additional controls that allow the user to create
;                +custom colors
;                |$CC_PREVENTFULLOPEN    - Disables the Define Custom Color
;                |$CC_RGBINIT            - Causes the dialog box to use the color specified in the rgbResult member as the initial
;                +color selection
;                |$CC_SHOWHELP           - Causes the dialog box to display the Help button
;                |$CC_SOLIDCOLOR         - Causes the dialog box to display only solid colors in the set of basic colors
;                lCustData   - Specifies application-defined data that the system passes to the hook procedure identified by the
;                +lpfnHook member
;                lpfnHook    - 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
;                lpTemplateName - Pointer to a null-terminated string that names the dialog box template resource in the module
;                +identified by the hInstance m
; Author ........: Gary Frost (gafrost)
; Remarks .......:
; ===============================================================================================================================
; #FUNCTION# ====================================================================================================================
; Name...........: _ChooseColor
; Description ...: Creates a Color dialog box that enables the user to select a color
; Syntax.........: _ChooseColor([$iReturnType = 0[, $iColorRef = 0[, $iRefType = 0[, $hWndOwnder = 0]]]])
; Parameters ....: $iReturnType - Determines return type, valid values:
;                |0 - COLORREF rgbcolor
;                |1 - BGR hex
;                |2 - RGB hex
;                $iColorRef - Default selected Color
;                $iRefType - Type of $iColorRef passed in, valid values:
;                |0 - COLORREF rgbcolor
;                |1 - BGR hex
;                |2 - RGB hex
;                $hWndOwnder - Handle to the window that owns the dialog box
;        $icustcolor - String of up to 16 COLORREF rgbcolor separated by |. If more, it will take the 1st 16, if less, the rest will be black
;    example: 13656238|14019166|5829460|7530337
; Return values .: Success   -Array in the following format:
;    |[0] - contains the number of elements
;    |[1] - Color returned
;    |[2] - 16 custom colors returned separated by |... not there is not a trailing |
;                Failure     - -1
; Author ........: Gary Frost (gafrost)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ChooseColor($iReturnType = 0, $iColorRef = 0, $iRefType = 0, $hWndOwnder = 0, $icustcolor = 0)
Local Const $__MISCCONSTANT_CC_ANYCOLOR = 0x0100
Local Const $__MISCCONSTANT_CC_FULLOPEN = 0x0002
Local Const $__MISCCONSTANT_CC_RGBINIT = 0x0001
Local Const $tagCHOOSECOLOR = "dword Size;hwnd hWndOwnder;handle hInstance;dword rgbResult;ptr CustColors;dword Flags;lparam lCustData;" & _
"ptr lpfnHook;ptr lpTemplateName"
Local $Return, $outcolors
Local $custcolors = "dword[16]"
Local $tChoose = DllStructCreate($tagCHOOSECOLOR)
Local $tcc = DllStructCreate($custcolors)
If $icustcolor <> 0 Then
If StringLeft ($icustcolor,1) = '|' Then $icustcolor = StringTrimLeft($icustcolor,1)
Local $incolors = StringSplit($icustcolor,'|')
If @error <> 1 Then
If $incolors > 16 Then $incolors = 16
for $i = 1 to $incolors[0]
DllStructSetData($tcc,1,$incolors[$i],$i)
Next
$incolors = 0
Endif
EndIf
If $iRefType = 1 Then ; BGR hex color to colorref
$iColorRef = Int($iColorRef)
ElseIf $iRefType = 2 Then ; RGB hex color to colorref
$iColorRef = Hex(String($iColorRef), 6)
$iColorRef = '0x' & StringMid($iColorRef, 5, 2) & StringMid($iColorRef, 3, 2) & StringMid($iColorRef, 1, 2)
EndIf
DllStructSetData($tChoose, "Size", DllStructGetSize($tChoose))
DllStructSetData($tChoose, "hWndOwnder", $hWndOwnder)
DllStructSetData($tChoose, "rgbResult", $iColorRef)
DllStructSetData($tChoose, "CustColors", DllStructGetPtr($tcc))
DllStructSetData($tChoose, "Flags", BitOR($__MISCCONSTANT_CC_ANYCOLOR,$__MISCCONSTANT_CC_RGBINIT)); took out $__MISCCONSTANT_CC_FULLOPEN, matter of preference
Local $aResult = DllCall("comdlg32.dll", "bool", "ChooseColor", "struct*", $tChoose)
If @error Then Return SetError(@error, @extended, -1)
If $aResult[0] = 0 Then Return SetError(-3, -3, -1) ; user selected cancel or struct settings incorrect
Local $color_picked = DllStructGetData($tChoose, "rgbResult")
For $i = 1 to 16
$outcolors &= DllStructGetData($tcc,1,$i)
If $i <> 16 Then $outcolors &= '|'
Next
If $iReturnType = 1 Then ; return Hex BGR Color
$Return = '0x' & Hex(String($color_picked), 6)
ElseIf $iReturnType = 2 Then ; return Hex RGB Color
$color_picked = Hex(String($color_picked), 6)
$Return = '0x' & StringMid($color_picked, 5, 2) & StringMid($color_picked, 3, 2) & StringMid($color_picked, 1, 2)
ElseIf $iReturnType = 0 Then ; return RGB COLORREF
$Return = $color_picked
Else
Return SetError(-4, -4, -1)
EndIf
Return StringSplit($Return & ',' & $outcolors,',')
EndFunc ;==>_ChooseColor

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0