mmips
asked on
Printing a VB6 Treeview
I would like some pointers on how one might print the contents of a treeview control?
Better answer gets better points!!!
Better answer gets better points!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
well you would have to ensure that the treeview was fully expanded. then you would have to use some trickery to get it to print just the area you want. see this MS article:
http://support.microsoft.com/support/kb/articles/Q85/9/78.ASP
http://support.microsoft.com/support/kb/articles/Q85/9/78.ASP
I have this code that I never tried!
Here is a new version that print treeview to a picturebox or to printer, graphicly with treelines and icons.
Put this code into a module
Option Explicit
Public Const INDENT_WIDTH = 300
Public Const ITEM_HEIGTH = 200
Private Const MAX_INDENT = 10
Private bBoudin(0 To MAX_INDENT) As Boolean
Public Sub PrintTreeview(ByRef tv As TreeView, prt As Object)
Dim i As Integer
Dim n As Node
Debug.Assert (TypeOf prt Is PictureBox) Or (TypeOf prt Is Printer)
Set n = tv.Nodes(1)
Do
PrintTreeviewNode tv, n, 0, prt
Set n = n.Next
Loop While Not (n Is Nothing)
End Sub
Private Sub PrintTreeviewNode(ByRef tv As TreeView, ByRef nd As Node, ByVal lvl As Integer, ByRef prt As Object)
Dim i As Integer
Dim n As Node
Dim x0 As Single, y0 As Single
x0 = 0
y0 = prt.CurrentY
'Print boudins
For i = IIf(tv.LineStyle = tvwRootLines, 0, 1) To lvl - 1
If bBoudin(i) Then
prt.Line (x0 + INDENT_WIDTH \ 4, y0)-Step(0, ITEM_HEIGTH), vbBlack
End If
x0 = x0 + INDENT_WIDTH
Next
bBoudin(lvl) = Not (nd.Next Is Nothing)
If tv.LineStyle = tvwRootLines Or lvl > 0 Then
If nd.Next Is Nothing Then
prt.Line (x0 + INDENT_WIDTH \ 4, y0)-Step(0, ITEM_HEIGTH \ 2), vbBlack
prt.Line -Step(INDENT_WIDTH - INDENT_WIDTH \ 4, 0), vbBlack
Else
prt.Line (x0 + INDENT_WIDTH \ 4, y0)-Step(0, ITEM_HEIGTH), vbBlack
prt.CurrentY = prt.CurrentY - ITEM_HEIGTH \ 2
prt.Line -Step(INDENT_WIDTH - INDENT_WIDTH \ 4, 0), vbBlack
End If
x0 = x0 + INDENT_WIDTH
End If
If Not (tv.ImageList Is Nothing) _
And (tv.Style And (tvwPictureText Or tvwPlusPictureText Or tvwTreelinesPictureText Or tvwTreelinesPlusMinusPictu reText)) Then
If Len(nd.Image) Then
prt.PaintPicture tv.ImageList.ListImages.It em(nd.Imag e).Picture , x0, y0
End If
x0 = x0 + INDENT_WIDTH
End If
prt.CurrentX = x0
prt.CurrentY = y0
prt.Print nd.Text
prt.CurrentY = y0 + ITEM_HEIGTH
If nd.Children Then
Set n = nd.Child
Do
PrintTreeviewNode tv, n, lvl + 1, prt
Set n = n.Next
Loop Until n Is Nothing
End If
End Sub
Here is a new version that print treeview to a picturebox or to printer, graphicly with treelines and icons.
Put this code into a module
Option Explicit
Public Const INDENT_WIDTH = 300
Public Const ITEM_HEIGTH = 200
Private Const MAX_INDENT = 10
Private bBoudin(0 To MAX_INDENT) As Boolean
Public Sub PrintTreeview(ByRef tv As TreeView, prt As Object)
Dim i As Integer
Dim n As Node
Debug.Assert (TypeOf prt Is PictureBox) Or (TypeOf prt Is Printer)
Set n = tv.Nodes(1)
Do
PrintTreeviewNode tv, n, 0, prt
Set n = n.Next
Loop While Not (n Is Nothing)
End Sub
Private Sub PrintTreeviewNode(ByRef tv As TreeView, ByRef nd As Node, ByVal lvl As Integer, ByRef prt As Object)
Dim i As Integer
Dim n As Node
Dim x0 As Single, y0 As Single
x0 = 0
y0 = prt.CurrentY
'Print boudins
For i = IIf(tv.LineStyle = tvwRootLines, 0, 1) To lvl - 1
If bBoudin(i) Then
prt.Line (x0 + INDENT_WIDTH \ 4, y0)-Step(0, ITEM_HEIGTH), vbBlack
End If
x0 = x0 + INDENT_WIDTH
Next
bBoudin(lvl) = Not (nd.Next Is Nothing)
If tv.LineStyle = tvwRootLines Or lvl > 0 Then
If nd.Next Is Nothing Then
prt.Line (x0 + INDENT_WIDTH \ 4, y0)-Step(0, ITEM_HEIGTH \ 2), vbBlack
prt.Line -Step(INDENT_WIDTH - INDENT_WIDTH \ 4, 0), vbBlack
Else
prt.Line (x0 + INDENT_WIDTH \ 4, y0)-Step(0, ITEM_HEIGTH), vbBlack
prt.CurrentY = prt.CurrentY - ITEM_HEIGTH \ 2
prt.Line -Step(INDENT_WIDTH - INDENT_WIDTH \ 4, 0), vbBlack
End If
x0 = x0 + INDENT_WIDTH
End If
If Not (tv.ImageList Is Nothing) _
And (tv.Style And (tvwPictureText Or tvwPlusPictureText Or tvwTreelinesPictureText Or tvwTreelinesPlusMinusPictu
If Len(nd.Image) Then
prt.PaintPicture tv.ImageList.ListImages.It
End If
x0 = x0 + INDENT_WIDTH
End If
prt.CurrentX = x0
prt.CurrentY = y0
prt.Print nd.Text
prt.CurrentY = y0 + ITEM_HEIGTH
If nd.Children Then
Set n = nd.Child
Do
PrintTreeviewNode tv, n, lvl + 1, prt
Set n = n.Next
Loop Until n Is Nothing
End If
End Sub
ASKER
I am splitting the points between the 2 answers...I ended up using pieces of both to accomplish what I wanted...Thanks to both of you...
Have opened up another question in relation to the above code if you have time
https://www.experts-exchange.com/questions/25226584/printing-from-acess-the-contents-of-a-tree-control-to-a-printer-using-existing-code.html?fromWizard=true
https://www.experts-exchange.com/questions/25226584/printing-from-acess-the-contents-of-a-tree-control-to-a-printer-using-existing-code.html?fromWizard=true
ASKER
That particular code snippet works if all you want is the data i.e. node text.. I would however like to print the treeview graphically...i.e. All of the file folders and the text as displayed....