Listview控件相关问题以及解决代码之一

当listview的HideSelection设置为false时,如何修改当listview控件失去焦点时被选中纪录的颜色(此时listview控件默认被选纪录为灰色)为指定的颜色(如深蓝色)。

listview的HideSelection设置为true

void __fastcall TForm1::ListView1AdvancedCustomDrawItem(
      TCustomListView *Sender, TListItem *Item, TCustomDrawState State,
      TCustomDrawStage Stage, bool &DefaultDraw)
{
  if (Item->Selected)
  {
    Sender->Canvas->Font->Color = clWhite;
    Sender->Canvas->Brush->Color = clActiveCaption;
  }

}

如何在ListView中把现有的Item改变背景或字体的颜色?

在OnCustomDrawItem事件添加代码
    if(Item->Index % 2)
    { 
        ListView1->Canvas->Brush->Color = clBtnFace;  // 背景为灰色
        ListView1->Canvas->Font->Color  = clBlue;     // 字体为蓝色
    }
只是改变了新增加进来的Item的颜色,原有的没有改变,该怎么改变原有的Item颜色?

让ListView重绘,比如在需要刷新时调用:
ListView->Invalidate();

在Listview中查找数据,如何使得匹配数据的行变色或是被选中的样子呢??

TSearchPara SearchPara;
    TfrmFind *Frm=new TfrmFind(this,&SearchPara);
    if((Frm->ShowModal()) == mrOk)
    {
        TListItem *Item;
        for(int i = 0;iItems->Count;i++)
        {
         Item = ListView_Event->Items->Item[i];
         if(SearchPara.EventNo == Item->SubItems->Strings[2])
         {
            ListView_Event->Items->Item[i]->Selected = true;
            ListView_Event->SetFocus();
         }
        }
    }

ListView使用的代码

listview有vsIcon,vsList,vsReport,vsSmallIcon这4中模式!

我一般是只用vsReport,就是跟数据表格一样!

ListView->ViewStyle = vsReport;

ListView->Items->Clear();
ListView->Items->BeginUpdate();
TListItem* NewItem1= ListView4->Items->Add()
NewItem1->SubItems->Add("字符串");//Item第一列是caption
...
ListView->Items->EndUpdate();

TListItem* NewItem1= ListView4->Items->Item[ListView4->Selected->Index];//指向选择行
or ListView4->Items->Item[k]; //指向第k行

第1列= NewItem1->Caption;
第2列= NewItem1->SubItems->Strings[0];
第3列= NewItem1->SubItems->Strings[1];
第4列= NewItem1->SubItems->Strings[2];

怎样使行中不同的数据有不同的颜色表示
-------------------------------------------
在OnCustomDrawItem事件或者OnCustomDrawSubItem事件中谁定能够Brush的颜色,如:
void __fastcall TForm1::ListView1CustomDrawItem(TCustomListView *Sender,
      TListItem *Item, TCustomDrawState State, bool &DefaultDraw)
{
    if (Item->Index==3) //将第4行的第一列底色设为黄色
        ListView1->Canvas->Brush->Color = clYellow;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListView1CustomDrawSubItem(TCustomListView *Sender,
      TListItem *Item, int SubItem, TCustomDrawState State,
      bool &DefaultDraw)
{
    //将第2行的第二列底色设为浅灰色,其它为白色
    if (Item->Index==1 && SubItem==1)
        ListView1->Canvas->Brush->Color = clLtGray;
    else
        ListView1->Canvas->Brush->Color = clWhite;
}

listview为report模式,如何根据listview中某个item的第二列的值改变本item的bigicon和smallicon?

bigicon和smallicon分别放在ListView的BigImages和SmallImages指定的TImages控件里;
在ListView的OnCustomDrawItem事件里添加代码:
{
  //以item第二列的值为索引值选择图标。注:列值非法当作0;超出范围当作-1
  Item->ImageIndex = Item->SubItems->Strings[0].ToIntDef(0);
}
Sorry,应该是LargeImages和SmallImages……

鼠标单击listview的item的事件应该怎么写在哪里?

在onclick中写
如下代码,给你参考:
if(ListView1->ItemIndex<0) return;  //点击了listview的其他区域
if(ListView1->Selected->Caption=="a")
ShowMessage("a");
else
ShowMessage("no");

ListView自画小问题

void __fastcall TForm1::ListView1AdvancedCustomDraw(
      TCustomListView *Sender, const TRect &ARect, TCustomDrawStage Stage,
      bool &DefaultDraw)
{
ListView1->Canvas->Pen->Color=clRed;
ListView1->Canvas->LineTo(ListView1->Width,0);
ListView1->Canvas->LineTo(ListView1->Width,ListView1->Height);
ListView1->Canvas->LineTo(0,ListView1->Height);
ListView1->Canvas->LineTo(0,0);
}

本意是把四周都画上一条红线,可是只画上了上边和左边的,其他两边没有,哪里有问题么?

边界问题.
Width和Height都-5

TListView中怎样在指定行用不同的颜色显示啊?

提供另外一个例子供参考
void __fastcall TForm1::DBGrid1DrawDataCell(TObject *Sender,const TRect &Rect, TField *Field, TGridDrawState State)
{
  if(Field->DataSet->RecNo%2 == 0 )
    DBGridEh1->Canvas->Brush->Color = clCream;
  else
    DBGridEh1->Canvas->Brush->Color = clWindow;
  if(State.Contains(gdSelected) || State.Contains(gdFocused))
    DBGridEh1->Canvas->Font->Color = clBlue;
  else
    DBGridEh1->Canvas->Font->Color = clBlack;
  DBGridEh1->DefaultDrawDataCell(Rect, Field, State);

TListView没有DefaultDrawDataCell方法啊?也没有找到对应的方法,有什么可以替代的吗?

在OnCustomDrawItem事件里写:

void __fastcall TForm1::ListView1CustomDrawItem(TCustomListView *Sender,
      TListItem *Item, TCustomDrawState State, bool &DefaultDraw)
{
    if (Item->Index % 2 == 0)
        ListView1->Canvas->Brush->Color = clCream;
    else
        ListView1->Canvas->Brush->Color = clWhite;
}
}

你可能感兴趣的:(C++,Builder软件开发自用文档)