March 2, 2021
Hot Topics:

Using the Choose Color Dialog Box

  • By Steve Anderson
  • Send Email »
  • More Articles »

The API function that is required to show the Color Dialog box is the ChooseColour API function, which has one parameter with passes a ChooseColour type structure to the API function and displays the common dialog box.

The ChooseColour Type structure has the following parameters:

lStructSize – This is the size of this type structure and you should set this parameter to the length of the structure

hWndOwner – You should set this parameter to the handle of the object that the colour will be applied to (e.g., text1.hwnd).  If you do not specify a handle, the user will be free to carry on using the application with the Colour dialog box still on the screen.

hInstance – In C++ and some other programming languages you can actually customise the dialog box, by adding extra buttons, parameters, etc…but this not possible in Visual Basic (as far as I know), so you should leave this parameter as Null (0&).

rgbResult – This is the parameter that contains the returned colour that the user selected from the Common Dialog box, which can be directly set into an object's backcolor or forecolor property, or transferred into a variable. You can even be set to the current back colour of an object, so the colour currently used is by default the colour selected in the common dialog control. (For information of setting the value of a currently used colour see the flags parameter).

lpCustColors – This is the parameter that is the cause of most problems. This is because it needs an array of custom colours to be set before calling. This seems easy enough, but the array needs to be passed in Unicode format, which isn't directly supported in Visual Basic.

Flags – The flags parameter contains information on the way you want to display the ChooseColour Common Dialog box and can be either one or a combination of the following.

  • CC_FULLOPEN - You will have noticed that the ChooseColour Common Dialog has the option to select a custom colour by clicking on the Define Custom Colours >> button. When displaying the Common Dialog box you have the option to open the custom colour part of the dialog box by default by passing this constant.  In fact this seems to be the most commonly used flag, as for instance the Appearance tab of the Display Control Panel Applet shows does the same when selecting a colour.
  • CC_PREVENTFULLOPEN - As you can display the custom colours part by default you can also prevent the user from displaying this part of the dialog box by passing this constant.  You may want to do this in the case where you want to restrict the user to only selecting the standard set of colours that are provided
  • CC_RGBINIT - You will recall me mentioning the ability to set the colour currently being used so that the colour that is being used is selected when displaying the Common Dialog box. You can do this by passing this constant and in rgbResult parameter you pass the colour currently being used. Unfortunately there is a little problem being that Visual Basic displays its colours in hexadecimal format. A simple way to get around this is the use the GetBKColour API function to return the correct type of value that can be passed. There is an example of this below in the structured class.
  • CC_SHOWHELP - This constant and the next one is not documented in this article, but are left an a task for the reader to accomplish. This constant simply displays a help button in the ChooseColour Common Dialog control and when clicked on sends a message to the owner window. You can the use these messages to display your own help.
  • CC_ENABLEHOOK - This is rather similar to CC_SHOWHELP as it sends messages from the Common Dialog box to the owner window of the Dialog Box. You can then create a message handler to use these messages to perform custom actions. This can be also thought of as sub-classing.

lCustData – Leave this as Null (0&).

lpfnHook – Leave this as Null (0&).

lpTemplateName – Leave this as Null (0&).

If the ChooseColour API function returns zero, then the user has clicked the Cancel button.

Page 2 of 3

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date