此类来自网络,非本人所写,但是找不到原地址了,只是在贴子中找到的。
原贴地址:http://bbs.youkuaiyun.com/topics/320014726
//---------------------------------------------------------------------------
.cpp
__fastcall TGroupItem::TGroupItem(const String caption, int numberOfSubItems)
{
fItems = NULL;
fCaption = caption;
for (int cnt = 1; cnt < numberOfSubItems + 1; cnt++)
{
Items->Add(new TItem(caption + " item " + IntToStr(cnt), IntToStr(cnt)));
}
}
//---------------------------------------------------------------------------
__fastcall TGroupItem::~TGroupItem()
{
delete fItems;
}
//---------------------------------------------------------------------------
void __fastcall TGroupItem::Collapse()
{
if (!Expanded) return;
ListItem->ImageIndex = 1;
fExpanded = false;
TListItem *li = ((TListView *)(ListItem->ListView))->Items->Item[ListItem->Index + 1];
while (li && li->Data && ((TObject *)(li->Data))->ClassNameIs("TItem"))
{
((TListView *)(ListItem->ListView))->Items->Delete(li->Index);
li = ((TListView *)(ListItem->ListView))->Items->Item[ListItem->Index + 1];
}
}
//---------------------------------------------------------------------------
void __fastcall TGroupItem::Expand()
{
int cnt;
TItem *item;
if (Expanded) return;
ListItem->ImageIndex = 0;
fExpanded = true;
TListItem *li;
for (int cnt = 0; cnt < Items->Count; cnt++)
{
item = (TItem *)(Items->Items[cnt]);
li = ((TListView *)(ListItem->ListView))->Items->Insert(1 + cnt + ListItem->Index);
li->Caption = item->Title;
li->SubItems->Add(item->Value);
li->Data = item;
li->ImageIndex = -1;
}
}
//---------------------------------------------------------------------------
TObjectList *__fastcall TGroupItem::GetItems()
{
if (!fItems)
fItems = new TObjectList(true);
return fItems;
}
//---------------------------------------------------------------------------
__fastcall TItem::TItem(const String title, const String value)
{
fTitle = title;
fValue = value;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearListViewGroups()
{
TListItem *li;
TGroupItem *qng;
for (int i = 0; i < lv1->Items->Count; i++)
{
li = lv1->Items->Item[i];
if (li->Data)
{
if ( ((TObject *)(li->Data))->ClassNameIs("TGroupItem"))
{
qng = (TGroupItem *)li->Data;
if (qng)
delete qng;
}
}
}
lv1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AddGroupItem(TGroupItem *gi)
{
TListItem *li = lv1->Items->Add();
li->Caption = gi->Caption;
li->ImageIndex = 1; //collapsed
li->Data = gi;
gi->ListItem = li; //link "back"
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FillListViewGroups()
{
ClearListViewGroups();
AddGroupItem(new TGroupItem("Group A", 3));
AddGroupItem(new TGroupItem("Group B", 1));
AddGroupItem(new TGroupItem("Group C", 4));
AddGroupItem(new TGroupItem("Group D", 5));
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
FillListViewGroups();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::lv1DblClick(TObject *Sender)
{
TGroupItem *gi;
// THitTests hts = lvGroups->GetHitTestInfoAt(
// lvGroups->ScreenToClient(Mouse->CursorPos).x,
// lvGroups->ScreenToClient(Mouse->CursorPos).y);
if (lv1->Selected)
{
TObject *obj = (TObject *)lv1->Selected->Data;
if (obj && obj->ClassNameIs("TGroupItem"))
{
gi = (TGroupItem *)obj;
if (gi)
{
if (!gi->Expanded)
gi->Expand();
else
gi->Collapse();
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ClearListViewGroups();
}
.h文件
class TGroupItem: public TObject
{
private:
TObjectList *fItems;
String fCaption;
TListItem *fListItem;
bool fExpanded;
TObjectList *__fastcall GetItems();
public:
__fastcall TGroupItem(const String caption, const int numberOfSubItems);
__fastcall ~TGroupItem();
void __fastcall Expand();
void __fastcall Collapse();
__property bool Expanded = { read=fExpanded };
__property String Caption = { read=fCaption };
__property TObjectList *Items = { read=GetItems };
__property TListItem *ListItem = { read=fListItem, write=fListItem };
};
//---------------------------------------------------------------------------
class TItem: public TObject
{
private:
String fTitle;
String fValue;
public:
__fastcall TItem(const String title, const String value);
__property String Title = { read=fTitle };
__property String Value = { read=fValue };
};