TVirtualStringTree中的图标绘有异常

时间:2017-04-03 13:10:51

标签: delphi delphi-10.1-berlin virtualtreeview tvirtualstringtree

这是重现问题的非常小的项目(VirtualStringTree版本6.5):

type
  TForm1 = class(TForm)
    vstTest: TVirtualStringTree;
    Images: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure vstTestGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
      TextType: TVSTTextType; var CellText: string);
    procedure vstTestGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode; Kind: TVTImageKind;
      Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: TImageIndex);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  PTestRec = ^TTestRec;
  TTestRec = record
    Col1: string;
    Col2: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Node: PVirtualNode;
  Data: PTestRec;
begin
  vstTest.Header.Columns.Add;
  vstTest.Header.Columns.Add;
  vstTest.Header.Options := vstTest.Header.Options + [hoVisible];
  vstTest.Images := Images;
  vstTest.NodeDataSize := SizeOf(TTestRec);
  Node := vstTest.AddChild(nil);
  Data := vstTest.GetNodeData(Node);
  Data.Col1 := 'Col1';
  Data.Col2 := 'Col2';
end;

procedure TForm1.vstTestGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: TImageIndex);
begin
  if Column = 0 then
    ImageIndex := 0;
end;

procedure TForm1.vstTestGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType; var CellText: string);
var
  Data: PTestRec;
begin
  Data := vstTest.GetNodeData(Node);
  case Column of
    0: CellText := Data.Col1;
    1: CellText := Data.Col2;
  end;
end;

表单包含ImageList,它只包含一个图标。完成。现在我们可以运行程序并在将鼠标光标移到Icon上时获得异常:

异常类EAssertionFailed,带有消息'为TVTImageKind.ikState提供了图像索引,但未提供图像列表。 (C:\ Program Files \ VirtualTreeView \ Source \ VirtualTrees.pas,第20248行)'。处理Project1.exe(3232)

正如您所看到的,我没有使用StateImages和OnGetImageIndexEx。为什么这个?这是来自12635行的VirtualTrees代码

WithStateImages := Assigned(FStateImages) or Assigned(OnGetImageIndexEx);

1 个答案:

答案 0 :(得分:6)

procedure TForm1.vstTestGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean; 
  var ImageIndex: TImageIndex);
begin
  if Column = 0 then
    ImageIndex := 0;
end;

此代码忽略Kind参数。该参数可以包含此枚举中的一个值:

TVTImageKind = (
  ikNormal,
  ikSelected,
  ikState,
  ikOverlay
);

如果您返回ikState的值,那么您还必须提供状态图像。这就是错误消息告诉你的内容。我想你的事件处理程序应该区别对待:

procedure TForm1.vstTestGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean; 
  var ImageIndex: TImageIndex);
begin
  case Kind of
  ikNormal, ikSelected:
    if Column = 0 then
      ImageIndex := 0;
  end;
end;