Delphi實現(xiàn)Listbox中的item根據(jù)內(nèi)容顯示不同顏色的方法
更新時間:2014年07月17日 10:30:42 投稿:shichen2014
這篇文章主要介紹了Delphi實現(xiàn)Listbox中的item根據(jù)內(nèi)容顯示不同顏色的方法,需要的朋友可以參考下
本文簡述了Delphi實現(xiàn)Listbox中的item根據(jù)內(nèi)容顯示不同顏色的方法,實現(xiàn)步驟如下:
ListBox1 的 Style 屬性改為 lbOwnerDrawVariable
在ListBox的OnDrawItem事件裡,根據(jù)item的值,改變Canvas屬性
示例代碼如下:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin //字體用原來默認的顏色 if Odd(index) then //當(dāng)items的index為奇數(shù)時的顏色 begin listbox1.Canvas.Brush.Color:=clwindow; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end else //當(dāng)items的index為偶數(shù)時的顏色 begin listbox1.Canvas.Brush.Color:=clinactivecaptiontext; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end; if odSelected in state then //當(dāng)選定時的顏色 begin listbox1.Canvas.Brush.Color:=clhighlight; ListBox1.Canvas.TextRect(Rect,rect.Left,Rect.Top,ListBox1.Items[index]); end; end;
相關(guān)文章
Delphi實現(xiàn)獲取進程列表及相關(guān)信息的實例
這篇文章主要介紹了Delphi實現(xiàn)獲取進程列表及相關(guān)信息的實例,希望通過本文大家能實現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09