如何在ComboBox控件中插入不同颜色的文字

zhyzhyxx 2004-04-13 04:49:20
我的程序必须时时检测系统情况,当接收到故障信息是需在ComboBox1中插入信息(文字颜色红色),正常状态信息在ComboBox1中的文字为黑色,怎样实现?
...全文
213 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JetKingLau 2004-04-13
  • 打赏
  • 举报
回复
设置 ComboBox1 的 Style 属性为 csOwnerDrawVariable 或 csOwnerDrawFixed,然后在 ComboBox1 的 OnDrawItem 函数中写:

void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
if( Control->ClassNameIs("TComboBox") ) {
TComboBox *cbb = (TComboBox*)Control;
cbb->Canvas->FillRect(Rect);//清空该行的背景区域
//根据 Index 或 cbb->Items->Strings[Index] 判断是否需要重画,或设置重画方式
cbb->Canvas->Font = cbb->Font;
cbb->Canvas->Font->Color = State. clRed;
int x, y;
AnsiString str = cbb->Items->Strings[Index];
x = Rect.left + 2;
y = Rect.top + Rect.Height() / 2 - cbb->Canvas->TextHeight(str) / 2;
cbb->Canvas->TextOutA(x, y, str);
}
}
zhyzhyxx 2004-04-13
  • 打赏
  • 举报
回复
写几行代码看看
wcccc1 2004-04-13
  • 打赏
  • 举报
回复
用canvas将字写上去就行了。

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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