Printer class for VB.NET-VBForums
Results 1 to 4 of 4

Thread: Printer class for VB.NET

Hybrid View

  1. #1

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

    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.

  2. #2
    Frenzied Member i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    1,762

    Re: Printer class for VB.NET

    just skimmed through your code and noticed that you don't have a print dialogue...

    may want to add one so the user can print to a specific printer

    Kris

  3. #3

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

    Re: Printer class for VB.NET

    I considered adding it but then chose against it. I was merely trying to mimique the behavior of Printer.Print method of VB6. Well, optionally, you can add this method to the class:

    vb.net Code:
    1. Public Shared Function ShowDialog() As DialogResult
    2.     Dim dlg As New PrintDialog
    3.     With dlg
    4.         .AllowCurrentPage = False
    5.         .AllowSelection = False
    6.         .AllowSomePages = False
    7.         .Document = prn
    8.         Return .ShowDialog
    9.     End With
    10. End Function

    And this one:

    vb.net Code:
    1. Public Shared Property PageSettings() As Printing.PageSettings
    2.     Get
    3.         Return prn.DefaultPageSettings
    4.     End Get
    5.     Set(ByVal value As Printing.PageSettings)
    6.         prn.DefaultPageSettings = value
    7.     End Set
    8. End Property

  4. #4
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    79

    Re: Printer class for VB.NET

    I think the PageSettings doesn't works.

    My.Printer.PageSettings.Margins.Top = 100
    My.Printer.Print("line 1" & vblf & "line 2")

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.