【问题标题】:How to auto expand and print a TreeView如何自动展开和打印 TreeView
【发布时间】:2013-05-01 11:30:42
【问题描述】:

我创建了一个应用程序,它会扫描每台计算机并在 TreeView 中填充硬件、软件和更新/修补程序信息:

我遇到的问题是打印,如何自动展开树形视图并将所选计算机的结果发送到打印机?我当前使用的方法涉及将内容发送到画布(BMP),然后将其发送到打印机,但这并不能仅捕获屏幕上显示的整个树视图。有什么建议吗?非常感谢。

【问题讨论】:

  • 只是树形视图的文本格式正确还是您想要图形视图?
  • 对图形视图不感兴趣,只对树视图的文本感兴趣,如果格式正确就好了。

标签: delphi printing treeview delphi-xe2


【解决方案1】:

打印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;

【讨论】:

  • 这个答案还不错,但至少在 Lazarus 中已经有一个 TTreeView 的方法,比如 SaveToFile,它会为你保存一个制表符分隔的文本文件。 (不过,上面的方法对于生成固定的布局文件、HTML 等还是很好的)
猜你喜欢
  • 1970-01-01
  • 2011-08-02
  • 2022-10-02
  • 2014-05-02
  • 1970-01-01
  • 2014-01-11
  • 2019-10-14
  • 2017-02-21
  • 1970-01-01
相关资源
最近更新 更多