listview 可折叠类,添加后可以使listview 具有折叠功能。

本文介绍了一种使用 C++ 在 ListView 控件中实现分组的方法,通过自定义 TGroupItem 和 TItem 类来创建可展开/折叠的项目组。每个组可以包含多个子项,并且能够响应双击事件来展开或收起子项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此类来自网络,非本人所写,但是找不到原地址了,只是在贴子中找到的。

原贴地址: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 };
};








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值