TListView在虚拟模式下限制为100,000,000项?

时间:2014-01-17 08:01:48

标签: windows delphi listview windows-7 delphi-xe4

将ListView中的Items.Count设置为超过100,000,000的任何数字与将计数设置为0具有相同的结果 - 这是底层Windows控件的限制,还是Delphi特定的?我预计限制为20亿,因为Delphi XE4的文档说限制是(签名的)DWORD的大小(即:2 ^ 31 - 1)。

简单示例:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure ListView1Data(Sender: TObject; Item: TListItem);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Assumes ListView1.OwnerData := True;
  ListView1.Items.Count := 100000001; // Works if 100000000 is used instead
end;

procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
  Item.Caption := Item.Index.ToString();
end;

end.

我做了一些事情,将LVM_SETITEMCOUNT直接发送到底层控件,但它返回任何LPARAM超过100,000,000的错误并将内部计数设置为0,这让我相信它是底层控件的限制。我无法在任何地方找到这个记录 - 虽然我认为拥有那么多项并不常见。假设这是控件的限制,应该提交一个Delphi错误报告,因为当调用失败时TListView不会抛出任何异常 - 它只是默默地打破所有内容。

现在,我正在解决这个问题,方法是将listview保持在虚拟模式之外,根据控件的大小(即:VisibleRowCount属性)添加到列表中,确保可见的项目数量,保持偏移到我的数据中,并循环遍历列表中的项目以与虚拟模式列表基本相同的方式填充列表,使用我自己的滚动条来控制偏移量实际上限制为~20亿。

这种行为有办法解决吗?任何有使用大量数据和ListView经验的人的见解吗?

2 个答案:

答案 0 :(得分:3)

这确实是潜在控制的限制。您需要更改UI设计以避免限制,或者找到不同的控件。也就是说,我怀疑有很多控件可以有效地显示1亿个项目。

答案 1 :(得分:3)

这是我的新解决方法,它将虚拟ListView可以显示的最大项目增加到9223372036854775807(2 ^ 63 - 1),以防它对任何人都有用。将其视为虚拟虚拟ListView。通过一些工作,可以将其扩展为适用于所有视图,而不仅仅是列表或详细信息。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, System.Math;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    ScrollBar1: TScrollBar;
    procedure ListView1Data(Sender: TObject; Item: TListItem);
    procedure ListView1Resize(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
      var ScrollPos: Integer);
  private
    { Private declarations }
    Offset: Int64;
    ItemCount: Int64;
    VisibleItems: Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Assumptions:
// ListView1.OwnerData := True
// ListView1.ViewStyle := vsReport with columns set up OR vsList
// ScrollBar1.Min := 0;
// 
// The position of the scrollbar represents equally spaced points along the data
// You can increase this to any number the scrollbar supports
// By default, that means 101 points (0-100), from offset 0 to (ItemCount - VisibleItems + 1)

const
  LISTVIEW_VIRTUALITEMS_MAX = 100000000;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ItemCount := High(Int64); // For testing
  // Make sure the listview shows enough items
  ListView1.Items.Count := Min(ItemCount, LISTVIEW_VIRTUALITEMS_MAX);
end;

procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
var
  Index: Int64;
begin
  // Item.Index now represents an offset from an offset, adding them together
  // gives the true index
  Index := Offset + Item.Index;
  Item.Caption := Index.ToString; // Testing
end;

procedure TForm1.ListView1Resize(Sender: TObject);
begin
  VisibleItems := ListView1.VisibleRowCount;
  if VisibleItems = 0 then VisibleItems := 1;
  ListView1.Items.Count := VisibleItems;
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  ListView1.Refresh;
end;

procedure TForm1.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
  var
  MaxOffset: Int64;
begin
  // Int64 support for scrollbar, etc
  MaxOffset := ItemCount - VisibleItems + 1;
  case ScrollCode of
    TScrollCode.scLineUp: begin
      if Offset > 0 then
        Offset := Offset - 1;
    end;
    TScrollCode.scLineDown: begin
      if Offset < MaxOffset then
        Offset := Offset + 1;
    end;
    scPageUp: begin
      if Offset > VisibleItems then
        Offset := Offset - VisibleItems
      else
        Offset := 0;
    end;
    scPageDown: begin
      if (MaxOffset - Offset) > VisibleItems then
        Offset := Offset + VisibleItems
      else
        Offset := MaxOffset;
    end;
    scPosition, scTrack: begin
      Offset := Trunc((ScrollPos / Scrollbar1.Max) * MaxOffset);
      Exit;
    end;
    scTop: begin
      Offset := 0;
      Exit;
    end;
    scBottom: begin
      Offset := MaxOffset;
      Exit;
    end;
    scEndScroll: begin
    end;
  end;
  ScrollPos := Trunc((Offset / ItemCount) * ScrollBar1.Max);
  ListView1.Refresh;
end;

end.
相关问题