Colorpicker does not work

Windows specific questions.
Post Reply
Lothar Schirm
Posts: 492
Joined: Sep 28, 2013 15:08
Location: Germany

Colorpicker does not work

Post by Lothar Schirm »

I took the following code for a colorpicker from here https://www.freebasic-portal.de/code-be ... r-227.html:

Code: Select all

#define WIN_INCLUDEALL
#include "windows.bi"

DIM AS CHOOSECOLOR PTR cc        'common dialog box structure
DIM AS UINTEGER PTR acrCustClr   'custom colors (16x4 bytes) -> Speicher dynamisch anfordern
DIM AS HWND hwnd                 'owner window
DIM AS HBRUSH hbrush             'brush handle
DIM AS UINTEGER rgbCurrent       'initial color selection

rgbCurrent = &HFF00FF  'Rosa als Voreinstellung - initial selection: pink

cc = CAllocate(sizeof(CHOOSECOLOR))
acrCustClr = CAllocate(64)

cc->lStructSize = Cast(UInteger,sizeof(CHOOSECOLOR))
cc->hwndOwner = hwnd
cc->lpCustColors = CAST(LPDWORD, acrCustClr)
cc->rgbResult = rgbCurrent
cc->Flags = CC_FULLOPEN Or CC_RGBINIT

if (ChooseColor(cc) = TRUE) Then
    hbrush = CreateSolidBrush(cc->rgbResult)
    rgbCurrent = cc->rgbResult
    'Print "OK, alles klar! Gewaehlter Farbwert (hexadezimal): "; HEX(rgbCurrent, 8)
    Print "Ok, no error! Selected color: "; HEX(rgbCurrent, 8)
Else
    'Print "Fehler: Aktion durch Benutzer abgebrochen oder Aufruf fehlgeschlagen."
    Print "Error: action aborted by the user or selection failed."
    
End If

Sleep
End
Worked well with previous versions of FreeBasic, but not with version 1.04 (32 bit and 64 bit), result is always 0.
Drago
Posts: 116
Joined: Aug 10, 2005 13:15

Re: Colorpicker does not work

Post by Drago »

With 1.05 32-Bit it is OK with:

Code: Select all

    if (ChooseColor(cc) = 1) Then
Grüße
Rainer
Lothar Schirm
Posts: 492
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Colorpicker does not work

Post by Lothar Schirm »

Thank you, works also with 1.04!
fxm
Moderator
Posts: 12576
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Colorpicker does not work

Post by fxm »

This is due to the new BOOLEAN data type which was added in version 1.04.4:
Now TRUE is implicitly convert to -1.

Other syntax, but only for version >= 1.04.0

Code: Select all

if (Cbool(ChooseColor(cc)) = True) Then
Last edited by fxm on Dec 16, 2015 19:38, edited 1 time in total.
fxm
Moderator
Posts: 12576
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Colorpicker does not work

Post by fxm »

Other syntax which works with all versions:

Code: Select all

if (ChooseColor(cc) <> Not TRUE) Then
Or:

Code: Select all

if (ChooseColor(cc) = Not TRUE) Then
    'Print "Fehler: Aktion durch Benutzer abgebrochen oder Aufruf fehlgeschlagen."
    Print "Error: action aborted by the user or selection failed."
Else
    hbrush = CreateSolidBrush(cc->rgbResult)
    rgbCurrent = cc->rgbResult
    'Print "OK, alles klar! Gewaehlter Farbwert (hexadezimal): "; HEX(rgbCurrent, 8)
    Print "Ok, no error! Selected color: "; HEX(rgbCurrent, 8)
aloberoger
Posts: 507
Joined: Jan 13, 2009 19:23

Re: Colorpicker does not work

Post by aloberoger »

you can also use

Code: Select all

if (ChooseColor(cc) = CTRUE) Then
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Colorpicker does not work

Post by MrSwiss »

@Lothar Schirm,

the Example will only work with 32bit Compiler! This should be corrected!

For it to work correctly with all Compiler Versions, you'd have to replace the
UInteger Stuff with ULong (also the Pointer = ULong Ptr), since Color is a fixed
UINT32, consisting of four UBytes(a, r, g, b).

Proof: your HEX output ... HEX(nnnn, 8) = 4 x UByte = 1 x ULong (2 per UByte).
Post Reply