listbox有什么办法可以让字体随意更改颜色

绍兴老鼠 2017-10-21 08:55:51
具体是这样的:
procedure TForm2.ListBox2DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
s: string;
rec:Trect;
begin
with TListBox(Control).Canvas do
begin
//if (odSelected in State) or (odFocused in State) then
//begin
//Brush.Color := clred;

case txtcolor of
1:Font.Color := clwindowtext;
2:Font.Color := clred;
3:Font.Color := cllime;
4:Font.Color := clfuchsia;
5:Font.Color := clblue;
end;
font.Name:='MS Sans Serif';
font.Size:=8;
font.Height:=-11;

//s := TListBox(Control).Items[Index];
//DrawText(Handle, PChar(s), Length(s), rect, DT_SingleLine or DT_VCenter);
ListBox2.Canvas.FillRect(Rect);
ListBox2.Canvas.TextOut(Rect.Left,Rect.Top,ListBox2.Items.Strings[Index]);
end;
end;
然后我在其他地方根据需要改变这个txtcolor值,但是结果得到的是全部区域都变成了指定的颜色。
我还试图改变rect,根据出现了这一行的itemindex*行高来指定rect(),这样得到的是只打印了这一行文字,其他的都不见了。
我的理解是重绘把其他的一起重绘掉了。
哪位前辈有什么方法吗。不能指定行来设置颜色,我是一个游戏根据战斗情况发送不同颜色的提示。
谢谢大家!
或者还有没有更快捷的控件呢。
我本来想用delphix里面的画布显示的,但是单步走吓死人,所以不到那步我也不想用它。
...全文
922 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
绍兴老鼠 2017-10-21
  • 打赏
  • 举报
回复
已经有了解决方案。 var a:array of integer; i:integer; setlength(a,10000); for i:=0 to 9999 do beign a[i]:=0; end; listbox重绘事件里面写: if a[index] = 0 then listbox1.canvas.font.color:=clblack; if a[index] = 1 then listbox1.canvas.font.color:=clred; ... 然后在想输出字体的时候之前写上 a[listbox1.item.count]:=1 红色 原理就是在将要绘制这一行的时候把这一行的颜色变为红色,由于重绘的时候会对所有index进行重绘,所以用了数组来给每一行指定一个唯一的标记。 行数到10000的时候需要清理。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