Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Drawing
Namespace HPRT.Printer
    Public Class PrinterState
        Public Shared ReadOnly STS_NORMAL As UInteger = &H0
        Public Shared ReadOnly STS_PAPEREMPTY As UInteger = 1
        Public Shared ReadOnly STS_COVEROPEN As UInteger = 2
        Public Shared ReadOnly STS_PAPERNEAREND As UInteger = 4
        Public Shared ReadOnly STS_MSR_READY As UInteger = 8
        Public Shared ReadOnly STS_SMARTCARD_READY As UInteger = 16
        Public Shared ReadOnly STS_ERROR As UInteger = 32
        Public Shared ReadOnly STS_NOT_OPEN As UInteger = 64
        Public Shared ReadOnly STS_OFFLINE As UInteger = 128
        Private m_status As UInteger = 0
        Public Sub New()
            m_status = 0
        End Sub
        Public Property State() As UInteger
            Get
                Return m_status
            End Get
            Set(ByVal value As UInteger)
                m_status = value
            End Set
        End Property
        Public ReadOnly Property IsNormal() As Boolean
            Get
                Return (STS_NORMAL = m_status)
            End Get
        End Property
        Public ReadOnly Property InError() As Boolean
            Get
                Return (PAPEREMPTY OrElse COVEROPEN OrElse PAPERNEAREND OrElse MSR_READY OrElse SMARTCARD_READY OrElse [ERROR] OrElse NOT_OPEN OrElse OFFLINE)
            End Get
        End Property
        Public ReadOnly Property PAPEREMPTY() As Boolean
            Get
                Return ((STS_PAPEREMPTY And m_status) > 0)
            End Get
        End Property
        Public ReadOnly Property COVEROPEN() As Boolean
            Get
                Return ((STS_COVEROPEN And m_status) > 0)
            End Get
        End Property
        Public ReadOnly Property PAPERNEAREND() As Boolean
            Get
                Return ((STS_PAPERNEAREND And m_status) > 0)
            End Get
        End Property
        Public ReadOnly Property MSR_READY() As Boolean
            Get
                Return ((STS_MSR_READY And m_status) > 0)
            End Get
        End Property
        Public ReadOnly Property SMARTCARD_READY() As Boolean
            Get
                Return ((STS_SMARTCARD_READY And m_status) > 0)
            End Get
        End Property
        Public ReadOnly Property [ERROR]() As Boolean
            Get
                Return ((STS_ERROR And m_status) > 0)
            End Get
        End Property
        Public ReadOnly Property NOT_OPEN() As Boolean
            Get
                Return ((STS_NOT_OPEN And m_status) > 0)
            End Get
        End Property
        Public ReadOnly Property OFFLINE() As Boolean
            Get
                Return ((STS_OFFLINE And m_status) > 0)
            End Get
        End Property
    End Class
    Public Class HPRTPrinter
        Public Const HPRTDIR As String = "ESC_SDK.dll"
        Public Const charSet As CharSet = CharSet.Ansi

        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtFormatError(ByVal error_no As Integer, ByVal langid As Integer, ByVal buf As Byte(), ByVal pos As Integer, ByVal bufSize As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrinterCreator(ByRef printer As IntPtr, ByVal model As String) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrinterDestroy(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPortOpen(ByVal printer As IntPtr, ByVal portSetting As String) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPortClose(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrinterInitialize(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtFeedLine(ByVal printer As IntPtr, ByVal nFeed As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetAlign(ByVal printer As IntPtr, ByVal align As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintText(ByVal printer As IntPtr, ByVal text As Byte(), ByVal alignment As Integer, ByVal attribute As Integer, ByVal textSize As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintBarCode(ByVal printer As IntPtr, ByVal bcType As Integer, ByVal bcData As String, ByVal width As Integer, ByVal height As Integer, ByVal alignment As Integer, _
   ByVal hriPosition As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintSymbol(ByVal printer As IntPtr, ByVal type As Integer, ByVal bcData As String, ByVal errLevel As Integer, ByVal width As Integer, ByVal height As Integer, _
   ByVal alignment As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtDefineDownloadedImageCompatible(ByVal printer As IntPtr, ByVal imagePath As String) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintDownloadedImageCompatible(ByVal printer As IntPtr, ByVal scaleMode As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=Runtime.InteropServices.CharSet.Unicode)> _
        Private Shared Function PrtDefineNVImageCompatible(ByVal printer As IntPtr, ByVal imagePath As String(), ByVal imageCnt As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintNVImageCompatible(ByVal printer As IntPtr, ByVal imageNo As Integer, ByVal scaleMode As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtDefineDownloadedImage(ByVal printer As IntPtr, ByVal imagePath As String, ByVal kc1 As Byte, ByVal kc2 As Byte) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintDownloadedImage(ByVal printer As IntPtr, ByVal kc1 As Byte, ByVal kc2 As Byte) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtDefineBufferedImage(ByVal printer As IntPtr, ByVal imagePath As String) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintBufferedImage(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtDefineNVImage(ByVal printer As IntPtr, ByVal imagePath As String, ByVal kc1 As Byte, ByVal kc2 As Byte) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintNVImage(ByVal printer As IntPtr, ByVal kc1 As Byte, ByVal kc2 As Byte, ByVal horizontal As Integer, ByVal vertical As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintImage(ByVal printer As IntPtr, ByVal filePath As String, ByVal scaleMode As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtCutPaper(ByVal printer As IntPtr, ByVal cutMode As Integer, ByVal distance As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtOpenDrawer(ByVal printer As IntPtr, ByVal pinMode As Integer, ByVal onTime As Integer, ByVal offTime As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSelectStandardMode(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetTextLineSpace(ByVal printer As IntPtr, ByVal lineSpace As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetTextPosition(ByVal printer As IntPtr, ByVal position As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSelectPageMode(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetPrintAreaInPageMode(ByVal printer As IntPtr, ByVal horizontal As Integer, ByVal vertical As Integer, ByVal width As Integer, ByVal height As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtCancelPrintDataInPageMode(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSelectPrintDirectionInPageMode(ByVal printer As IntPtr, ByVal direction As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetAbsolutePrintPosition(ByVal printer As IntPtr, ByVal position As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetAbsoluteVerticalPrintPositionInPageMode(ByVal printer As IntPtr, ByVal position As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetPrintAndReturnStandardMode(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintDataInPageMode(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtCheckPrinterState(ByVal printer As IntPtr, ByRef printerState As UInteger) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtDirectIO(ByVal printer As IntPtr, ByVal writeData As Byte(), ByVal writenum As Integer, ByVal readData As Byte(), ByVal readNum As Integer, ByRef readedNum As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtSetCharacterSet(ByVal printer As IntPtr, ByVal characterSet As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtGetPrinterVersion(ByVal printer As IntPtr, ByVal version As Integer(), ByVal versionLen As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPositionNextLabel(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR)> _
        Private Shared Function PrtPrintTwoQRCode(ByVal printer As IntPtr, ByVal data1 As String, ByVal width1 As Integer, ByVal hAlign1 As Integer, ByVal vAlign1 As Integer, ByVal data2 As String, _
   ByVal width2 As Integer, ByVal hAlign2 As Integer, ByVal vAlign2 As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrtPrintTwoQRCodeS(ByVal printer As IntPtr, ByVal height As Integer, ByVal data1 As String, ByVal data2 As String) As Integer
        End Function
        Private printer As IntPtr
        Private langid As Integer = 0
        Private E_SUCCESS As Integer = Constants.E_SUCCESS
        Private E_INVALID_PARAMETER As Integer = Constants.E_INVALID_PARAMETER
        Private state As New PrinterState()
        Private modelname As String = ""
        Public Sub New(ByVal model As String)
            Dim result As Integer = E_SUCCESS
            printer = IntPtr.Zero
            result = PrtPrinterCreator(printer, model)
            If E_SUCCESS <> result Then
                Dim errorMsg As String = FormatError(result)
                printer = IntPtr.Zero
                Throw New Exception(errorMsg)
            End If
            modelname = model
        End Sub
        Public Function FormatError(ByVal error_no As Integer) As String
            Dim temp As Byte() = New Byte(512) {}
            PrtFormatError(error_no, langid, temp, 0, 512)
            Return System.Text.Encoding.[Default].GetString(temp, 0, 512)
        End Function
        Public Property Model() As String
            Get
                Return modelname
            End Get
            Set(ByVal value As String)
                If value.ToUpper().Trim() <> modelname.ToUpper().Trim() Then
                    Dim p As IntPtr = IntPtr.Zero
                    If Constants.E_SUCCESS = PrtPrinterCreator(p, value) Then
                        PrtPrinterDestroy(printer)
                        printer = p
                        modelname = value
                    End If
                End If
            End Set
        End Property
        Public Function ChangeModel(ByVal modelName As String) As Integer
            Dim result As Integer = Constants.E_SUCCESS
            Return result
        End Function
        Public Function PortOpen(ByVal uri As String) As Integer
            If printer <> IntPtr.Zero Then
                Return PrtPortOpen(printer, uri)
            Else
                Return Constants.E_BAD_HANDLE
            End If
        End Function
        Public Function PortClose() As Integer
            Dim result As Integer = E_SUCCESS
            result = PrtPortClose(printer)
            Return result
        End Function
        Public Function Initialize() As Integer
            Return PrtPrinterInitialize(printer)
        End Function
        Public Function PrintText(ByVal text As String, ByVal alignment As Integer, ByVal attribute As Integer, ByVal textsize As Integer) As Integer
            Dim len As Integer = text.Length
            Dim data As Byte() = Encoding.GetEncoding("BIG5").GetBytes(text)
            Return PrtPrintText(printer, data, alignment, attribute, textsize)
        End Function
        Public Function PrintBarCode(ByVal bcType As Integer, ByVal bcData As String, ByVal width As Integer, ByVal height As Integer, ByVal alignment As Integer, ByVal hriPosition As Integer) As Integer
            Return PrtPrintBarCode(printer, bcType, bcData, width, height, alignment, _
             hriPosition)
        End Function
        Public Function PrintSymbol(ByVal type As Integer, ByVal data As String, ByVal errLevel As Integer, ByVal width As Integer, ByVal height As Integer, ByVal alignment As Integer) As Integer
            Return PrtPrintSymbol(printer, type, data, errLevel, width, height, _
             alignment)
        End Function
        Public Function DefineDownloadedImageCompatible(ByVal imagePath As String) As Integer
            Return PrtDefineDownloadedImageCompatible(printer, imagePath)
        End Function
        Public Function PrintDownloadedImageCompatible(ByVal scaleMode As Integer) As Integer
            Return PrtPrintDownloadedImageCompatible(printer, scaleMode)
        End Function
        Public Function PrintImage(ByVal imagePath As String, ByVal scaleMode As Integer) As Integer
            Return PrtPrintImage(printer, imagePath, scaleMode)
        End Function
        Public Function DefineNVImageCompatible(ByVal fileList As String(), ByVal ImageQty As Integer) As Integer
            Return PrtDefineNVImageCompatible(printer, fileList, ImageQty)
        End Function
        Public Function PrintNVImageCompatible(ByVal imageNo As Integer, ByVal scaleMode As Integer) As Integer
            Return PrtPrintNVImageCompatible(printer, imageNo, scaleMode)
        End Function
        Public Function DefineDownloadedImage(ByVal imagePath As String, ByVal kc1 As Byte, ByVal kc2 As Byte) As Integer
            Return PrtDefineDownloadedImage(printer, imagePath, kc1, kc2)
        End Function
        Public Function PrintDownloadedImage(ByVal kc1 As Byte, ByVal kc2 As Byte) As Integer
            Return PrtPrintDownloadedImage(printer, kc1, kc2)
        End Function
        Public Function DefineBufferedImage(ByVal imagePath As String) As Integer
            Return PrtDefineBufferedImage(printer, imagePath)
        End Function
        Public Function PrintBufferedImage() As Integer
            Return PrtPrintBufferedImage(printer)
        End Function
        Public Function DefineNVImage(ByVal imagePath As String, ByVal kc1 As Byte, ByVal kc2 As Byte) As Integer
            Return PrtDefineNVImage(printer, imagePath, kc1, kc2)
        End Function
        Public Function PrintNVImage(ByVal kc1 As Byte, ByVal kc2 As Byte) As Integer
            Return PrtPrintNVImage(printer, kc1, kc2, 1, 1)
        End Function
        Public Function FeedLine(ByVal lines As Integer) As Integer
            Return PrtFeedLine(printer, lines)
        End Function
        Public Function SetAlign(ByVal align As Integer) As Integer
            Return PrtSetAlign(printer, align)
        End Function
        Public Function CutPaper(ByVal cutMode As Integer, ByVal distance As Integer) As Integer
            Return PrtCutPaper(printer, cutMode, distance)
        End Function
        Public Function OpenDrawer(ByVal pinMode As Integer, ByVal onTime As Integer, ByVal offTime As Integer) As Integer
            Return PrtOpenDrawer(printer, pinMode, onTime, offTime)
        End Function
        Public Function SelectStandardMode() As Integer
            Return PrtSelectStandardMode(printer)
        End Function
        Public Function SetTextLineSpacing(ByVal spacing As Integer) As Integer
            Return PrtSetTextLineSpace(printer, spacing)
        End Function
        Public Function SetTextPosition(ByVal position As Integer) As Integer
            Return PrtSetTextPosition(printer, position)
        End Function
        Public Function SelectPageMode() As Integer
            Return PrtSelectPageMode(printer)
        End Function
        Public Function SetPageModePrintArea(ByVal horizontal As Integer, ByVal vertical As Integer, ByVal width As Integer, ByVal height As Integer) As Integer
            Return PrtSetPrintAreaInPageMode(printer, horizontal, vertical, width, height)
        End Function
        Public Function CancelPrintData() As Integer
            Return PrtCancelPrintDataInPageMode(printer)
        End Function
        Public Function SetPageModePrintDirection(ByVal direction As Integer) As Integer
            Return PrtSelectPrintDirectionInPageMode(printer, direction)
        End Function
        Public Function SetPageModeHorizontalPosition(ByVal position As Integer) As Integer
            Return PrtSetAbsolutePrintPosition(printer, position)
        End Function
        Public Function SetPageModeVerticalPosition(ByVal position As Integer) As Integer
            Return PrtSetAbsoluteVerticalPrintPositionInPageMode(printer, position)
        End Function
        Public Function PrintAndReturnStandardMode() As Integer
            Return PrtSetPrintAndReturnStandardMode(printer)
        End Function
        Public Function PositionNextLabel() As Integer
            Return PrtPositionNextLabel(printer)
        End Function
        Public Function PrintDataInPageMode() As Integer
            Return PrtPrintDataInPageMode(printer)
        End Function
        Public Function CheckPrinterState(ByRef printerState As UInteger) As Integer
            Return PrtCheckPrinterState(printer, printerState)
        End Function
        Public Function GetState(ByRef printerState As PrinterState) As Integer
            Dim s As UInteger = 0
            Dim result As Integer = E_SUCCESS
            result = CheckPrinterState(s)
            If E_SUCCESS = result Then
                printerState.State = s
                state.State = s
            Else
                printerState.State = printerState.State Or PrinterState.STS_ERROR
                state.State = state.State Or PrinterState.STS_ERROR
            End If
            Return result
        End Function
        Public Function DirectIO(ByVal writedata As Byte(), ByVal readdata As Byte(), ByVal readnum As Integer, ByRef readednum As Integer) As Integer
            If writedata Is Nothing OrElse writedata.Length = 0 Then
                MessageBox.Show("No data to write!")
                Return E_INVALID_PARAMETER
            End If
            If True Then
                Dim readedcnt As Integer = 0
                Dim errorno As Integer = PrtDirectIO(printer, writedata, writedata.Length, readdata, readnum, readedcnt)
                If E_SUCCESS = errorno Then
                    readednum = readedcnt
                End If
                Return errorno
            End If
            Return 0
        End Function
        Public Function SetCharacterSet(ByVal CharacterSet As Integer) As Integer
            Return PrtSetCharacterSet(printer, CharacterSet)
        End Function
        Public Sub PrintText2Image(ByVal path As String, ByVal text As String, ByVal font_mode As FontStyle, ByVal font_size As Integer)
            Dim bmp_height As Integer = font_size * 2
            Dim bmp As System.Drawing.Bitmap = New Bitmap(384, bmp_height)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.Clear(Color.White)
            Dim brush As New SolidBrush(Color.Black)
            Dim font As New Font(FontFamily.GenericSerif, font_size, font_mode)
            g.DrawString(text, font, brush, 0, 5)
            bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp)
            font.Dispose()
            g.Dispose()
            PrtPrintImage(printer, path, Constants.PRINT_IMAGE_NORMAL)
        End Sub
        Public Function GetPrinterVersion(ByVal Version As Integer()) As Integer
            Return PrtGetPrinterVersion(printer, Version, Version.Length)
        End Function
        Public Function PrintTwoQRCode(ByVal data1 As String, ByVal width1 As Integer, ByVal hAlign1 As Integer, ByVal vAlign1 As Integer, ByVal data2 As String, ByVal width2 As Integer, _
         ByVal hAlign2 As Integer, ByVal vAlign2 As Integer) As Integer
            Return PrtPrintTwoQRCode(printer, data1, width1, hAlign1, vAlign1, data2, _
             width2, hAlign2, vAlign2)
        End Function
        Public Function PrintTwoQRCodeS(ByVal height As Integer, ByVal data1 As String, ByVal data2 As String) As Integer
            Return PrtPrintTwoQRCodeS(printer, height, data1, data2)
        End Function

        Private Function PrtDirectIO(ByVal printer As IntPtr, ByVal writedata As Byte(), ByVal p3 As Object, ByVal readdata As Byte(), ByVal p5 As Object, ByVal readedcnt As UInteger) As Integer
            Throw New NotImplementedException
        End Function

    End Class
End Namespace
