A: This code should draw all the entries in a listbox as red. (Don't forget to change the Listbox's style to lbOwnerDrawFixed.)
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState); VAR
S : String;
Temp: array[0..255] of Char;
begin
WITH Control AS TListBox, Canvas DO
BEGIN
S := Items[Index];
FillRect(Rect);
MoveTo(Rect.Left+2, Rect.Top);
SetTextAlign(Canvas.Handle, TA_LEFT
OR TA_UPDATECP);
Font.Color := clRed;
StrPCopy(Temp, S);
WinProcs.TextOut(Canvas.Handle, 0, 0,
Temp, StrLen(Temp));
END;
end;
{*******************************}
Here is a version that does not use the API call, but does the same thing:
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormActivate(Sender: TObject);
procedure ListBox1DrawItem(Control: TWinControl;
Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormActivate(Sender: TObject);
begin
listbox1.items.LoadFromFile('c:\autoexec.bat');
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
WITH Control AS TListBox, Canvas DO BEGIN
FillRect(Rect);
MoveTo(Rect.Left+2, Rect.Top);
SetTextAlign(Canvas.Handle, TA_LEFT OR TA_UPDATECP);
Font.Color := clGreen;
TextOut(0, 0, Items[Index]);
END;
end;
end.