604
社区成员




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 };
};
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TListView *lvGroups;
TImageList *listViewImages;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall lvGroupsAdvancedCustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, TCustomDrawStage Stage,
bool &DefaultDraw);
void __fastcall lvGroupsDblClick(TObject *Sender);
private: // User declarations
void __fastcall AddGroupItem(TGroupItem *gi);
void __fastcall ClearListViewGroups();
void __fastcall FillListViewGroups();
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
__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::FormCreate(TObject *Sender)
{
FillListViewGroups();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ClearListViewGroups();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::lvGroupsAdvancedCustomDrawItem(
TCustomListView *Sender, TListItem *Item, TCustomDrawState State,
TCustomDrawStage Stage, bool &DefaultDraw)
{
// bold group items
TObject *obj = (TObject *)(Item->Data);
if (obj && obj->ClassNameIs("TGroupItem"))
lvGroups->Canvas->Font->Style = lvGroups->Canvas->Font->Style << fsBold;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::lvGroupsDblClick(TObject *Sender)
{
TGroupItem *gi;
// THitTests hts = lvGroups->GetHitTestInfoAt(
// lvGroups->ScreenToClient(Mouse->CursorPos).x,
// lvGroups->ScreenToClient(Mouse->CursorPos).y);
if (lvGroups->Selected)
{
TObject *obj = (TObject *)lvGroups->Selected->Data;
if (obj && obj->ClassNameIs("TGroupItem"))
{
gi = (TGroupItem *)obj;
if (gi)
{
if (!gi->Expanded)
gi->Expand();
else
gi->Collapse();
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearListViewGroups()
{
TListItem *li;
TGroupItem *qng;
for (int i = 0; i < lvGroups->Items->Count; i++)
{
li = lvGroups->Items->Item[i];
if (li->Data && ((TObject *)(li->Data))->ClassNameIs("TGroupItem"))
{
qng = (TGroupItem *)li->Data;
if (qng)
delete qng;
}
}
lvGroups->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AddGroupItem(TGroupItem *gi)
{
TListItem *li = lvGroups->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 TForm1::ClearListViewGroups()
{
TListItem *li ;
TGroupItem *qng ;
for li in lvGroups.Items do
{
if( TObject(li.Data) == TGroupItem )
{
qng = TGroupItem(li->Data);
FreeAndNil(qng);
};
}
lvGroups->Clear;
}