首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否更改特定Checklistbox项的字体或颜色?

是否更改特定Checklistbox项的字体或颜色?
EN

Stack Overflow用户
提问于 2012-11-28 21:47:05
回答 1查看 8.6K关注 0票数 5

我正在使用Delphi XE-3。我希望更改复选列表框中单个项目的颜色或字体。这个是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-28 22:50:47

您将需要对复选列表框使用所有者描述。将复选列表框的Style属性设置为lbOwnerDrawFixed,并编写OnDrawItem事件的处理程序。在此事件处理程序中,您可以使用类似以下内容:

代码语言:javascript
复制
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;

下面是上面例子的结果:

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13606562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档