ChooseColor

Windows specific questions.
Post Reply
ring0
Posts: 14
Joined: Dec 26, 2012 6:33
Location: Oz

ChooseColor

Post by ring0 »

Hi,
I am having some problems with ChooseColor, using FB1.05, win32. I notice in another post dated 2015 ?? that the headers for this are broken. Is that still the case? Should ChooseColor structure definition and ChooseColor function call work OK?? Code below compiles without errors but program crashes at function call. In FBedit the autocomplete lists CHOOSECOLOR and CHOOSECOLORA - what is the difference? The all capitals of the autocomplete always changes to mixed case for ChooseColor but I note example code from other posters where this is not the case. Also I do not get autocomplete options for the cc fields. Any insights welcome.
Thanks.

Code: Select all

#Include once "windows.bi"
#Include Once "win/commdlg.bi"
#Include Once "win/commctrl.bi"

...blah, blah

Case lblPlotColor
        Dim As ChooseColor cc
                                        cc.lStructSize    = SizeOf(cc)
                                        cc.hwndOwner      = hWin
                                        cc.hInstance      = NULL
                                        cc.rgbResult      = &H00FFFFFF
                                        cc.lpCustColors   = NULL
                                        cc.Flags          = CC_FULLOPEN
                                        cc.lCustData      = NULL
                                        cc.lpfnHook       = NULL
                                        cc.lpTemplateName = NULL
        If (ChooseColor(@cc)<> Not TRUE) Then                   
            SetWindowText(hWin,hex$(cc.rgbResult))
        EndIf      
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: ChooseColor

Post by St_W »

As the latest official FreeBasic release is from January 2016 that is even quite likely. <off-topic>Unfortunately many fixes suggest here on the forums are not contributed in form of a patch to the official FreeBasic code and then often forgotten after a while.</off-topic>
Did you try whether the latest headers (e.g. available from here: http://users.freebasic-portal.de/stw/bu ... eaders.zip) change anything? The defintion in the headers looks correct on firsth sight, yet I could easily have missed something.
ring0 wrote:In FBedit the autocomplete lists CHOOSECOLOR and CHOOSECOLORA - what is the difference?
The Win32 API provides ANSI and Unicode versions of many API functions and structures dealing with strings. The former have a "A" suffix, while the latter have a "W" suffix. The name without suffix (if available) redirects to one or the other depending on wheter "UNICODE" is defined (or not). See also https://msdn.microsoft.com/en-us/librar ... 46830.aspx on the bottom.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: ChooseColor

Post by St_W »

The headers are fine. I just wrote a small demo application and it works in both 32-bit and 64-bit configurations.

Code: Select all

#Include once "windows.bi"
#Include Once "win/commdlg.bi"

dim as COLORREF customColors(0 to 15)

Dim As ChooseColor cc
cc.lStructSize    = SizeOf(cc)
cc.rgbResult      = BGR(255,255,255)
cc.lpCustColors   = @customColors(0)
cc.Flags          = CC_FULLOPEN or CC_RGBINIT
If (ChooseColor(@cc)) Then                   
	print "Color chosen: "; hex(cc.rgbResult)
else
	print "cancelled"
EndIf
Your error is probably (concluding from the code excerpt you showed) that you are passing an invalid pointer in the cc.lpCustColors value. The documentation doesn't mention that this value may be NULL.
ring0
Posts: 14
Joined: Dec 26, 2012 6:33
Location: Oz

Re: ChooseColor

Post by ring0 »

Hello St_W,
thank you for your assistance, removing the NULL has stopped the crash!
Cheers,
ring0
Post Reply