是否可以更改 TListBox 中的项目选择焦点颜色和文本颜色?
当项目中未启用主题,或者列表框样式设置为所有者绘制时,项目周围的选择被涂成蓝色,我相信这是由系统的外观设置全局定义的。
我想将所选项目的颜色更改为自定义颜色。
举个例子,结果是这样的:
请注意,最后一个列表框已在 Paint 中进行了修改以说明示例。
是否可以更改 TListBox 中的项目选择焦点颜色和文本颜色?
当项目中未启用主题,或者列表框样式设置为所有者绘制时,项目周围的选择被涂成蓝色,我相信这是由系统的外观设置全局定义的。
我想将所选项目的颜色更改为自定义颜色。
举个例子,结果是这样的:
请注意,最后一个列表框已在 Paint 中进行了修改以说明示例。
试试这个:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
with (Control as TListBox).Canvas do
begin
if odSelected in State then
Brush.Color := $00FFD2A6;
FillRect(Rect);
TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
if odFocused In State then begin
Brush.Color := ListBox1.Color;
DrawFocusRect(Rect);
end;
end;
end;
我看到了,Style 属性必须是 lbOwnerDrawFixed
这帮助我完成了我还需要做的事情,即消除任何可见的选择。我稍微修改了上面的代码来完成这个:
procedure TForm1.OnDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
with (Control as TListBox).Canvas do
begin
if odSelected in State then
begin
Brush.Color := clWhite;
Font.Color := clBlack;
end;
FillRect(Rect);
TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
if odFocused In State then begin
Brush.Color := ListBox1.Color;
DrawFocusRect(Rect);
end;
end;
end;
使所选项目的背景颜色为白色,字体颜色为黑色,这符合我的需要。非常感谢!