''' <summary> ''' 打印收據 ''' </summary> ''' <remarks></remarks> Private Sub PrintReceipt() Try '調用更改默認打印機 Call changeDefaultPrinter(PrintDocType.Receipt)
Dim PrintDoc As New Printing.PrintDocument
AddHandler PrintDoc.PrintPage, AddressOf DrawReceipt '關鍵是這句話,當使用Print方法時,會自動調用DrawReceipt方法,這個方法是畫小收據的過程,可以自定義. PrintDoc.Print()
Catch ex As Exception MsgBox("Printing error!" & vbCrLf & "Please make sure your printer is valid!", 48) End Try End Sub
'這個函數的參數是固定的.用e變量來畫出任何圖形或文字,這樣就可以打印到打印機去了.這個例子有點複雜,有空我再簡化一下.
''' <summary> ''' 畫出Receipt,用於打印.收據, ''' 臨時使用,最後完善一個打印類 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub DrawReceipt(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs)
Dim MyTitformat As New StringFormat MyTitformat.Alignment = StringAlignment.Center Dim MyTitformat_item As New StringFormat
Dim MyConformat As New StringFormat MyConformat.Alignment = StringAlignment.Near
'e.Graphics.PageUnit = GraphicsUnit.Millimeter e.Graphics.PageUnit = GraphicsUnit.Point 'e.Graphics.PageUnit = GraphicsUnit.Pixel Dim MyTitFont1 As New Font("Arial", 21, FontStyle.Bold) '標題字體 Dim MyTitFont2 As New Font("Arial", 12, FontStyle.Bold) '標題字體 Dim MyConFont As New Font("Arial", 8, FontStyle.Regular) '正文字體 Dim MyConFont1 As New Font("Arial", 9, FontStyle.Bold) '正文字體 Dim MyLinePen As New Pen(Color.Black, 1) Dim RecRow As Single = 0 RecRow = 1 Dim nPageH As Double = 0 Dim nPageW As Double = 0
nPageH = nPh nPageW = nPw nPageW = 180 topW = nPageW
''左半側三例的X座標 Dim XL0 As Double = 0 Dim XL1 As Double = 0 Dim XL2 As Double = 0 Dim XL3 As Double = 0 Dim XL4 As Double = 0
''當前的Y座標 Dim YCurrent As Double = 20 ''中間豎線的X座標 Dim XLine As Double = 0
''每行文字的高度 Dim dHline As Double = 0
''當前打印的區域,主要用於控制描述打印的自動換行和換行後的高度 Dim CurRecF As RectangleF
''當前打印的字符 Dim sCurTPrintStr As String = ""
''用於保存打印公司信息時的打印寬度 Dim companyInfoW As Double = 230
XLine = nPageW / 2 - 25 Me.itemnumW = 90 dItemDesW = 2 dItemQuantityW = 23 dItemPriceW = 52 allPriceW = 60
XL0 = 0 XL1 = XL0 + Me.itemnumW XL2 = XL1 + dItemDesW XL3 = XL2 + Me.dItemQuantityW XL4 = XL3 + Me.dItemPriceW
'e.Graphics.DrawLine(Pens.Black, CInt(XL0), CInt(YCurrent), CInt(XL0 + nPageW), CInt(YCurrent)) 'YCurrent = YCurrent + 1 'e.Graphics.DrawLine(Pens.Black, CInt(XL0), CInt(YCurrent), CInt(XL4 + dItemPriceW), CInt(YCurrent))
Dim LogoIsExistence As Boolean = True '先判斷是否要在收據上打印Logo If objSetup.Logo_Type = 1 Or objSetup.Logo_Type = 3 Then If My.Computer.FileSystem.FileExists(Application.StartupPath.Trim & "/Pictures/LogoOnReceipt.TIF") Then LogoIsExistence = True
Dim printLogo As Image = Image.FromFile(Application.StartupPath.Trim & "/Pictures/LogoOnReceipt.TIF") YCurrent = 10 '畫左側Logo Dim xL As Single = 10.0F Dim yL As Single = Convert.ToSingle(YCurrent) e.Graphics.DrawImage(printLogo, xL, yL) Else LogoIsExistence = False End If End If 'companyInfoW = e.PageSettings.PaperSize.Width
'判斷是否要在收據上打印公司信息 If objSetup.Logo_Type = 2 Or objSetup.Logo_Type = 3 Then '打印公司信息
'判斷標題是否要打印:如果打印Logo的話公司名就不要打印 If objSetup.Logo_Type = 3 And LogoIsExistence Then '重新計算Y座標值,空出Logo的高度 dHline = 48 YCurrent = YCurrent + dHline Else If objSetup.Company_Info_1.Trim <> "" Then sCurTPrintStr = objSetup.Company_Info_1.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont1).Width '計算打印字符串的寬度 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)
CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont1, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont1, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline
XL1 = XL0 + itemnumW End If End If
If objSetup.Company_Info_2.Trim <> "" Then sCurTPrintStr = objSetup.Company_Info_2.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2).Width 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)
CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont2, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline
XL1 = XL0 + itemnumW End If
If objSetup.Company_Info_3.Trim <> "" Then sCurTPrintStr = objSetup.Company_Info_3.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2).Width 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)
CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont2, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline
XL1 = XL0 + itemnumW End If
If objSetup.Company_Info_4.Trim <> "" Then sCurTPrintStr = objSetup.Company_Info_4.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2).Width 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)
CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont2, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline
XL1 = XL0 + itemnumW End If End If
'如果只打印Logo則需要計算Y座標值 If objSetup.Logo_Type = 1 Then dHline = 48 YCurrent = YCurrent + dHline End If
sCurTPrintStr = "INVOICE #:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = thisInvoice_Number.ToString.Trim e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)
dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline
sCurTPrintStr = "DATE/TIME:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = FormatDateTime(Now, DateFormat.ShortDate) & " " & FormatDateTime(Now, DateFormat.LongTime).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)
dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline
sCurTPrintStr = "CASHIER: " & sys_employee.Cashier_ID.Trim e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = "STATION: " & objStation.Station_ID e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)
dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline
sCurTPrintStr = "CUSTOMER:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = oCustomer.First_Name.Trim & " " & oCustomer.Last_Name.Trim e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)
dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline * 2
Dim i As Integer
'''''''''''''''''''''''''''''''''''''''''''''''''''''' '表頭 sCurTPrintStr = "ITEM" e.Graphics.DrawString(sCurTPrintStr, MyConFont1, Brushes.Black, XL0, YCurrent)
CurRecF = New RectangleF(XL1, YCurrent, itemnumW, 100)
'e.Graphics.DrawString(" DESCRIPTION", MyConFont1, Brushes.Black, XL1, YCurrent) 'dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, itemnumW, MyConformat).Height
e.Graphics.DrawString("QTY", MyConFont1, Brushes.Black, XL2, YCurrent)
e.Graphics.DrawString("PRICE", MyConFont1, Brushes.Black, XL3, YCurrent)
e.Graphics.DrawString("TOTAL", MyConFont1, Brushes.Black, XL4, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont1, dItemDesW + itemnumW, MyTitformat).Height YCurrent = YCurrent + dHline
'YCurrent = YCurrent + 10 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''表頭下面畫一條線 e.Graphics.DrawLine(MyLinePen, New PointF(XL0, YCurrent), New PointF(XL4 + Me.allPriceW, YCurrent)) dHline = 2 YCurrent = YCurrent + dHline
''畫出每個產品 Dim nLenItems As Integer = 0 nLenItems = oSalesInv.InvoiceItems.Length
For i = 0 To nLenItems - 1 If oSalesInv.InvoiceItems(i).Print_On_Receipt = True Then
''<左側>一個產品:三個字段,數量:描述:單價 sCurTPrintStr = oSalesInv.InvoiceItems(i).DiffItemName.Trim CurRecF = New RectangleF(XL0, YCurrent, Me.itemnumW, 100) e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, CurRecF, MyTitformat_item) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, itemnumW, MyConformat).Height CurRecF = New RectangleF(XL1, YCurrent, itemnumW, 100)
''sCurTPrintStr = oSalesInv.InvoiceItems(i).DiffItemName '商品名稱 ' ''e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL1, YCurrent) ''e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, CurRecF, MyTitformat) ' ''e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XR1, YCurrent)
sCurTPrintStr = " " & oSalesInv.InvoiceItems(i).Quantity '數量 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL2, YCurrent)
sCurTPrintStr = FormatCurrency(oSalesInv.InvoiceItems(i).PricerPer.money).ToString '單價 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL3, YCurrent)
sCurTPrintStr = FormatCurrency(oSalesInv.InvoiceItems(i).Quantity * oSalesInv.InvoiceItems(i).PricerPer.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If
Next
''產品結束下面畫一條線 e.Graphics.DrawLine(MyLinePen, New PointF(XL0, YCurrent), New PointF(XL4 + Me.allPriceW, YCurrent)) dHline = 2 YCurrent = YCurrent + dHline
'用於控制尾部的彙總描述寬度和高度。 Dim nEndDesW As Double nEndDesW = XL0 + topW / 2 '如果整單有折扣,則顯示折扣前和折扣信息 If oSalesInv.Discount > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "UNDISCOUNTED TOTAL:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.Total_Price.money / (1 - oSalesInv.Discount)).ToString '總應收錢數 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL2, YCurrent)
YCurrent = YCurrent + dHline
CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "-" & FormatPercent(oSalesInv.Discount) & "Discount:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.Total_Price.money / (1 - oSalesInv.Discount) * oSalesInv.Discount).ToString '總應收錢數 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL2, YCurrent)
YCurrent = YCurrent + dHline
End If ''產品總費用 CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "SUBTOTAL:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.Total_Price.money).ToString '總應收錢數 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline
''產品總稅 CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "TAX:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.Total_Tax1.money + oSalesInv.Total_Tax2.money + oSalesInv.Total_Tax3.money).ToString '總稅 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline
''GRAND TOTAL CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "GRAND TOTAL:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.Grand_Total.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline
'' ''這裏要加上多種支付方式。 ''先空出一行 YCurrent = YCurrent + dHline
''判斷現金支付 If oSalesInv.CA_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.CA_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If ''判斷信用卡支付 If oSalesInv.CC_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "CREDIT CARD:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.CC_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If ''判斷支票支付 If oSalesInv.CH_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "TRAVELLER CHECK AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.CH_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If
''判斷Room Card支付 If oSalesInv.RC_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "ROOM CARD AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.RC_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If
''判斷Gift Card支付 If oSalesInv.GC_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "GIFT CARD AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.GC_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If ''判斷Debit Card支付 If oSalesInv.DC_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "DEBIT CARD AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.DC_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If ''判斷On Account支付 If oSalesInv.OA_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "STORE CREDIT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.OA_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If ''判斷Food Stamps支付 If oSalesInv.FS_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "FOOD STAMPS :" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(oSalesInv.FS_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline End If
''AMT TENDERED CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "PAYMENT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(Me.AmtTendered.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
YCurrent = YCurrent + dHline
''Change due CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "CHANGE DUE:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height
sCurTPrintStr = FormatCurrency(Me.AmtChange.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)
'YCurrent = YCurrent + dHline
end sub