Marco's Web Center

Menu for Development

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old)Tools
(Old) Conferences

My Other Sites
Italian Site (www.marcocantu.it)
the delphi search

Spirit of delphi

Advertising
Home My Blog Books Object Pascal Marco

Home: Code Repository: Mastering Delphi 5

Project ODLIST

Project Structure


ODLIST.DPR

program ODList;

uses
  Forms,
  ODListF in 'ODListF.pas' {ODListForm};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TODListForm, ODListForm);
  Application.Run;
end.

ODLISTF.PAS

unit ODListF;

interface

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

type
  TODListForm = class(TForm)
    ListBox1: TListBox;
    ColorDialog1: TColorDialog;
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormCreate(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    procedure AddColors (Colors: array of TColor);
  end;

var
  ODListForm: TODListForm;

implementation

{$R *.DFM}

procedure TODListForm.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with Control as TListbox do
  begin
    // erase
    Canvas.FillRect (Rect);
    // draw item
    Canvas.Font.Color := TColor (
      Items.Objects [Index]);
    Canvas.TextOut(Rect.Left, Rect.Top,
      Listbox1.Items[Index]);

//    InflateRect (Rect, -1, -1);
    if (odFocused in State) and (odSelected in State) then
      Canvas.DrawFocusRect (Rect);
   end;
end;

procedure TODListForm.AddColors (Colors: array of TColor);
var
  I: Integer;
begin
  for I := Low (Colors) to High (Colors) do
    ListBox1.Items.AddObject (
      ColorToString (Colors[I]),
      TObject(Colors[I]));
end;

procedure TODListForm.FormCreate(Sender: TObject);
begin
  Canvas.Font := ListBox1.Font;
  ListBox1.ItemHeight := Canvas.TextHeight('0');
  AddColors ([clRed, clBlue, clYellow, clGreen, clFuchsia, clLime,
    clGray, RGB (213, 23, 123), RGB (0, 0, 0),
    clAqua, clNavy, clOlive, clPurple, clTeal]);
end;

procedure TODListForm.ListBox1DblClick(Sender: TObject);
begin
  if ColorDialog1.Execute then
    AddColors ([ColorDialog1.Color]);
end;

end.

ODLISTF.DFM

object ODListForm: TODListForm
  Left = 242
  Top = 121
  Width = 423
  Height = 442
  Caption = 'Owner-draw Listbox'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ListBox1: TListBox
    Left = 0
    Top = 0
    Width = 415
    Height = 415
    Align = alClient
    Font.Charset = ANSI_CHARSET
    Font.Color = clBlack
    Font.Height = -32
    Font.Name = 'Arial'
    Font.Style = [fsBold]
    ItemHeight = 16
    ParentFont = False
    Sorted = True
    Style = lbOwnerDrawFixed
    TabOrder = 0
    OnDblClick = ListBox1DblClick
    OnDrawItem = ListBox1DrawItem
  end
  object ColorDialog1: TColorDialog
    Ctl3D = True
    Left = 72
    Top = 48
  end
end