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

ListView自绘时子项内容不显示,该如何解决

2013-01-26 
ListView自绘时子项内容不显示是不是要在别的事件中增加什么处理?void __fastcall TForm14::lv1DrawItem(T

ListView自绘时子项内容不显示
是不是要在别的事件中增加什么处理?


void __fastcall TForm14::lv1DrawItem(TCustomListView *Sender, TListItem *Item, TRect &Rect,
          TOwnerDrawState State)
{
  if( Item->Index == Sender->ItemIndex )
   {
 Sender->Canvas->Brush->Color = clMedGray ;
   }
  else
   {
  if(Item->Index %2 == 0 )
 Sender->Canvas->Brush->Color = clSkyBlue  ;
  else
 Sender->Canvas->Brush->Color = clMoneyGreen ;
   }

  Sender->Canvas->FillRect(Rect);
  Sender->Canvas->TextOutW(Rect.Left+2,
   (Rect.Height() - Sender->Canvas->TextHeight(Item->Caption))/2 + Rect.Top ,
   Item->Caption);
}

[解决办法]
好事做到底,给你一个代码。
void __fastcall TfrmTest::ListView1DrawItem(TCustomListView *Sender,
      TListItem *Item, TRect &Rect, TOwnerDrawState State)
{
    TListView *lv = (TListView *)Sender;
    lv->Canvas->Brush->Color = Item->Index % 2? clYellow: clGreen;

    if(State.Contains(odSelected))
        lv->Canvas->Brush->Color = clRed;
    lv->Canvas->FillRect(Rect);
    //
    int nOffsetLeft(2);
    if(lv->SmallImages)
    {
        lv->SmallImages->Draw(lv->Canvas, Rect.Left + 1, Rect.Top, Item->ImageIndex);
        nOffsetLeft += lv->SmallImages->Width;
    }
    lv->Canvas->Brush->Style = bsClear;
    lv->Canvas->TextRect(Rect, Rect.Left + nOffsetLeft, Rect.Top + 2, Item->Caption); // 显示文字

    int nItemWidth = 0;
    for(int i=0; i<Item->SubItems->Count; i++)
    {
        nItemWidth = nItemWidth + lv->Columns->Items[i]->Width;
        lv->Canvas->TextOut(Rect.Left + nItemWidth,
                Rect.Top + 2, Item->SubItems->Strings[i]); // 显示文字
    }
}


这次就要ListView1->OwnerDraw = true;了。

热点排行