gpt4 book ai didi

delphi - TVirtualStringTree 中的图标绘制异常

转载 作者:行者123 更新时间:2023-12-03 15:46:08 28 4
gpt4 key购买 nike

这是重现问题的非常小的项目(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,其中仅包含一个图标。完毕。现在我们可以运行程序并在将鼠标光标移动到图标上时出现异常:

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

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

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

最佳答案

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;

关于delphi - TVirtualStringTree 中的图标绘制异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43185548/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com