// File->New->VCL Forms Application, then
// Drop a TTreeView and a TButton on the form.
// Add the following for the FormCreate (to create the treeview content)
// and button click handlers, and the following procedure to create
// the text content:
procedure TreeToText(const Tree: TTreeView; const RichEdit: TRichEdit);
var
Node: TTreeNode;
Indent: Integer;
Padding: string;
const
LevelIndent = 4;
begin
RichEdit.Clear;
Node := Tree.Items.GetFirstNode;
while Node <> nil do
begin
Padding := StringOfChar(#32, Node.Level * LevelIndent);
RichEdit.Lines.Add(Padding + Node.Text);
Node := Node.GetNext;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
HideForm: TForm;
HideEdit: TRichEdit;
begin
HideForm := TForm.Create(nil);
try
HideEdit := TRichEdit.Create(HideForm);
HideEdit.Parent := HideForm;
TreeToText(TreeView1, HideEdit);
HideEdit.Print('Printed TreeView Text');
finally
HideForm.Free;
end;
end;
procedure TForm3.FormCreate(Sender: TObject);
var
i, j: Integer;
RootNode, ChildNode: TTreeNode;
begin
RootNode := TreeView1.Items.AddChild(nil, 'Root');
for i := 1 to 6 do
begin
ChildNode := TreeView1.Items.AddChild(RootNode, Format('Root node %d', [i]));
for j := 1 to 4 do
TreeView1.Items.AddChild(ChildNode, Format('Child node %d', [j]));
end;
end;
1条答案
按热度按时间kzmpq1sx1#
打印
TTreeView
的问题是,看不见的部分没有要绘制的东西。(Windows只绘制控件的可见部分,因此当您使用PrintTo
或APIPrintWindow
函数时,它只有可打印的可见节点--未显示的内容尚未绘制,因此无法打印。)如果表格布局有效(没有线条,只是缩进级别),最简单的方法是创建文本并将其放入隐藏的
TRichEdit
中,然后让TRichEdit.Print
处理输出。下面是一个例子: