TListView分组 (Delphi代码转换成bcb)

213yy 2009-09-08 12:35:48
请熟悉的人帮忙转换成bcb,谢谢。
unit Unit1;

uses
Contnrs,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, ComCtrls;

type
TGroupItem = class
private
fItems : TObjectList;
fCaption: string;
fListItem: TListItem;
fExpanded: boolean;
function GetItems: TObjectList;
public
constructor Create(const caption : string; const numberOfSubItems : integer);
destructor Destroy; override;

procedure Expand;
procedure Collapse;

property Expanded : boolean read fExpanded;
property Caption : string read fCaption;
property Items : TObjectList read GetItems;
property ListItem : TListItem read fListItem write fListItem;
end;

TItem = class
private
fTitle: string;
fValue: string;
public
constructor Create(const title, value : string);
property Title: string read fTitle;
property Value : string read fValue;
end;


TForm1 = class(TForm)
lvGroups: TListView;
listViewImages: TImageList;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure lvGroupsAdvancedCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
procedure lvGroupsDblClick(Sender: TObject);
private
procedure ClearListViewGroups;
procedure FillListViewGroups;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.ClearListViewGroups;
var
li : TListItem;
qng : TGroupItem;
begin
for li in lvGroups.Items do
begin
if TObject(li.Data) is TGroupItem then
begin
qng := TGroupItem(li.Data);
FreeAndNil(qng);
end;
end;
lvGroups.Clear;
end;

procedure TForm1.FillListViewGroups;

procedure AddGroupItem(gi : TGroupItem);
var
li : TListItem;
begin
li := lvGroups.Items.Add;

li.Caption := gi.Caption;
li.ImageIndex := 1; //collapsed

li.Data := gi;
gi.ListItem := li; //link "back"
end;
begin
ClearListViewGroups;

AddGroupItem(TGroupItem.Create('Group A', 3));
AddGroupItem(TGroupItem.Create('Group B', 1));
AddGroupItem(TGroupItem.Create('Group C', 4));
AddGroupItem(TGroupItem.Create('Group D', 5));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FillListViewGroups;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
ClearListViewGroups;
end;

procedure TForm1.lvGroupsAdvancedCustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
var DefaultDraw: Boolean);
begin
//bold group items
if TObject(item.Data) is TGroupItem then
begin
lvGroups.Canvas.Font.Style := lvGroups.Canvas.Font.Style + [fsBold];
end;
end;

//handles TListView OnDblClick even
procedure TForm1.lvGroupsDblClick(Sender: TObject);
var
hts : THitTests;
gi : TGroupItem;
begin
inherited;

hts := lvGroups.GetHitTestInfoAt(lvGroups.ScreenToClient(Mouse.CursorPos).X, lvGroups.ScreenToClient(Mouse.CursorPos).y);

if (lvGroups.Selected <> nil) then
begin
if TObject(lvGroups.Selected.Data) is (TGroupItem) then
begin
gi := TGroupItem(lvGroups.Selected.Data);

if NOT gi.Expanded then
gi.Expand
else
gi.Collapse;
end;
end;
end;


{$region 'TGroupItem'}

procedure TGroupItem.Collapse;
var
li : TListItem;
begin
if NOT Expanded then Exit;

ListItem.ImageIndex := 1;
fExpanded := false;

li := TListView(ListItem.ListView).Items[ListItem.Index + 1];
while (li <> nil) AND (TObject(li.Data) is TItem) do
begin
TListView(ListItem.ListView).Items.Delete(li.Index);
li := TListView(ListItem.ListView).Items[ListItem.Index + 1];
end;
end;

constructor TGroupItem.Create(const caption: string; const numberOfSubItems : integer);
var
cnt : integer;
begin
fCaption := caption;

for cnt := 1 to numberOfSubItems do
begin
Items.Add(TItem.Create(caption + ' item ' + IntToStr(cnt), IntToStr(cnt)));
end;
end;

destructor TGroupItem.Destroy;
begin
FreeAndNil(fItems);
inherited;
end;

procedure TGroupItem.Expand;
var
cnt : integer;
item : TItem;
begin
if Expanded then Exit;

ListItem.ImageIndex := 0;
fExpanded := true;

for cnt := 0 to -1 + Items.Count do
begin
item := TItem(Items[cnt]);
with TListView(ListItem.ListView).Items.Insert(1 + cnt + ListItem.Index) do
begin
Caption := item.Title;
SubItems.Add(item.Value);
Data := item;
ImageIndex := -1;
end;
end;
end;

function TGroupItem.GetItems: TObjectList;
begin
if fItems = nil then fItems := TObjectList.Create(true);
result := fItems;
end;
{$endregion}

{$region 'TItem' }

constructor TItem.Create(const title, value: string);
begin
fTitle := title;
fValue := value;
end;
{$endregion}

end.
...全文
452 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Faintsnow 2012-03-13
  • 打赏
  • 举报
回复
有办法做到首行缩进么?
213yy 2009-09-10
  • 打赏
  • 举报
回复
谢谢ccrun,
高!
给分。
再次感谢ccrun
kouwenlong 2009-09-10
  • 打赏
  • 举报
回复
佩服,妖哥。
ccrun.com 2009-09-10
  • 打赏
  • 举报
回复
我来了。答应别人的事情,总要有个了结,只好自己多花一些时间了。

头文件中:
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));
}


以上代码在Windows 7, C++Builder 6.0中测试无误。
laowang2 2009-09-09
  • 打赏
  • 举报
回复
关注
ccrun.com 2009-09-09
  • 打赏
  • 举报
回复
不好意思楼主,昨天晚上已经翻译完了,但是调试还有故障,所以今天还需要再帮你测试好了再回复给你。
ccrun.com 2009-09-09
  • 打赏
  • 举报
回复
不好意思楼主,昨天晚上已经翻译完了,但是调试还有故障,所以今天还需要再帮你测试好了再回复给你。
213yy 2009-09-09
  • 打赏
  • 举报
回复
顶一下。
ding shang qu !
213yy 2009-09-08
  • 打赏
  • 举报
回复
由20分加到40分,最后直接加到100,请高手帮忙转换成bcb,谢谢。
xjq2003 2009-09-08
  • 打赏
  • 举报
回复
procedure TForm1.ClearListViewGroups;
var
li : TListItem;
qng : TGroupItem;
begin
for li in lvGroups.Items do
begin
if TObject(li.Data) is TGroupItem then
begin
qng := TGroupItem(li.Data);
FreeAndNil(qng);
end;
end;
lvGroups.Clear;
end;

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;
}
213yy 2009-09-08
  • 打赏
  • 举报
回复
谢谢ccrun
ccrun.com 2009-09-08
  • 打赏
  • 举报
回复
先挂个号,晚上再来。

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