Using ChooseColor API command

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Hi,
I wrote a small program to attach HEX and RGB color
codes to the clipboard. I'm trying to use the ChooseColor
API command, since I would like to add 16 "Custom Colors" to
Color dialog box, rather than the plain ColorRequester() function.

I've got most of it figured out (new to structure calls to the API), but I can't seem to return a color value from the API command.

Note: Deleted code...see results below

Thanks,
--Bob
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

The ChoseColor_() API command DON'T return the color directly, but returns if the command was successfull or not. To get the color you've choosen you have to look into the structure you sent to the command. Something like this should do it.

Code: Select all

ChoseColor_(@ColorSpec)
Color.l = ColorSpec\rgbResult
Haven't tested it though, but give it a try...should work :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Pupil,
Thanks for the response, but the ChooseColor_ commands 'primary job' is to create a dialog box just like ColorRequester(). Since ColorRequester doesn't have any parameters, I thought I would use the Windows API to 'seed' the dialog box with pre-defined random colors.

I have this working in (gasp!) PowerBASIC.

From Windows API Help:
The ChooseColor function creates a Color common dialog box that enables the user to select a color.

BOOL ChooseColor(

LPCHOOSECOLOR lpcc // pointer to structure with initialization data
);


Parameters

lpcc

Pointer to a CHOOSECOLOR structure that contains information used to initialize the dialog box. When ChooseColor returns, this structure contains information about the user's color selection.



Return Values

If the user clicks the OK button of the dialog box, the return value is nonzero. The rgbResult member of the CHOOSECOLOR structure contains the RGB color value of the color selected by the user.
The trouble I'm having is getting the dialog to even 'pop up'.

--Bob
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Pupil is pointing you in the right direction.
Maybe this will make it a little more clear...

Code: Select all

Global ColorSpec.CHOOSECOLORAPI 
Dim CustomColor.l(15)
 
#WindowWidth  = 250
#WindowHeight = 80
 
