• 0
Sign in to follow this  
Jhonas

Colorir itens do Combobox com o ColorDialog

Question

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    ColorDialog1: TColorDialog;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  cc : integer = -1;
  Colors1: array[0..19] of TColor; // atribuir 20 cores do ColorDialog1
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
   if ColorDialog1.Execute then
      begin
         inc(cc); // contador (index) do combobox 
         Colors1[cc] := ColorDialog1.Color; // pegar a cor selecionada
         ComboBox1.Items.Append(Edit1.Text); // adicionar no Combobox o Edit1.Text
      end;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
 with Control as TComboBox do
 begin
   Canvas.Font.Color := TColor(Colors1[Index]); // mudar cor da fonte do item
   Canvas.Brush.Color := clWhite; // mostrar fundo branco no combobox
   Canvas.FillRect(Rect);
   Canvas.TextOut(Rect.Left,Rect.Top,ComboBox1.Items.Strings[index]); // mostrar Nomes
 end;
end;

end.

ColorBox4.JPG

OBS: Digite um nome no Edit1 e ao clicar no botão, o ColorDialog  é aberto para escolher uma cor

a cor será armazenada em uma variavel tipo array que será indexada ao item adicionado ao combobox

O mesmo  se aplica ao ListBox

abraço

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this