This is a very useful color dialog box that you can use in your Delphi applications so that your program users can choose a color for something you specify, like a forms color or a panels color.
To run up the ColorDialog you use the execute function.
function Execute: Boolean; override;
Like this:
begin
ColorDialog1.Execute;
end;
You can use this code to change a panels color:
begin
if ColorDialog1.Execute then
Panel1.Color := ColorDialog1.Color;
end;
If you want to be able to let your program users use Custom Color Options you can use this code:
begin
ColorDialog1.Options := [cdFullOpen];
if ColorDialog1.Execute then
begin
Panel1.Color := ColorDialog1.Color;
end;
end;
TColorDialog.Options:
All the TColorDialog options are set to
off
by default.
cdFullOpen | Is used to show the full ColorDialog box with the custom color options available. |
cdPreventFullOpen | Is used to disable the Custom
Colors
button in the dialog, so users cannot choose
and define custom colors. |
cdSolidColor | Is used to tell Windows to use the nearest solid color it can find to the color chosen. |
cdShowHelp | Adds the Help button to the dialog. |
cdAnyColor | The user can select colors that are non-solid (These might have to be approximated by dithering). |
To select cdFullOpen and cdShowHelp you can do this:
begin
ColorDialog1.Options := [cdFullOpen, cdShowHelp];
if ColorDialog1.Execute then
begin
Panel1.Color := ColorDialog1.Color;
end;
end;
Show the Panel1.Color as the selected color in a TColorDialog:
The code ColorDialog1.Color := Panel1.Color; as shown below in the example will show the current color of Panel1 as the selected color in the TColorDialog.
begin
ColorDialog1.Options := [cdFullOpen, cdShowHelp];
ColorDialog1.Color := Panel1.Color;
if ColorDialog1.Execute then
begin
Panel1.Color := ColorDialog1.Color;
end;
end;
You can also specify a color as well:
ColorDialog1.Color := clBlue;
Have you ever wondered how they fill those little boxes in a TColorDialog with colours (Colors)?
Here is how you could do it.
Drop the TColorDialog on a Form (Form1).
In the Object Inspector for the
ColorDialog
double-click on the CustomColors property.
This will open the 'String list editor'.
Paste in these lines:
ColorE=EABCCA
ColorF=FBBCCA
ColorG=ACBBCA
ColorH=BDBCCA
ColorI=DEBCCA
ColorJ=EFBCCA
ColorK=BBBAAA
ColorL=DDDAAA
ColorM=EEEAAA
ColorN=ABCDEF
ColorO=FEDCBA
ColorP=CDBEFA
Click 'OK' to save your changes to the String list editor.
Drop Shape1 (TShape) on Form1.
Drop a Button on Form1.
DoubleClick on the Button and type this
code:
if ColorDialog1.Execute then
begin
Shape1.Brush.Color
:= ColorDialog1.Color;
end;
Run the program. Click on the button, click on a color and Shape1 will change to that color.
Notice you will have many new colors available to choose from.