Results 1 to 4 of 4

Thread: Printer class for VB.NET

Threaded View

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Printer class for VB.NET

    Remember the good old times of VB6 where you could simply call
    Code:
    Printer.Print(text)
    and this was all you needed in order to print plain text on the default printer.

    Well, we can forget about it in VS.Net. I like VS, but sometimes you don't have to print anything but plain text.

    So, here is a small class I wrote which does exactly that - prints plain text on the default printer. It automatically measures the text to fit on your page wraps it and spans over several pages if needed.
    You can add more properties to it to set margins, paper orientation if you want, but it will lose its simple charm so I leave it as it is.

    I've built this class into My namespace so you only need to call
    Code:
    My.Printer.Print(text)
    in order to print something.



    vb.net Code:
    1. Namespace My
    2.     ''' <summary>
    3.     ''' Represents the lost VB6 functionality of a Printer object
    4.     ''' </summary>
    5.     ''' <remarks>Usage: My.Printer.Print(text)</remarks>
    6.     Public Class Printer
    7.         Private Shared Lines As New Queue(Of String)
    8.         Private Shared _myfont As Font
    9.         Private Shared prn As Printing.PrintDocument
    10.  
    11.         Shared Sub New()
    12.             _myfont = New Font(SystemFonts.DefaultFont.FontFamily, 0.16, FontStyle.Regular, GraphicsUnit.Inch)
    13.             prn = New Printing.PrintDocument
    14.             AddHandler prn.PrintPage, AddressOf PrintPageHandler
    15.         End Sub
    16.  
    17.         Public Shared Sub Print(ByVal text As String)
    18.             Dim linesarray() = text.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
    19.  
    20.             For Each line In linesarray
    21.                 Lines.Enqueue(line)
    22.             Next
    23.             prn.Print()
    24.         End Sub
    25.  
    26.         Private Shared Sub PrintPageHandler(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs)
    27.             Dim sf As New StringFormat()
    28.             Dim vpos As Single = e.PageSettings.HardMarginY
    29.  
    30.             Do While Lines.Count > 0
    31.                 Dim line As String = Lines.Dequeue
    32.                 Dim sz As SizeF = e.Graphics.MeasureString(line, _myfont, e.PageSettings.Bounds.Size, sf)
    33.  
    34.                 Dim rct As New RectangleF( _
    35.                     e.PageSettings.HardMarginX, vpos, _
    36.                     e.PageBounds.Width - e.PageSettings.HardMarginX * 2, _
    37.                     e.PageBounds.Height - e.PageSettings.HardMarginY * 2)
    38.  
    39.                 e.Graphics.DrawString(line, _myfont, Brushes.Black, rct)
    40.                 vpos += sz.Height
    41.                 If vpos > e.PageSettings.Bounds.Height - e.PageSettings.HardMarginY * 2 Then
    42.                     e.HasMorePages = True
    43.                     Exit Sub
    44.                 End If
    45.             Loop
    46.         End Sub
    47.     End Class
    48. End Namespace

    Any feedback on this will be greatly appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width