首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

ListBox->OndreawItem有关问题请问

2012-03-08 
ListBox-OndreawItem问题请教void_fastcallTForm1::ListBox2DrawItem(TWinControl*Control,intIndex,TRec

ListBox->OndreawItem问题请教
void   _fastcall   TForm1::ListBox2DrawItem(TWinControl   *Control,   int   Index,   TRect   &Rect,   TOwnerDrawState   State)
{
ListBox2-> Canvas-> Brush-> Style   =   bsClear;
ListBox2-> Canvas-> Font-> Color   =   clGray;
ListBox2-> Canvas-> TextOutA(Rect.Left,Rect.Bottom,ListBox2-> Items-> Strings[Index]);
}

用上述代码给指定Index行着色时,绝对不是显卡问题,某些行需选中并点击,才能将内容显示出来,为什么ListBox内容显示得不是很清楚?

如果不在ListBox的OnDrawItem里调用重画事件,即不采用BCB自动生成的函数ListBox2DrawItem(TWinControl   *Control,   int   Index,   TRect   &Rect,   TOwnerDrawState   State)
,如何在外部添加代码对某行进行着色或者修改字体颜色呢?

[解决办法]
ListBox2-> Canvas-> TextOutA(Rect.Left,Rect.Bottom,ListBox2-> Items-> Strings[Index]);
改为
ListBox2-> Canvas-> TextOutA(Rect.Left,Rect.Top,ListBox2-> Items-> Strings[Index]);

即Bottom-> Top
[解决办法]
int nItem = -1;
TColor nItemColor;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
nItem = 2;
nItemColor = clRed;
ListBox2-> Repaint();
}

void __fastcall TForm1::ListBox2DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
ListBox2-> Canvas-> Brush-> Style = bsClear;
ListBox2-> Canvas-> Font-> Color = nItem == Index ? nItemColor : ListBox2-> Font-> Color;;
ListBox2-> Canvas-> TextOutA(Rect.Left,Rect.Top,ListBox2-> Items-> Strings[Index]);
}

热点排行