2012-11-28 2 views
5

私はDelphi XE-3を使用しています。 チェックリストボックス内の単一項目の色またはフォントに変更したいと考えています。 これは可能ですか?特定のChecklistboxアイテムのフォントまたは色を変更していますか?

+0

または類似/ HTTPに複製VTW –

+0

ようchecklistboxウィットh3rdパーティのコントロールをシミュレートします://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