Logo New book: Delphi 2007 Handbook
My blog in online
Delphi tech support service: support.marcocantu.com
Google
  Web www.marcocantu.com

Menu for Development

Site Menu
Delphi 2007 Handbook
Mastering Borland Delphi 2005
Essential Pascal
Essential Delphi
Buy Books Online
Code Repository
Newsgroups
White Papers
Tools
Conferences
Training
Delphi Links
Contact Marco

My Other Sites
Italian Site (www.marcocantu.it)
Developers Newsgroups Browser (dev.newswhat.com)
My town (www.piazzacavalli.net)
the delphi search
Wintech Italia (my company)

Breaking News
Buy Mastering Borland Delphi 2005 from Amazon
Free ebook: Mastering Delphi Update for Delphi 2006

Advertising
Home My Blog Books My Bookstore Development Links Marco


Home: Code Repository: Mastering Delphi 6

Chapter 06 - Project QOdlist

Project Structure

QOdlist.dpr
program QOdlist;

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

{$R *.res}

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

interface

uses
  Qt, SysUtils, Classes, QGraphics, QControls, QForms, QDialogs,
  QStdCtrls, Types;

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

var
  ODListForm: TODListForm;

implementation

{$R *.xfm}

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;

function RGB (red, green, blue: Byte): Cardinal;
begin
  Result := blue + green * 256 + red * 256 * 256;
end;

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

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

procedure TODListForm.ListBox1DrawItem(Sender: TObject; Index: Integer;
  Rect: TRect; State: TOwnerDrawState; var Handled: Boolean);
begin
  with Sender 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;

end.
ODListF.xfm
object ODListForm: TODListForm
  Left = 242
  Top = 121
  Width = 423
  Height = 442
  ActiveControl = ListBox1
  Caption = 'Owner-draw Listbox'
  Color = clButton
  Font.Color = clText
  Font.Height = 11
  Font.Name = 'MS Sans Serif'
  Font.Pitch = fpVariable
  Font.Style = []
  Font.Weight = 40
  ParentFont = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  TextWidth = 6
  object ListBox1: TListBox
    Left = 0
    Top = 0
    Width = 423
    Height = 442
    Style = lbOwnerDrawFixed
    Align = alClient
    Font.CharSet = fcsLatin1
    Font.Color = clBlack
    Font.Height = 32
    Font.Name = 'Arial'
    Font.Pitch = fpVariable
    Font.Style = [fsBold]
    ItemHeight = 37
    ParentFont = False
    TabOrder = 0
    OnDblClick = ListBox1DblClick
    OnDrawItem = ListBox1DrawItem
  end
  object ColorDialog1: TColorDialog
    Left = 72
    Top = 48
  end
end