如何以不同的颜色绘制所选的列表框项目? [英] How do I draw the selected list-box item in a different color?
问题描述
是否可以在TListBox中更改项目选择对焦颜色和文本颜色?
Is is possible to change the item selection focus color and text color in a TListBox?
当项目中未启用主题或列表框样式时设置为所有者绘制,项目周围的选择是蓝色的,我相信是由系统的外观设置全局定义的。
When themes are not enabled in the project, or the list box style is set to owner-draw, the selection around the item is painted blue, which I believe is globally defined by the system's appearance settings.
我想改变颜色选择的项目到自定义颜色。
I would like to change the color of selected items to a custom color.
所以一个例子,结果将是这样的:
So an example, the result would be something like this:
< img src =https://i.stack.imgur.com/cuGYH.pngalt =enter image description here>
注意最后一个列表框已经被在Paint中修改以说明示例。
Note the last listbox has been modified in Paint to illustrate the example.
推荐答案
尝试这样:
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;
这篇关于如何以不同的颜色绘制所选的列表框项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!