Using the Colour Dialogue

For our Lazarus demonstration we added to the form only a ColorDialog (from the Dialogs tab of components) and a static text (from the Additional tab).

Output from program ColorDialogDemo

Output from program ColorDialogDemo

Our code of the files needed for program ColorDialogDemo follows. You can copy the code if necessary, but a glance should be enough to adapt it for your own use. Again, it is the Execute function of the dialogue that is vital and the Color property is obvious (if you remember the American spelling).

uColorDialogDemo.pas

unit uColorDialogDemo;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type
  TfrmColour = class(TForm)
    ColorDialog1: TColorDialog;
    StaticText1: TStaticText;
    procedure FormClick(Sender: TObject);
  end;
var
  frmColour: TfrmColour;

implementation

{$R *.lfm}

procedure TfrmColour.FormClick(Sender: TObject);
begin
  if ColorDialog1.Execute then
    frmColour.Color:= ColorDialog1.Color;
end;

end.

uColorDialogDemo.lfm

object frmColour: TfrmColour
  Left = 509
  Height = 96
  Top = 235
  Width = 327
  Caption = 'Colour'
  ClientHeight = 96
  ClientWidth = 327
  OnClick = FormClick
  LCLVersion = '0.9.30'
  object StaticText1: TStaticText
    Left = 8
    Height = 20
    Top = 16
    Width = 324
    Caption = 'Click on the form to access the colour dialogue.'
    TabOrder = 0
  end
  object ColorDialog1: TColorDialog
    Color = clBlack
    CustomColors.Strings = (
      'ColorA=000000'
      'ColorB=000080'
      'ColorC=008000'
      'ColorD=008080'
      'ColorE=800000'
      'ColorF=800080'
      'ColorG=808000'
      'ColorH=808080'
      'ColorI=C0C0C0'
      'ColorJ=0000FF'
      'ColorK=00FF00'
      'ColorL=00FFFF'
      'ColorM=FF0000'
      'ColorN=FF00FF'
      'ColorO=FFFF00'
      'ColorP=FFFFFF'
      'ColorQ=C0DCC0'
      'ColorR=F0CAA6'
      'ColorS=F0FBFF'
      'ColorT=A4A0A0'
    )
    left = 120
    top = 40
  end
end

ColorDialogDemo.lpr

program ColorDialogDemo;

uses
  Interfaces, Forms, uColorDialogDemo;

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TfrmColour, frmColour);
  Application.Run;
end.
Programming - a skill for life!

How to use message, file, colour, font and calendar dialogue boxes to interact with the user