---- 首先把Listbox2的style屬性改為lbOwnerDrawVariable。然後分別編寫它的OnDrawItem事件和OnDrawItem事件。下面就是Listbox2 的OnDrawItem事件和OnDrawItem事件的代碼:
procedure TForm1.ListBox2DrawItem(Control:TWinControl; Index: Integer;Rect: TRect; State: TOwnerDrawState);beginwith ListBox2.Canvas dobeginFillRect(Rect);Font.Size := 12;if Index mod 2 =0 ThenbeginFont.Name := '宋體';Font.Color := Clred;endelsebeginFont.Name := '隸書';Font.Color := Clgreen;end;TextOut(Rect.Left+1, Rect.Top+1,ListBox2.Items[Index]);end;end;
procedure TForm1.ListBox2MeasureItem(Control: TWinControl; Index: Integer;var Height: Integer);beginwith ListBox1.Canvas dobeginFont.Size := 12;if Index mod 2 =0 ThenbeginFont.Name := '黑體';Font.Color := Clred;endelsebeginFont.Name := '隸書';Font.Color := Clgreen;end;Height := TextHeight('Wg') + 2;end;end;
---- 此程序在Windows95、Delphi4.0環境下運行通過。