2013-05-01 62 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