» »

vb.net printanje treeviewa

vb.net printanje treeviewa

majlo ::

Imam en problem, želim printati treeview oziroma ga vsaj shraniti kot sliko. Preguglal sem že pol interneta se mi zdi, pa še nisem naletel na delujočo rešitev.

Nikakor mi ne uspe spravit celotnega treeviewa v sliko, ko ima ta scroolbar. Če je treeview brez scrollbara (je cel viden), potem deluje.

Delal sem po naslednjem samplu:

http://www.codeproject.com/KB/printing/...

Sicer je to v c#, prevedel sem kodo v vb.net (uporabil sem kar prevajalnik iz http://www.developerfusion.com/tools/co...

Je komu znano, kako bi uspel to spravit v delovanje? Oziroma ima kdo kodo za to?

Sem za probo dodal en panel v formo, dal not en treeview, skopiral node in določil velikost, ki sem jo dobil v kodi (+par pik večje), pa mi določi pravo velikost, da je sam treeview brez scroll barov.

Nikakor pa mi ni potem uspelo dobit ven celotne slike. Izrisalo mi je prvo stran ter okoli tretjino druge. Po občutku bi moral potem dokument imeti okoli 4 strani.
Če pa sem shranil kot image, pa je bila slika samo črna (sicer velikost je štimala).

Bi prilepil kodo sem, samo sem bil tako jezen, da sem jo izbrisal iz projekta.

Sem tudi že razmišljal, da bi filal C1 report z istimi podatki iz treeviwa (filam rekurzivno iz SQL baze), samo je problem, ker ne morem naredit rekurzije v C1 (oziroma bi moral za vsaki level v nodu naredit sub report). Po dosedanjih podatkih sem prišel najglobje do 9. levela, samo se lahko to spremeni na več (do največ 31 se mi zdi). To bo tudi omejitev v kodi (zaradi enega grida na formi, ki se fila z rekurzivo na sql). Mogoče bi lahko poizkusil z C1 flexgridom, ker tisto naj bi šlo brez težav tiskat (imam kontrole ampak še z njim nisem delal, samo z trueDBgridom).

Ima kdo kakšno idejo oziroma celo delujoč sample???

Če je potrebno, lahko jutri ponovno prevedem kodo pa jo pastam tu, samo je tako ali tako gor v linku.

majlo ::

Mi ni dalo mira, pa sem si na domač komp inštaliral VS 2010, naredil en projekt, dodal to kodo (pretvoril kot zgoraj), treeview, z loopom malo nafilal treeview ter poizkusil shranit kot image (3 različne tipe - kot jpg, kot bmp ter kot png) in deluje WTF?????

Naredil tudi printpreview in tudi deluje?????

Mogoče sem tudi malo pretiraval z nodi - slika je 632 * 23429 ;)

Moram na službenem kompu preverit kaj bi lahko blo narobe (sicer mam tam VS 2008).

Zgodovina sprememb…

  • spremenilo: majlo ()

keworkian ::

A si delal screenshot?
Obscenities in B-Flat

majlo ::

P**da, na vs2008 mi ne dela, doma na 2010 pa dela.

tu je koda če je komu kaj jasno:

