从 ListView 切换到 VirtualStringTree

发布于 2024-10-10 07:23:27 字数 647 浏览 4 评论 0原文

我正在尝试使用 VirtualStringTree 而不是 Listview 来构建我的项目,因为速度差异巨大。问题是,即使在浏览了演示之后,我还是无法确切地弄清楚如何将它用作 ListView。就像添加、删除以及基本上使用 ListView 项目一样简单,但是当我查看 VT 时,它几乎变得太复杂了。

我所寻找的只是一个看起来像ListView的VT,带有子项等。

以下是一些使用ListView的例程,我想将它们与VT一起使用(这只是一个伪示例:

procedure Add;
begin
  with ListView.Items.Add do
    Begin
      Caption := EditCaption.Text;
      SubItems.Add(EditSubItem.Text):
    End;

end;

Procedure ReadItem(I : Integer);
begin

   ShowMessage(ListView.Items[I].Caption);
   ShowMessage(ListView.Items[I].SubItems[0]);

end;

当然,还有删除功能,但因为那就像 1 行,我没有打扰:P

有人可以将上面的示例翻译为使用 ListView 样式 VT

谢谢!

I am trying to build my projects with a VirtualStringTree rather than a Listview, because of the vast speed difference. The thing is, even after looking thru the demo's, I just can't figure out exactly how I would use it as a ListView. Like, adding, deleting, and basically just working with ListView items is so easy, but when I look at the VT, it gets almost too complicated.

All I am looking for, is a VT that looks like a ListView, with subitems etc.

Here are some routines using the ListView, that I would like to use with VT (This is just a pseudo example:

procedure Add;
begin
  with ListView.Items.Add do
    Begin
      Caption := EditCaption.Text;
      SubItems.Add(EditSubItem.Text):
    End;

end;

Procedure ReadItem(I : Integer);
begin

   ShowMessage(ListView.Items[I].Caption);
   ShowMessage(ListView.Items[I].SubItems[0]);

end;

Of course, also the Delete function, but since thats like 1 line, I didnt bother :P

Could anyone maybe translate the above examples into using a ListView style VT?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

看轻我的陪伴 2024-10-17 07:23:27

为什么不在虚拟模式下使用列表视图?这看起来、感觉起来都不错,而且性能也很棒。

Delphi TListView 控件是 Windows 列表视图组件的包装器。在其默认操作模式下,列表数据的副本将从您的应用程序传输到 Windows 控件,这很慢。

在 Windows 术语中,替代方法称为虚拟列表视图。您的应用程序不会将数据传递给 Windows 控件。相反,当控件需要显示数据时,它只会向您的应用程序询问所需的数据。

Delphi TListView 控件通过使用 OwnerData 属性公开虚拟列表视图。您必须稍微重写列表视图代码,但这并不太难。

我还提供了另一个问题的链接 这里涵盖了类似的内容。相当奇怪的是,该问题的接受答案谈到了列表框,尽管问题是关于列表视图控件的。

Why don't you use a list view in virtual mode? That will look and feel right and perform great.

The Delphi TListView control is a wrapper around the Windows list view component. In its default mode of operation copies of the list data are transferred from your app to the Windows control and this is slow.

The alternative to this is known as a virtual list view in Windows terminology. Your app doesn't pass the data to the Windows control. Instead, when the control needs to display data it asks your app for just the data that is needed.

The Delphi TListView control exposes virtual list views by use of the OwnerData property. You'll have to re-write your list view code somewhat but it's not too hard.

I also offer a link to another question here that covered similar ground. Rather oddly, the accepted answer for that question talked about list boxes even though the question was about list view controls.

不爱素颜 2024-10-17 07:23:27

使用 VirtualStringTree 它比简单的 TListView 稍微复杂一些,但是这里有一个非常简单的教程,我不久前创建了有关如何使用 VirtualStringTree http://www.youtube.com/watch?v=o6FpUJhEeoY 我希望它有帮助,干杯!

with VirtualStringTree it's a bit more complex than the simple TListView, however here's a very simple tutorial that I've created a little while back on how to use VirtualStringTree http://www.youtube.com/watch?v=o6FpUJhEeoY I hope it helps, cheers!

夏九 2024-10-17 07:23:27

只需使用普通的 TListView,但在虚拟模式下使用它。

这非常简单:

  1. OwnerData 属性设置为 true
  2. 实现 OnData 事件处理程序。

显示 3 行简单列表的示例实现:

Type TMyItem=record
  Item:String;
  SubItem:String;
end;

var Items:Array of TMyItem;

// set up some in-memory dataset.. choose your own layout
SetLength(Items,3);
Items[0].Item := 'foo1';
Items[0].SubItem := 'bar1';

Items[1].Item := 'foo2';
Items[1].SubItem := 'bar2';

Items[2].Item := 'foo3';
Items[2].SubItem := 'bar3';

// tell ListView1 how many items there are
ListView1.Items.Count := Length(Items); 

procedure TfrmMain.ListView1Data(Sender: TObject; Item: TListItem);
begin
  Item.Caption := IntToStr(Item.Index);
  Item.SubItems.Add( MyArray[Item.Index] );
  Item.SubItems.Add( UpperCase(MyArray[Item.Index]) );
end;

// Updating a value:
Items[1].Item := 'bzzz';
ListView1.Update;

仅此而已!

需要记住的一些事情:

  1. 您不再调用 ListView1.Items.Add() 。
  2. 您需要将自己的数据列表保存在内存中的某个位置,或者实时提供数据,因此您不能再在列表视图中“存储”数据。
  3. 您需要设置 items.count 属性,否则您将看不到任何内容。
  4. 如果发生变化,请调用 ListView1.Update()。

Just use your normal TListView, but use it in virtual mode.

It's really simple:

  1. Set the OwnerData property to true
  2. Implement the OnData event handler.

Sample implementation that shows a simple list of 3 rows:

Type TMyItem=record
  Item:String;
  SubItem:String;
end;

var Items:Array of TMyItem;

// set up some in-memory dataset.. choose your own layout
SetLength(Items,3);
Items[0].Item := 'foo1';
Items[0].SubItem := 'bar1';

Items[1].Item := 'foo2';
Items[1].SubItem := 'bar2';

Items[2].Item := 'foo3';
Items[2].SubItem := 'bar3';

// tell ListView1 how many items there are
ListView1.Items.Count := Length(Items); 

procedure TfrmMain.ListView1Data(Sender: TObject; Item: TListItem);
begin
  Item.Caption := IntToStr(Item.Index);
  Item.SubItems.Add( MyArray[Item.Index] );
  Item.SubItems.Add( UpperCase(MyArray[Item.Index]) );
end;

// Updating a value:
Items[1].Item := 'bzzz';
ListView1.Update;

That's all!

Some things to keep in mind:

  1. You don't call ListView1.Items.Add() anymore.
  2. You need to keep your own list of data somewhere in memory, or come up with the data in real-time, so you cannot 'store' data in the listview any longer.
  3. You need to set the items.count property, or you won't see anything.
  4. Call ListView1.Update() if something changes.
隔岸观火 2024-10-17 07:23:27
procedure Add;
Var
  Data: PLogData;
  XNode: PVirtualNode;
begin
  with vst do
    Begin
      XNode := AddChild(nil);
      ValidateNode(XNode, False);
      Data := GetNodeData(Xnode); 
      Data^.Name:= EditCaption.Text;
      Data^.Msg := EditSubItem.Text;
    End;

end;

Procedure ReadItem(I : Integer);
var
  Data: PLogData;
begin
  if not Assigned(vst.FocusedNode) then Exit;

  Data := vst.GetNodeData(vst.FocusedNode);
  ShowMessage(Data^.Name);
  ShowMessage(Data^.Msg);

end;

基本上这就是您需要做的,但是 VirtualStringTree 有/需要很多其他东西一起工作才能完全理解它。一旦您“掌握”了 VST,它就会变得简单而强大。以下网页将为您提供帮助:http://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus

,下面我将添加我使用更多代码来显示简单的 VST 日志。我将所有代码保留在数据模块中,只需使用程序 Log 来显示信息并将您的 FormMain.vstLog 更改为您的

unit udmVstLog;

interface

uses
  SysUtils, Windows, Forms, Classes, Graphics,
  VirtualTrees, ActnList, Dialogs, ExtDlgs;

type
  PLogData = ^TLogData;
  TLogData = record
    IsErr   : Boolean;
    Name: String;
    Msg : String;
  end;

type
  TdmVstLog = class(TDataModule)
    actlst1: TActionList;
    actClear: TAction;
    actSave: TAction;
    actCopyLine2Mem: TAction;
    sdlgLog: TSaveTextFileDialog;
    procedure DataModuleCreate(Sender: TObject);
    procedure actClearExecute(Sender: TObject);
    procedure actSaveExecute(Sender: TObject);
    procedure actCopyLine2MemExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
    procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
    procedure VSTPaintText(Sender: TBaseVirtualTree;
      const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
      TextType: TVSTTextType);
  end;

  procedure Log(aIsErr: Boolean; AName, AMsg: string); overload;
  procedure Log(AName, AMsg: string); overload;
  procedure Log(AMsg: string); overload;

var
  dmVstLog: TdmVstLog;

implementation

uses uFormMain, ClipBrd;

{$R *.dfm}
procedure Log(aIsErr: Boolean; AName, AMsg: string);
Var
  Data: PLogData;
  XNode: PVirtualNode;
begin
  XNode:=FormMain.vstLog.AddChild(nil);
  FormMain.vstLog.ValidateNode(XNode, False);
  Data := FormMain.vstLog.GetNodeData(Xnode);
  Data^.IsErr := aIsErr;
  if aIsErr then
    Data^.Name:= DateTimeToStr(Now) + ' ERROR ' + AName
  else
    Data^.Name:= DateTimeToStr(Now) + ' INFO ' + AName;
  Data^.Msg:= AMsg;
end;

procedure Log(AName, AMsg: string);
begin
  Log(False,AName,AMsg);
end;

procedure Log(AMsg: string);
begin
  Log(False,'',AMsg);
end;



// VirtualStringTree Events defined here
procedure TdmVstLog.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
  Data: PLogData;
begin
  Data:=Sender.GetNodeData(Node);
  Finalize(Data^);
end;

procedure TdmVstLog.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
 Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
var
  Data: PLogData;
begin
  Data := Sender.GetNodeData(Node);
  case Column of
    0: CellText := Data^.Name + ' - '+ Data^.Msg;
  end;
end;

procedure TdmVstLog.VSTPaintText(Sender: TBaseVirtualTree;
  const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType);
Var
  Data: PLogData;
begin
  Data := Sender.GetNodeData(Node);

  if Data^.IsErr then
    TargetCanvas.Font.Color:=clRed;

end;

//PopUpMenu Actions defined here!
procedure TdmVstLog.actClearExecute(Sender: TObject);
begin
  FormMain.vstLog.Clear;
end;

procedure TdmVstLog.actCopyLine2MemExecute(Sender: TObject);
var
  Data: PLogData;
begin
  if not Assigned(FormMain.vstLog.FocusedNode) then Exit;

  Data := FormMain.vstLog.GetNodeData(FormMain.vstLog.FocusedNode);
  ClipBoard.AsText := Data^.Name + ' - ' + Data^.Msg;
end;

procedure TdmVstLog.actSaveExecute(Sender: TObject);
Var
  XNode: PVirtualNode;
  Data: PLogData;
  ts: TStringList;
begin
  If FormMain.vstLog.GetFirst = nil then Exit;
  XNode:=nil;
  if sdlgLog.Execute then begin
    ts:= TStringList.create;
    try
      Repeat
        if XNode = nil then XNode:=FormMain.vstLog.GetFirst Else XNode:=FormMain.vstLog.GetNext(XNode);
        Data:=FormMain.vstLog.GetNodeData(XNode);
        ts.Add(Data^.Name + ' - '+ Data^.Msg);
      Until XNode = FormMain.vstLog.GetLast();
      ts.SaveToFile(sdlgLog.FileName);
    finally
      ts.Free;
    end;
  end;

end;

// Datamodule Events defined here
procedure TdmVstLog.DataModuleCreate(Sender: TObject);
begin
  with FormMain.vstLog do begin
    NodeDataSize := SizeOf(TLogData);
    OnFreeNode := VSTFreeNode;
    OnGetText := VSTGetText;
    OnPaintText := VSTPaintText;
  end;
end;

end.

......

procedure RemoveSelectedNodes(vst:TVirtualStringTree);
begin
  if vst.SelectedCount = 0 then Exit;
  vst.BeginUpdate;
  vst.DeleteSelectedNodes;
  vst.EndUpdate;
end;

procedure RemoveAllNodes(vst:TVirtualStringTree);
begin
  vst.BeginUpdate;
  vst.Clear;
  vst.EndUpdate;
end;
procedure Add;
Var
  Data: PLogData;
  XNode: PVirtualNode;
begin
  with vst do
    Begin
      XNode := AddChild(nil);
      ValidateNode(XNode, False);
      Data := GetNodeData(Xnode); 
      Data^.Name:= EditCaption.Text;
      Data^.Msg := EditSubItem.Text;
    End;

end;

Procedure ReadItem(I : Integer);
var
  Data: PLogData;
begin
  if not Assigned(vst.FocusedNode) then Exit;

  Data := vst.GetNodeData(vst.FocusedNode);
  ShowMessage(Data^.Name);
  ShowMessage(Data^.Msg);

end;

Basically that is what you need to do, but the VirtualStringTree has/needs alot of other things working together to fully understand it. And once you "get it" the VST is easy and powerful. The following webpage will help you: http://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus

and below I will add more code I use for a simple VST Log display. I keep all the code in datamodule, just use the procedure Log to display information and change your FormMain.vstLog to yours...

unit udmVstLog;

interface

uses
  SysUtils, Windows, Forms, Classes, Graphics,
  VirtualTrees, ActnList, Dialogs, ExtDlgs;

type
  PLogData = ^TLogData;
  TLogData = record
    IsErr   : Boolean;
    Name: String;
    Msg : String;
  end;

type
  TdmVstLog = class(TDataModule)
    actlst1: TActionList;
    actClear: TAction;
    actSave: TAction;
    actCopyLine2Mem: TAction;
    sdlgLog: TSaveTextFileDialog;
    procedure DataModuleCreate(Sender: TObject);
    procedure actClearExecute(Sender: TObject);
    procedure actSaveExecute(Sender: TObject);
    procedure actCopyLine2MemExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
    procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
      Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
    procedure VSTPaintText(Sender: TBaseVirtualTree;
      const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
      TextType: TVSTTextType);
  end;

  procedure Log(aIsErr: Boolean; AName, AMsg: string); overload;
  procedure Log(AName, AMsg: string); overload;
  procedure Log(AMsg: string); overload;

var
  dmVstLog: TdmVstLog;

implementation

uses uFormMain, ClipBrd;

{$R *.dfm}
procedure Log(aIsErr: Boolean; AName, AMsg: string);
Var
  Data: PLogData;
  XNode: PVirtualNode;
begin
  XNode:=FormMain.vstLog.AddChild(nil);
  FormMain.vstLog.ValidateNode(XNode, False);
  Data := FormMain.vstLog.GetNodeData(Xnode);
  Data^.IsErr := aIsErr;
  if aIsErr then
    Data^.Name:= DateTimeToStr(Now) + ' ERROR ' + AName
  else
    Data^.Name:= DateTimeToStr(Now) + ' INFO ' + AName;
  Data^.Msg:= AMsg;
end;

procedure Log(AName, AMsg: string);
begin
  Log(False,AName,AMsg);
end;

procedure Log(AMsg: string);
begin
  Log(False,'',AMsg);
end;



// VirtualStringTree Events defined here
procedure TdmVstLog.VSTFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
  Data: PLogData;
begin
  Data:=Sender.GetNodeData(Node);
  Finalize(Data^);
end;

procedure TdmVstLog.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
 Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
var
  Data: PLogData;
begin
  Data := Sender.GetNodeData(Node);
  case Column of
    0: CellText := Data^.Name + ' - '+ Data^.Msg;
  end;
end;

procedure TdmVstLog.VSTPaintText(Sender: TBaseVirtualTree;
  const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType);
Var
  Data: PLogData;
begin
  Data := Sender.GetNodeData(Node);

  if Data^.IsErr then
    TargetCanvas.Font.Color:=clRed;

end;

//PopUpMenu Actions defined here!
procedure TdmVstLog.actClearExecute(Sender: TObject);
begin
  FormMain.vstLog.Clear;
end;

procedure TdmVstLog.actCopyLine2MemExecute(Sender: TObject);
var
  Data: PLogData;
begin
  if not Assigned(FormMain.vstLog.FocusedNode) then Exit;

  Data := FormMain.vstLog.GetNodeData(FormMain.vstLog.FocusedNode);
  ClipBoard.AsText := Data^.Name + ' - ' + Data^.Msg;
end;

procedure TdmVstLog.actSaveExecute(Sender: TObject);
Var
  XNode: PVirtualNode;
  Data: PLogData;
  ts: TStringList;
begin
  If FormMain.vstLog.GetFirst = nil then Exit;
  XNode:=nil;
  if sdlgLog.Execute then begin
    ts:= TStringList.create;
    try
      Repeat
        if XNode = nil then XNode:=FormMain.vstLog.GetFirst Else XNode:=FormMain.vstLog.GetNext(XNode);
        Data:=FormMain.vstLog.GetNodeData(XNode);
        ts.Add(Data^.Name + ' - '+ Data^.Msg);
      Until XNode = FormMain.vstLog.GetLast();
      ts.SaveToFile(sdlgLog.FileName);
    finally
      ts.Free;
    end;
  end;

end;

// Datamodule Events defined here
procedure TdmVstLog.DataModuleCreate(Sender: TObject);
begin
  with FormMain.vstLog do begin
    NodeDataSize := SizeOf(TLogData);
    OnFreeNode := VSTFreeNode;
    OnGetText := VSTGetText;
    OnPaintText := VSTPaintText;
  end;
end;

end.

...

procedure RemoveSelectedNodes(vst:TVirtualStringTree);
begin
  if vst.SelectedCount = 0 then Exit;
  vst.BeginUpdate;
  vst.DeleteSelectedNodes;
  vst.EndUpdate;
end;

procedure RemoveAllNodes(vst:TVirtualStringTree);
begin
  vst.BeginUpdate;
  vst.Clear;
  vst.EndUpdate;
end;
咿呀咿呀哟 2024-10-17 07:23:27

获取 VT Contributions 包并查看 的一些后代虚拟字符串树。那些都在里面。我没有在项目中使用过它们,但它们似乎使虚拟字符串树更易于使用。


这里是我的入门入门:

在使用虚拟字符串树相当多之后,我发现充分利用它的唯一方法是实现 init 节点/子函数并设置根节点计数,与使用ownerdraw := true 的列表视图非常相似。

使用 VirtualStringTree 做事情非常容易,您只需要实现获取文本函数和节点大小函数(将其设置为等于您想要用作树背后的数据的任何记录的大小)

我发现这几乎总是更容易做到
<代码>
TVirtualTreeNodeRecordData = 记录
数据:TVirtualTreeNodeData;
结尾

并在 init 函数上创建数据对象。它为您创建指针,但您需要释放对象(再次使用另一个删除节点回调)。

Get the VT Contributions pack and check out some of the descendants of virtual string tree. That are in there. I haven't used them in projects, but they seem to make Virtual String Tree easier to use.


Here's my getting started primer nonetheless:

I've found after using Virtual String Tree quite a bit that the only way you can make the most of it is by implementing the init node/child functions and setting the root node count, much the same as you would a list view with ownerdraw := true.

It's pretty easy to do stuff with VirtualStringTree, you just need to implement the get text function and the node size functions (set it equal to the size of whatever record you'd like to use as the data behind your tree)

I've found it's almost always easier to do

TVirtualTreeNodeRecordData = record
Data : TVirtualTreeNodeData;
end

and create the data object on the init functions. It creates the pointers for you, but you need to free the objects (again, use another delete node callback).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文