Delphi XE-3 を使用しています。チェックリストボックスの単一項目の色やフォントを変更したい。これは可能ですか?
8707 次
1 に答える
11
チェックリストボックスには所有者の描画を使用する必要があります。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;
上記の例の結果は次のとおりです。
于 2012-11-28T14:50:47.170 に答える