2013-05-01 54 views
1

我创建了出去,并扫描每一台计算机,并填充一个TreeView与硬件,软件和更新/修补程序信息的应用程序:如何自动展开,并打印一个TreeView

enter image description here

我的问题在打印过程中,如何自动展开树视图并将选定计算机的结果发送给打印机?我目前使用的方法涉及将内容发送到画布(BMP),然后将其发送到打印机,但不会捕获整个树视图,只是屏幕上显示的任何内容。有什么建议?非常感谢。

+0

只是树视图的文本格式正确或你想图形视图? – Glenn1234 2013-05-01 12:58:43

+0

对图形视图不感兴趣,只是树视图的文本,如果格式正确,它会很好。 – Cor4Ever 2013-05-01 14:13:09

回答

2

打印TTreeView时出现的问题是不可见的部分无法绘制。 (Windows仅绘制控件的可见部分,因此当您使用PrintTo或API PrintWindow函数时,它仅具有可见的节点可用于打印 - 未显示的内容尚未绘制,因此无法打印。)

如果表格布局作品(没有台词,只是缩进水平),最简单的方法是创建文本,并把它放在一个隐蔽TRichEdit,然后让TRichEdit.Print处理的输出。这里有一个例子:

// 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; 
+0

我工作过,非常感谢你。 – Cor4Ever 2013-05-01 22:58:40

+0

这个答案并不坏,但至少在Lazarus中已经有一个像SaveToFile一样的TTreeView方法,它将为您保存一个制表符文本文件。 (尽管如此,上述方法对于生成固定的布局文件,HTML等是很好的) – Noah 2013-12-27 07:09:17