在POS打印领域,使用EPS/POS标准进行打印往往困难重重。经过数日的设计和尝试,开发者可能会遇到无法打印阿拉伯文本的问题,而打印图像则需要大量的代码,打印预览几乎不可能实现,因为可能需要构建一个引擎来解析那些特定的标准代码以在屏幕上显示它们。
一个更简单的解决方案是使用经典的VB打印机对象。对于那些不熟悉打印机对象的人,可以在这里了解更多。
要在VB.NET代码中使用VB 6.0的古老打印机类,需要使用PowerPack兼容性库,该库可以在这里下载。
首先,需要在项目的引用中添加Microsoft.VisualBasic.PowerPacks
。
代码中需要一些导入:
Imports System.Drawing
Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
一些声明和需要的枚举:
Private p As Printer
Private _Path As String
Private _Align As TextAlignment = TextAlignment.Default
Private bIsDebug As Boolean = True
Public Enum TextAlignment As Byte
[Default] = 0
Left
Center
Right
End Enum
现在,类构造函数接受打印机名称作为参数,并且还接受应用程序路径,用于构建要使用的图像路径(如果有的话),可以传递Application.StartupPath
。
#Region "Constructors"
Public Sub New(ByVal AppPath As String)
SetPrinterName("GP-80220II (USB2)", AppPath)
End Sub
Public Sub New(ByVal strPrinterName As String, ByVal AppPath As String)
SetPrinterName(strPrinterName, AppPath)
End Sub
Private Sub SetPrinterName(ByVal PrinterName As String, ByVal AppPath As String)
Dim prnPrinter As Printer
For Each prnPrinter In Printers
If prnPrinter.DeviceName = PrinterName Then
p = prnPrinter
Exit For
End If
Next
p.DocumentName = "ERP System"
Me.Path = AppPath
If bIsDebug Then
p.PrintAction = Printing.PrintAction.PrintToPreview
End If
End Sub
#End Region
使用了以下字体大小,这些大小适合情况。可以根据自己的需要自由更改:
以下代码显示了控制字体的属性:
#Region "Font"
Public Property Alignment() As TextAlignment
Get
Return _Align
End Get
Set(ByVal value As TextAlignment)
_Align = value
End Set
End Property
Public Sub AlignLeft()
_Align = TextAlignment.Left
End Sub
Public Sub AlignCenter()
_Align = TextAlignment.Center
End Sub
Public Sub AlignRight()
_Align = TextAlignment.Right
End Sub
Public Property FontName() As String
Get
Return p.FontName
End Get
Set(ByVal value As String)
p.FontName = value
End Set
End Property
Public Property FontSize() As Single
Get
Return p.FontSize
End Get
Set(ByVal value As Single)
p.FontSize = value
End Set
End Property
Public Property Bold() As Boolean
Get
Return p.FontBold
End Get
Set(ByVal value As Boolean)
p.FontBold = value
End Set
End Property
Public Sub DrawLine()
p.DrawWidth = 2
p.Line(p.Width, p.CurrentY)
p.CurrentY += 20 'to move under the drawn line
End Sub
Public Sub NormalFont()
Me.FontSize = 9.5F
End Sub
Public Sub BigFont()
Me.FontSize = 15.0F
End Sub
Public Sub SmallFont()
Me.FontSize = 6.0F
End Sub
Public Sub SetFont(Optional ByVal FontSize As Single = 9.5F, _
Optional ByVal FontName As String = "FontA1x1", _
Optional ByVal BoldType As Boolean = False)
Me.FontSize = FontSize
Me.FontName = FontName
Me.Bold = BoldType
End Sub
#End Region
对于图像打印,使用了PrintLogo
子程序,但可以使用通用方法(如下所示的PrintImage
):
#Region "Images"
Public Property Path() As String
Get
Return _Path
End Get
Set(ByVal value As String)
_Path = value
End Set
End Property
Public Sub PrintLogo()
Me.PrintImage(_Path & "\Logo.bmp")
End Sub
Private Sub PrintImage(ByVal FileName As String)
Dim pic As Image
pic = pic.FromFile(FileName)
p.PaintPicture(pic, p.CurrentX, p.CurrentY)
p.CurrentY = p.CurrentY + pic.Height
End Sub
#End Region
现在,对于情况,将纸张分成了6个部分(六分之一),以便于控制。这可能适合情况,如果不是,可以很容易地更改它。
还要注意,打印机在正常字体下可以打印48个字符,这就是为什么也将纸张分成了48列。
#Region "Control"
Public Sub NewPage()
p.NewPage()
End Sub
Public Property RTL() As Boolean
Get
Return p.RightToLeft
End Get
Set(ByVal value As Boolean)
p.RightToLeft = value
End Set
End Property
Public Sub FeedPaper(Optional ByVal nlines As Integer = 3)
For i As Integer = 1 To nlines
Me.WriteLine("")
Next
End Sub
Public Sub GotoCol(Optional ByVal ColNumber As Integer = 0)
Dim ColWidth As Single = p.Width / 48
p.CurrentX = ColWidth * ColNumber
End Sub
Public Sub GotoSixth(Optional ByVal nSixth As Integer = 1)
Dim OneSixth As Single = p.Width / 6
p.CurrentX = OneSixth * (nSixth - 1)
End Sub
Public Sub UnderlineOn()
p.FontUnderline = True
End Sub
Public Sub UnderlineOff()
p.FontUnderline = False
End Sub
Public Sub EndDoc()
p.EndDoc()
End Sub
Public Sub EndJob()
Me.EndDoc()
End Sub
Public Sub WriteLine(ByVal Text As String)
Dim sTextWidth As Single = p.TextWidth(Text)
Select Case _Align
Case TextAlignment.Default
'do nothing
Case TextAlignment.Left
p.CurrentX = 0
Case TextAlignment.Center
p.CurrentX = (p.Width - sTextWidth) / 2
Case TextAlignment.Right
p.CurrentX = (p.Width - sTextWidth)
End Select
p.Print(Text)
End Sub
Public Sub WriteChars(ByVal Text As String)
p.Write(Text)
End Sub
Public Sub CutPaper()
p.NewPage()
End Sub
#End Region
使用该类,可以创建一个示例收据,查看代码和打印结果。
Dim P As New PrinterClass(Application.StartupPath)
With P
'Printing Logo
.RTL = False
.PrintLogo()
'Printing Title
.FeedPaper(4)
.AlignCenter()
.BigFont()
.Bold = True
.WriteLine("Sales Receipt")
'Printing Date
.GotoSixth(1)
.NormalFont()
.WriteChars("Date:")
.WriteLine(DateTime.Now.ToString)
.DrawLine()
.FeedPaper(2)
'Printing Header
.GotoSixth(1)
.WriteChars("#")
.GotoSixth(2)
.WriteChars("Description")
.GotoSixth(5)
.WriteChars("Count")
.GotoSixth(6)
.WriteChars("Total")
.WriteLine("")
.DrawLine()
.FeedPaper(1)
'Printing Items
.SmallFont()
Dim i As Integer
For i = 1 To 6
.GotoSixth(1)
.WriteChars(i)
.GotoSixth(2)
.WriteChars("Item# " & (Rnd() * 100) \ 1)
.GotoSixth(5)
.WriteChars(Rnd() * 10 \ 1)
.GotoSixth(6)
.WriteChars((Rnd() * 50 \ 1) & " JD(s)")
.WriteLine("")
Next
'Printing Totals
.NormalFont()
.DrawLine()
.GotoSixth(1)
.UnderlineOn()
.WriteChars("Total")
.UnderlineOff()
.GotoSixth(5)
.WriteChars((Rnd() * 300 \ 1) & " JD(s)")
.CutPaper()
'Can be used with real printer to cut the paper.
'Ending the session
.EndDoc()
End With
注意事项:
bIsDebug
设置为true
)时,以便预览窗口反映打印机的实际属性(宽度、字体等)。