Home

Win32 Controls: The Color Dialog Box

 

Introduction to the Color Dialog Box

 

Description

To provide the selection of colors on Microsoft Windows applications, the operating system provides a common dialog box appropriate for such tasks. The Color dialog box is used by various reasons to let the user set or change a color of an object such as the background color of a control or the color used to paint an object. When it displays, by default, the dialog box appears as follows:

 
The Color Dialog Box

 

This displays a constant list of colors to the user. If none of the available colors is appropriate for the task at hand, the user can click the Define Custom Colors button to expand the dialog box:

The expanded Color dialog box allows the user to either select one of the preset colors or to custom create a color by specifying its red, green, and blue values.

The user can change the color in four different areas. The top left section displays a list of 48 predefined colors. If the desired color is not in that section, the user can click and drag the mouse in the multi-colored palette. The user can also drag the right bar that displays a range based on the color of the palette; the user can scroll up and down by dragging the arrow. For more precision, the user can type the Red, Green and Blue values. Each uses a integral value that ranges from 1 to 255.

Creating a Color Dialog Box

In the VCL, the color dialog box is available through a class named TColorDialog. The TColorDialog class is derived from the TCommonDialog class. The TCommonDialog class is derived from TComponent.

To visually add a Color dialog box to your application, from the Dialogs section of the Tool Palette, click the TColorDialog button TColorDialog and click anywhere on the form. To programmatically create a color dialog box, declare a variable of type TColorDialog. Here is an example:

procedure TForm1.btnColorClick(Sender: TObject);
var Dlg : TColorDialog;
begin
    Dlg := TColorDialog.Create(Form1);
end;

Characteristics of the Color Dialog Box

 

The Color

The most important and most obvious property of the Color dialog box is the selected color once the user has made a choice. To provide this information, the TColorDialog class is equipped with the Color property:

property Color: TColor read FColor write FColor;

When the user opens the dialog, you can set the default color on the Object Inspector using the Color property. You can also set this color programmatically as follows:

procedure TForm1.btnColorClick(Sender: TObject);
var Dlg : TColorDialog;
begin
    Dlg := TColorDialog.Create(Form1);
    Dlg.Color := clRed;
end;

When the user has finished using the Color dialog box and clicks OK, you can find out what color was selected by using the TColorDialog.Color property.

The Size of the Dialog Box

You can control the regular or full size of the dialog using the Options property:

property Options: TColorDialogOptions read FOptions write FOptions;

At design time, to manipulate the options, on the Object Inspector, click the + button on the Options field to expand it. Since the options are controlled by the TColorDialogOption is a set, you can specify as many options as you want:

procedure TForm1.btnColorClick(Sender: TObject);
var Dlg : TColorDialog;
begin
    Dlg := TColorDialog.Create(Form1);
    Dlg.Color := clRed;

    Dlg.Options := [cdFullOpen, cdAnyColor];
end;

Custom Colors

If you want to supply the user with a set of colors of your choice, you can do this using a list of custom colors. To create this list, click the CustomColor field to reveal its ellipsis button, then click that button to display the String List Editor dialog box. You can specify up to 16 colors. The colors are named ColorA, ColorB, ColorC, and so on up to ColorP. To create the list, type the ordinal name and assign it an integer number. Here is an example:

String List Editor

Using the Dialog Box

The most important method of the Color dialog is the Execute() member function. This method occurs when the user clicks OK or presses Enter after selecting a color. You can use it to get the selected color and use it as you see fit. The following example displays a Color dialog when the user clicks a button on the form. When the user clicks OK on the Color dialog, the selected color is applied to the background of the form:

procedure TForm1.btnColorClick(Sender: TObject);
var Dlg : TColorDialog;
begin
    Dlg := TColorDialog.Create(Form1);
    Dlg.Color := clRed;

    Dlg.Options := [cdFullOpen, cdAnyColor];

    Dlg.Execute;
    Color := Dlg.Color;
end;

The most efficient approach is to make sure that the dialog was opened and closed successfully, then retrieve the color if the user clicked OK to close the dialog. This is done through a conditional call, as follows:

procedure TForm1.btnColorClick(Sender: TObject);
var Dlg : TColorDialog;
begin
    Dlg := TColorDialog.Create(Form1);

    if Dlg.Execute then
    	Color := Dlg.Color;
end;
 
 
 
 

Home Copyright © 2010-2016, FunctionX