hWnd=OpenWindow(0, 100, 150, #WindowWidth, #WindowHeight, #PB_Window_SystemMenu, "PureBasic - Colors Sent to Clipboard")
If CreateGadgetList(hWnd)=0 Or hWnd=0:End:EndIf 
    
SetGadgetFont(Bold) 
TextGadget(0, 5, 8, 200, 18, "Select Color Preference:")
 
SetGadgetFont(Normal)
OptionGadget(1,  50,  32, 150, 10, "Return Raw Hex Color")
OptionGadget(2,  50,  52, 150, 10,  "Return RGB Color") 
 
 
For Counter.l = 0 To 15
  CustomColor(Counter) = Random(16777215)
Next
 
 
Repeat
  EventID = WaitWindowEvent()     
  If EventID=#PB_EventGadget      
    Select EventGadgetID() 
          
      Case 1  ; HEX ColorRequester
      ColorSpec\lStructSize   = SizeOf(CHOOSECOLORAPI)
      ColorSpec\hwndOwner     = hWnd
      ColorSpec\hInstance     = 0 
      ColorSpec\rgbResult     = 0
      ColorSpec\lpCustColors  = @CustomColor(0)
      ColorSpec\Flags         = #CC_FULLOPEN|CC_RGBINIT
      ColorSpec\lCustData     = 0 
      ColorSpec\lpfnHook      = @HookProc
      ColorSpec\lpTemplateName= 0
 
      If ChooseColor_(ColorSpec) 
        Color=ColorSpec\rgbResult                 
        result.s = "RGB("+Str(Red(Color))+","+Str(Green(Color))+","+Str(Blue(Color))+")"
        MessageRequester("Color:", result, 0)
      EndIf 
                                     
 
    EndSelect
  EndIf
Until EventID = #PB_EventCloseWindow
End
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Thanks Paul,

The thing I was not getting was the line:
ColorSpec\lStructSize = SizeOf(CHOOSECOLORAPI)

I had been looking for something along the lines of
LEN(), but it only returns the length of a string.

I couldn't find anything like SizeOf() in the HTML Help file.

Still can't find it...only some reference to it in PureBasic History
(Feb 2001).

Perhaps it could be re-added to the Help file?

Thanks again,
- Bob

PS - I'll re-post the final product in a few days. I'm going out of town for a few days.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Finally got back into town and got my little program running. I refined the code as much as possible, so I think it will be quite handy for coders looking to add colors to their text, etc.

Thanks again Paul for the "SizeOf" tip.

Code: Select all

; ------------------------------------------------------------
;  PickColor.pb - Colors to your clipboard
;     
;                 Sometimes when working with the Windows API
;                 you'd like to see colors in HEX. 
;                 Sometimes you'd like RGB colors.

;                 This small program allows you to choose and
;                 place the color info to the clipboard. Simply
;                 paste the results into your program.
;                 Public Domain              Aug. 2002 - blueb
;
; ------------------------------------------------------------

; Declarations
Global hWnd.l
Global ColorSpec.CHOOSECOLORAPI  ;Note: This is a Windows API structure
Dim CustomColor.l(15)

Normal.l = LoadFont(0, "Arial", 9)
Bold.l = LoadFont(1, "Arial Bold", 10)

#WindowWidth  = 250
#WindowHeight = 80
 
;--------------------------------------------------------------------------
Procedure.l UpdateClipBoard(Result.s)
    hGout.l
    mx.l
    Result.s
      hGout = GlobalAlloc_(#GMEM_MOVEABLE |#GMEM_ZEROINIT |#GMEM_DDESHARE, @Result)
      Mx = GlobalLock_(hGout)
      PokeS(Mx, Result)
      GlobalUnLock_(hGout)
      If OpenClipBoard_(hWnd)
           EmptyClipBoard_()
              SetClipBoardData_(#CF_TEXT, hGout)
           CloseClipBoard_()
          ; GlobalFree_(hGout)  Note: From MSDN...you should not free the handle, 
          ;                     as that handle is now the property of Windows.
      EndIf
EndProcedure
;---------------------------------------------------------------------------

If OpenWindow(0, 100, 150, #WindowWidth, #WindowHeight, #PB_Window_MinimizeGadget, "PickColor")
hWnd = WindowID()
 If CreateGadgetList(WindowID())
   
  ;Add text gadget
  SetGadgetFont(Bold) 
  TextGadget(0, 5, 8, 200, 18, "Select Color Preference:")
  
  ;Add option buttons
  SetGadgetFont(Normal)
  OptionGadget(1,  50,  32, 150, 10, "Returns HEX Color")
  OptionGadget(2,  50,  52, 150, 10,  "Returns RGB Color") 
  
  ;Event loop 
  Repeat
    EventID = WaitWindowEvent()
     
     If EventID = #PB_EventGadget

      Select EventGadgetID()
          
          Case 1  ; HEX ColorRequester
                  ; Places a new assortment of colors in the custom boxes
                  ; each time the option button is pressed. 
                  For Counter.l = 0 To 15 
                      CustomColor(Counter) = Random(16777215) 
                  Next
            
                  ColorSpec\lStructSize  = SizeOf(CHOOSECOLORAPI)
                  ColorSpec\hwndOwner    = hWnd
                  ColorSpec\lpCustColors = @CustomColor(0)
                  ColorSpec\Flags = #CC_FULLOPEN|#CC_RGBINIT 
                  
                  If ChooseColor_(ColorSpec) 
                      Color=ColorSpec\rgbResult 
                      result.s = "&h" + Hex(Color) 
                      MessageRequester("HEX Color:", result, 0)
                      UpdateClipBoard(result) 
                  EndIf
          
                  
          Case 2  ; RGB ColorRequester
                  ; Places a new assortment of colors in the custom boxes
                  ; each time the option button is pressed. 
                  For Counter.l = 0 To 15 
                      CustomColor(Counter) = Random(16777215) 
                  Next
                            
                  ColorSpec\lStructSize = SizeOf(CHOOSECOLORAPI) ;Thanks to Paul   :>)
                  ColorSpec\hwndOwner = hWnd
                  ColorSpec\lpCustColors = @CustomColor(0) 
                  ColorSpec\Flags = #CC_FULLOPEN|#CC_RGBINIT 

                  If ChooseColor_(ColorSpec) 
                      Color=ColorSpec\rgbResult 
                      result.s = "RGB("+Str(Red(Color))+","+Str(Green(Color))+","+Str(Blue(Color))+")" 
                      MessageRequester("RGB Color:", result, 0)
                      UpdateClipBoard(result) 
                  EndIf
        EndSelect
      ;Minimize window and place it on the task bar.  
      res.l = CloseWindow_(hWnd)       
    EndIf

  Until EventID = #PB_EventCloseWindow
  
 EndIf
   
EndIf

End
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

It works fien and it is a nice API use sample. BTW you may have notice that ColorRequester would save some source code writing here.

Good anyway.

Rgrds

Francois Weil
14, rue Douer
F64100 Bayonne
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Francois,

Thanks, I hope you find it useful.

The reason I tried this is that ColorRequester()would not allow me to put the 15 random colors in the custom color boxes.

Small utilities make my life easier...
- Windows API likes HEX numbers
- PureBasic likes RGB()

Regards,
--Bob

Note that this is also a very good example of how to get information into and out of the clipboard!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

I agree with your snippet and intentions ... definitely.

Good work.

Francois Weil
14, rue Douer
F64100 Bayonne
Post Reply