Public Class PrintHelper
        Private _printDoc As PrintDocument
        Private _lastPrintPosition As Point
        Private _controlImage As Image = Nothing
        Private _nodeHeight As Integer = 0
        Private _currentDir As PrintDirection
        Private _scrollBarHeight As Integer = 0
        Private _scrollBarWidth As Integer = 0
        Private _pageNumber As Integer = 0
        Private _date As DateTime
        Private _title As String = String.Empty

        Private Enum PrintDirection
            Horizontal
            Vertical
        End Enum

        Public Sub New()
            _lastPrintPosition = New Point(0, 0)
            Me._printDoc = New PrintDocument()
            AddHandler Me._printDoc.BeginPrint, New System.Drawing.Printing.PrintEventHandler(AddressOf Me.printDoc_BeginPrint)
            AddHandler Me._printDoc.PrintPage, New System.Drawing.Printing.PrintPageEventHandler(AddressOf Me.printDoc_PrintPage)
            AddHandler Me._printDoc.EndPrint, New PrintEventHandler(AddressOf _printDoc_EndPrint)
        End Sub

        Private Sub _printDoc_EndPrint(ByVal sender As Object, ByVal e As PrintEventArgs)
            _controlImage.Dispose()
        End Sub

      
        Public Sub PrintPreviewTree(ByVal tree As TreeView, ByVal title As String)
            Me._title = title
            PrepareTreeImage(DirectCast(tree, TreeView))
            Dim pp As New PrintPreviewDialog()
            pp.Document = Me._printDoc
            pp.Show()
        End Sub

      
        Public Sub PrintTree(ByVal tree As TreeView, ByVal title As String)
            Me._title = title
            PrepareTreeImage(DirectCast(tree, TreeView))
            Dim pd As New PrintDialog()
            pd.Document = Me._printDoc
            If pd.ShowDialog() = DialogResult.OK Then
                Me._printDoc.Print()
            End If

        End Sub

      
        Private Sub PrepareTreeImage(ByVal tree As TreeView)
            _scrollBarWidth = tree.Width - tree.ClientSize.Width
            _scrollBarHeight = tree.Height - tree.ClientSize.Height
            tree.Nodes(0).EnsureVisible()
            Dim height As Integer = tree.Nodes(0).Bounds.Height
            Me._nodeHeight = height
            Dim width As Integer = tree.Nodes(0).Bounds.Right
            Dim node As TreeNode = tree.Nodes(0).NextVisibleNode
            While node IsNot Nothing
                height += node.Bounds.Height
                If node.Bounds.Right > width Then
                    width = node.Bounds.Right
                End If
                node = node.NextVisibleNode
            End While
            'keep track of the original tree settings
            Dim tempHeight As Integer = tree.Height
            Dim tempWidth As Integer = tree.Width
            Dim tempBorder As BorderStyle = tree.BorderStyle
            Dim tempScrollable As Boolean = tree.Scrollable
            Dim selectedNode As TreeNode = tree.SelectedNode
            'setup the tree to take the snapshot
            tree.SelectedNode = Nothing
            Dim tempDock As DockStyle = tree.Dock
            tree.Height = height + _scrollBarHeight
            tree.Width = width + _scrollBarWidth
            tree.BorderStyle = BorderStyle.None
            tree.Dock = DockStyle.None
            'get the image of the tree

            ' .Net 2.0 supports drawing controls onto bitmaps natively now
            ' However, the full tree didn't get drawn when I tried it, so I am
            ' sticking with the P/Invoke calls
            '_controlImage = new Bitmap(height, width);
            'Bitmap bmp = _controlImage as Bitmap;
            'tree.DrawToBitmap(bmp, tree.Bounds);

            Me._controlImage = GetImage(tree.Handle, tree.Width, tree.Height)

            'Dim bm As Bitmap = _controlImage

            ' '' Save the picture as a bitmap, JPEG, and GIF.
            'bm.Save("D:\testb1" & ".bmp", _
            '    System.Drawing.Imaging.ImageFormat.Bmp)
            'bm.Save("D:\testj1" & ".jpg", _
            '    System.Drawing.Imaging.ImageFormat.Jpeg)
            'bm.Save("D:\testp1" & ".png", _
            '   System.Drawing.Imaging.ImageFormat.Png)


            'reset the tree to its original settings
            tree.BorderStyle = tempBorder
            tree.Width = tempWidth
            tree.Height = tempHeight
            tree.Dock = tempDock
            tree.Scrollable = tempScrollable
            tree.SelectedNode = selectedNode
            'give the window time to update
            Application.DoEvents()
        End Sub

        ' Returns an image of the specified width and height, of a control represented by handle.
        Private Function GetImage(ByVal handle As IntPtr, ByVal width As Integer, ByVal height As Integer) As Image
            Dim screenDC As IntPtr = GetDC(IntPtr.Zero)
            Dim hbm As IntPtr = CreateCompatibleBitmap(screenDC, width, height)
            Dim image As Image = Bitmap.FromHbitmap(hbm)
            Dim g As Graphics = Graphics.FromImage(image)
            Dim hdc As IntPtr = g.GetHdc()
            'WM_PRINTCLIENT
            SendMessage(handle, &H318, hdc, (&H10 Or &H4 Or &H2))
            g.ReleaseHdc(hdc)
            ReleaseDC(IntPtr.Zero, screenDC)
            Return image
        End Function

        Private Sub printDoc_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
            _lastPrintPosition = New Point(0, 0)
            Me._currentDir = PrintDirection.Horizontal
            Me._pageNumber = 0
            Me._date = DateTime.Now
        End Sub

        Private Sub printDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
            Me._pageNumber += 1
            Dim g As Graphics = e.Graphics
            Dim sourceRect As New Rectangle(_lastPrintPosition, e.MarginBounds.Size)
            Dim destRect As Rectangle = e.MarginBounds

            If (sourceRect.Height Mod Me._nodeHeight) > 0 Then
                sourceRect.Height -= (sourceRect.Height Mod Me._nodeHeight)
            End If
            g.DrawImage(Me._controlImage, destRect, sourceRect, GraphicsUnit.Pixel)
            'check to see if we need more pages
            If (Me._controlImage.Height - Me._scrollBarHeight) > sourceRect.Bottom OrElse (Me._controlImage.Width - Me._scrollBarWidth) > sourceRect.Right Then
                'need more pages
                e.HasMorePages = True
            End If
            If Me._currentDir = PrintDirection.Horizontal Then
                If sourceRect.Right < (Me._controlImage.Width - Me._scrollBarWidth) Then
                    'still need to print horizontally
                    _lastPrintPosition.X += (sourceRect.Width + 1)
                Else
                    _lastPrintPosition.X = 0
                    _lastPrintPosition.Y += (sourceRect.Height + 1)
                    Me._currentDir = PrintDirection.Vertical
                End If
            ElseIf Me._currentDir = PrintDirection.Vertical AndAlso sourceRect.Right < (Me._controlImage.Width - Me._scrollBarWidth) Then
                Me._currentDir = PrintDirection.Horizontal
                _lastPrintPosition.X += (sourceRect.Width + 1)
            Else
                _lastPrintPosition.Y += (sourceRect.Height + 1)
            End If

            'print footer
            Dim brush As Brush = New SolidBrush(Color.Black)
            Dim footer As String = Me._pageNumber.ToString(System.Globalization.NumberFormatInfo.CurrentInfo)
            Dim f As New Font(FontFamily.GenericSansSerif, 10.0F)
            Dim footerSize As SizeF = g.MeasureString(footer, f)
            Dim pageBottomCenter As New PointF(e.PageBounds.Width \ 2, e.MarginBounds.Bottom + ((e.PageBounds.Bottom - e.MarginBounds.Bottom) \ 2))
            Dim footerLocation As New PointF(pageBottomCenter.X - (footerSize.Width / 2), pageBottomCenter.Y - (footerSize.Height / 2))
            g.DrawString(footer, f, brush, footerLocation)

            'print header
            If Me._pageNumber = 1 AndAlso Me._title.Length > 0 Then
                Dim headerFont As New Font(FontFamily.GenericSansSerif, 24.0F, FontStyle.Bold, GraphicsUnit.Point)
                Dim headerSize As SizeF = g.MeasureString(Me._title, headerFont)
                Dim headerLocation As New PointF(e.MarginBounds.Left, ((e.MarginBounds.Top - e.PageBounds.Top) \ 2) - (headerSize.Height / 2))
                g.DrawString(Me._title, headerFont, brush, headerLocation)
            End If
        End Sub

        'External function declarations
        <DllImport("user32.dll")> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As Integer) As IntPtr
        End Function
        <DllImport("user32.dll")> _
        Private Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr
        End Function
        <DllImport("user32.dll")> _
        Private Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
        End Function
        <DllImport("gdi32.dll")> _
        Private Shared Function CreateCompatibleBitmap(ByVal hDC As IntPtr, ByVal width As Integer, ByVal height As Integer) As IntPtr
        End Function
    End Class

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim print As New PrintHelper()
        print.PrintPreviewTree(Me.tvwDB, "My Tree Sample")
    End Sub




Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

vb.net: dynamic textbox

Oddelek: Programiranje
51100 (990) saule
»

Vb 2005 - TextBox - Izračun števila vrstic?

Oddelek: Izdelava spletišč
81517 (1392) smash
»

[C#] Prosim pomagajte! Potrebujem program, ki bi pobiral podatke iz ene strani

Oddelek: Programiranje
212568 (2178) David1994
»

API+VB

Oddelek: Programiranje
261969 (1673) webblod
»

Odpiranje dat.exe v VB

Oddelek: Programiranje
122804 (2597) webblod

Več podobnih tem