2012-11-28 82 views
5

我正在使用Delphi XE-3。 我希望更改爲清單框中單個項目的顏色或字體。 這可能嗎?更改特定覈對清單項目的字體或顏色?

+0

或模擬checklistbox機智像VTW –

+0

類似h3rd方控制/複製到http ://stackoverflow.com/questions/8563508/how-do-i-draw-the-selected-list-box-item-in-a-different-color –

回答

10

您將需要使用所有者繪製您的檢查列表框。將您的檢查列表框的Style屬性設置爲lbOwnerDrawFixed,併爲OnDrawItem事件編寫處理程序。在此事件處理程序,你可以使用這樣的事情:

procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer; 
    Rect: TRect; State: TOwnerDrawState); 
var 
    Flags: Longint; 
begin 
    with (Control as TCheckListBox) do 
    begin 
    // modifying the Canvas.Brush.Color here will adjust the item color 
    case Index of 
     0: Canvas.Brush.Color := $00F9F9F9; 
     1: Canvas.Brush.Color := $00EFEFEF; 
     2: Canvas.Brush.Color := $00E5E5E5; 
    end; 
    Canvas.FillRect(Rect); 
    // modifying the Canvas.Font.Color here will adjust the item font color 
    case Index of 
     0: Canvas.Font.Color := clRed; 
     1: Canvas.Font.Color := clGreen; 
     2: Canvas.Font.Color := clBlue; 
    end; 
    Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX); 
    if not UseRightToLeftAlignment then 
     Inc(Rect.Left, 2) 
    else 
     Dec(Rect.Right, 2); 
    DrawText(Canvas.Handle, Items[Index], Length(Items[Index]), Rect, Flags); 
    end; 
end; 

這裏是上面的例子的結果:

enter image description here

+6

這不包括項目的狀態(如果它的重點,選擇與否)並忽略VCL樣式。 – TLama