所以我正在使用Delphi 2010,自从我开始使用VirtualTreeView(确切地说是VirtualStringTree)以来已经有一段时间了..看来我正在以错误的方式做一些事情..因为事情没有按预期工作我期待着。
我正在尝试使用指向存储在数据记录中并通过扫描用户指定的路径生成的文件/子文件夹描述的节点来填充我的 VST。(更多详细信息如下图所示)
如图所示,节点以奇怪的方式显示..&无论我做什么,节点数据都没有正确初始化.."file"列的节点标题是唯一有效的。
这是我使用的代码:
1- 节点数据声明:
type nodeData=record
Text, Size, Path:String;
ImageIndex:Integer;
end;
PNodeData=^nodeData;
var hashmap:TDictionary<String, PVirtualNode>; // hashmap-> to store parent nodes (Folder)
filesList:TDictionary<Integer,nodeData>; // fileList to store records of data
<小时/>
2-方法
a) 扫描用户给出的路径
procedure AddAllFilesInDir(const Dir:String);
begin
// it scans the path 'Dir' and extract the "name & size" of each file/folder found in this dir
end;
b) 生成filesList tdictionary... 图像存储在链接到 treeview.images 属性的“treeImageLIst”中
procedure addFileToList(Name, Size:String);
var d:nodeData;
parent:String;
SHFileInfo :TSHFileINfo;
Icon:TIcon;
begin
parent:=ExtractFileDir(Name);
//Get The Icon That Represents The File/Folder
SHGetFileInfo(PChar(Name), 0, SHFileInfo, SizeOf(SHFileInfo),
SHGFI_ICON or SHGFI_SMALLICON );
Icon := TIcon.Create;
Icon.Handle := SHFileInfo.hIcon;
// set The Name, Size, Path
d.Name:=ExtractFileName(Name);
d.Size:=Size;
d.Path:=parent;
// set the ImageIndex
d.ImageIndex:=Form1.treeImageList.AddIcon(Icon);
// add the node to fileList
filesList.Add(filesList.Count, d);
// Destroy the Icon
DestroyIcon(SHFileInfo.hIcon);
Icon.Free;
end;
c) 创建树“theTree”
procedure createTree();
var theNode, Node:PVirtualNode;
d:PNodeData;
parent:String;
nData:nodeData;
i:integer;
begin
for i := 0 to filesList.Count - 1 do
begin
nData:=filesList.Items[i]; parent:=nData.Path;
if(hashmap.ContainsKey(parent))then theNode:=hashmap.Items[parent]
else theNode:=nil;
Node:=Form1.theTree.AddChild(theNode);
// add a checkbox and make it checked
Node.CheckType:=ctCheckBox;
Node.CheckState:=csCheckedNormal;
// get the newly created node data
d:=Form1.theTree.GetNodeData(Node);
// assign a data to the newly created node
d^:=nData;
// add the node to hashmap if it's a new folder node
if((ExtractFileExt(nData.Text)='')and(not hashmap.ContainsKey(nData.Path+'\'+nData.Text)))
then hashmap.Add(nData.Path+'\'+nData.Text, Node);
end;
Form1.theTree.Expanded[Form1.theTree.TopNode]:=True;
end;
d) 树形 View 事件
procedure TForm1.theTreeFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var d:PNodeData;
begin
d := Sender.GetNodeData(Node);
Finalize(d^);
end;
procedure TForm1.theTreeGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: String);
var d:PNodeData;
begin
d:=Sender.GetNodeData(Node);
case Column of
0:CellText:=d^.Text;
1:CellText:=d^.Size;
end;
end;
procedure TForm1.theTreeGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: Integer);
var d:PNodeData;
begin
d:=Sender.GetNodeData(Node);
if(Kind in [ikNormal, ikSelected])then
begin
if(Column=0)then ImageIndex:=d^.ImageIndex;
end;
end;
<小时/>
我现在真的很沮丧..我不知道为什么节点没有正确创建..虽然我测试了记录数据并且它们创建得很好,但是当我测试 onNodeClick 事件时我发现节点指向的数据记录仅返回第一个字段..而其他字段要么为空,要么生成访问冲突异常。
最佳答案
您发布的代码太多,无法提出有用的问题。每天都会发布数百个没有代码示例或代码示例不足的问题。你已经走了另一条路,真的走得很远。
“填充”虚拟 TreeView 的最佳方法是不填充它。相反,您只需设置 rootNodeCount
,然后在必要时迭代地设置子节点的子节点计数。对于折叠的节点, TreeView 知道有或没有子节点就足够了。展开子节点后,您可以填充子元素。
请注意,当您按照虚拟控件的预期方式进行操作时,您并没有编写大量代码。相反,您只是“回答有关模型对象的底层状态的问题”。有多少个根节点?你告诉虚拟树那个数字。然后,您只需在它询问时回答问题即可。根节点下的节点 #3 的第 0 列的文本是什么? (它调用您处理的事件,然后您返回该信息)。请注意,初始化 DATA 是大多数 VirtualTreeViews 新手经常误解的事情。理想情况下,DATA 应包含一个指向真实模型对象的指针,该对象可以回答 VirtualTreeView 提出的问题。回答这些问题的最有效的类可能甚至不需要为树中的每个可见节点实例化真实的模型对象,尽管这当然是可以接受和常见的。重要的是您要了解这并不是绝对必要的。
其次,如果您的目标是使用 TVirtualTreeView 模拟 shell 浏览器,那么该代码已经为您完成,请查看 VirtualTreeview 中提供的高级 VT 演示的子部分网站。请参阅我在此处指出的选项卡:
关于Delphi 填充 VirtualStringTree 的理想方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15769790/