Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Drawing
Namespace 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 Define
        'rotation
        Public Shared ReadOnly ROTATE_NONE As UInteger = 0
        Public Shared ReadOnly ROTATE_90 As UInteger = 1
        Public Shared ReadOnly ROTATE_180 As UInteger = 2
        Public Shared ReadOnly ROTATE_270 As UInteger = 3

        'ECC LEVEL
        Public Shared ReadOnly ECC_LEVEL_L As UInteger = 0
        Public Shared ReadOnly ECC_LEVEL_M As UInteger = 1
        Public Shared ReadOnly ECC_LEVEL_Q As UInteger = 2
        Public Shared ReadOnly ECC_LEVEL_H As UInteger = 3

        'Barcdoe
        Public Shared ReadOnly BARCODE_128 As UInteger = 0
        Public Shared ReadOnly BARCODE_128A As UInteger = 1
        Public Shared ReadOnly BARCODE_128B As UInteger = 2
        Public Shared ReadOnly BARCODE_128C As UInteger = 3
        Public Shared ReadOnly BARCODE_128E As UInteger = 4
        Public Shared ReadOnly BARCODE_39 As UInteger = 5
        Public Shared ReadOnly BARCODE_39C As UInteger = 6
        Public Shared ReadOnly BARCODE_93 As UInteger = 7
        Public Shared ReadOnly BARCODE_CODABAR As UInteger = 8
        Public Shared ReadOnly BARCODE_CODABAR16 As UInteger = 9
        Public Shared ReadOnly BARCODE_EAN13 As UInteger = 10
        Public Shared ReadOnly BARCODE_EAN132 As UInteger = 11
        Public Shared ReadOnly BARCODE_EAN135 As UInteger = 12
        Public Shared ReadOnly BARCODE_EAN8 As UInteger = 13
        Public Shared ReadOnly BARCODE_EAN82 As UInteger = 14

    End Class


    Public Class DemPrinter
        Public Const HPRTDIR As String = "CPCL_SDK.dll"
        Public Const charSet As CharSet = charSet.Ansi

        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function FormatError(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 PrinterCreator(ByRef printer As IntPtr, ByVal model As String) As Integer
        End Function

        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrinterCreatorS(ByVal model As String) As Point
        End Function

        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PrinterDestroy(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PortOpen(ByVal printer As IntPtr, ByVal portSetting As String) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function PortClose(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function DirectIO(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 CPCL_AddLabel(ByVal printer As IntPtr, ByVal offset As Integer, ByVal height As Integer, ByVal qty As Integer) As Integer
        End Function

        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetAlign(ByVal printer As IntPtr, ByVal align As Integer) As Integer
        End Function

        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddText(ByVal printer As IntPtr, ByVal rotate As Integer, ByVal fontType As Integer, ByVal fontSize As Integer, ByVal xPos As Integer, ByVal yPos As Integer, ByVal data As String) As Integer
        End Function

        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddBarCode(ByVal printer As IntPtr, ByVal rotate As Integer, ByVal type As Integer, ByVal width As Integer, ByVal ratio As Integer, ByVal height As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal data As String) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddBarCodeText(ByVal printer As IntPtr, ByVal enable As Integer, ByVal fontType As Integer, ByVal fontSize As Integer, ByVal offset As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddQRCode(ByVal printer As IntPtr, ByVal rotate As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal model As Integer, ByVal unitwidth As Integer, ByVal ecclevel As Integer, ByVal data As String) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddPDF417(ByVal printer As IntPtr, ByVal rotate As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal xdots As Integer, ByVal ydots As Integer, ByVal columns As Integer, ByVal rows As Integer, ByVal ecclevel As Integer, ByVal data As String) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddBox(ByVal printer As IntPtr, ByVal x_pos As Integer, ByVal y_pos As Integer, ByVal xend As Integer, ByVal yend As Integer, ByVal thickness As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddLine(ByVal printer As IntPtr, ByVal x_pos As Integer, ByVal y_pos As Integer, ByVal xend As Integer, ByVal yend As Integer, ByVal thickness As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddImage(ByVal printer As IntPtr, ByVal rotate As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal imagepath As String) As Integer
        End Function
        <DllImport(HPRTDIR, charSet:=charSet, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_AddImageData(ByVal printer As IntPtr, ByVal rotate As Integer, ByVal widthBytes As Integer, ByVal height As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal data As Byte()) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetFontSize(ByVal printer As IntPtr, ByVal width As Integer, ByVal height As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetDensity(ByVal printer As IntPtr, ByVal density As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetSpeed(ByVal printer As IntPtr, ByVal speed As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetTextSpacing(ByVal printer As IntPtr, ByVal spacing As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetLeftMargin(ByVal printer As IntPtr, ByVal margin As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetTextBold(ByVal printer As IntPtr, ByVal bold As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_SetTextUnderline(ByVal printer As IntPtr, ByVal udnderline As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_Abort(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_Print(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_NextLabelPos(ByVal printer As IntPtr) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_PreFeed(ByVal printer As IntPtr, ByVal distance As Integer) As Integer
        End Function
        <DllImport(HPRTDIR, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function CPCL_PostFeed(ByVal printer As IntPtr, ByVal distance As Integer) As Integer
        End Function

        Public 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 = PrinterCreator(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) {}
            FormatError(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 = PrinterCreator(p, value) Then
                        PrinterDestroy(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 PortOpen(printer, uri)
            Else
                Return Constants.E_BAD_HANDLE
            End If
        End Function
        Public Function DemPortClose() As Integer
            Dim result As Integer = E_SUCCESS
            result = PortClose(printer)
            result = PrinterDestroy(printer)
            Return result
        End Function

        Public Function DemDirectIO(ByVal writeData As Byte(), ByVal writenum As Integer, ByVal readData As Byte(), ByVal readNum As Integer, ByRef readedNum As Integer) As Integer
            Dim result As Integer = E_SUCCESS
            result = DirectIO(printer, writeData, writenum, readData, readNum, readedNum)
            Return result
        End Function




        Public Function AddLabel(ByVal offset As Integer, ByVal height As Integer, ByVal qty As Integer) As Integer
            Return CPCL_AddLabel(printer, offset, height, qty)
        End Function
        Public Function SetAlign(ByVal align As Integer) As Integer
            Return CPCL_SetAlign(printer, align)
        End Function
        Public Function AddText(ByVal rotate As Integer, ByVal fontType As Integer, ByVal fontSize As Integer, ByVal xPos As Integer, ByVal yPos As Integer, ByVal data As String) As Integer
            Return CPCL_AddText(printer, rotate, fontType, fontSize, xPos, yPos, data)
        End Function
        Public Function AddBarCode(ByVal rotate As Integer, ByVal type As Integer, ByVal width As Integer, ByVal ratio As Integer, ByVal height As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal data As String) As Integer
            Return CPCL_AddBarCode(printer, rotate, type, width, ratio, height, xpos, ypos, data)
        End Function
        Public Function AddBarCodeText(ByVal enable As Integer, ByVal fontType As Integer, ByVal fontSize As Integer, ByVal offset As Integer) As Integer
            Return CPCL_AddBarCodeText(printer, enable, fontType, fontSize, offset)
        End Function
        Public Function AddQRCode(ByVal rotate As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal model As Integer, ByVal unitwidth As Integer, ByVal ecclevel As Integer, ByVal data As String) As Integer
            Return CPCL_AddQRCode(printer, rotate, xpos, ypos, model, unitwidth, ecclevel, data)
        End Function
        Public Function AddPDF417(ByVal rotate As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal xdots As Integer, ByVal ydots As Integer, ByVal columns As Integer, ByVal rows As Integer, ByVal ecclevel As Integer, ByVal data As String) As Integer
            Return CPCL_AddPDF417(printer, rotate, xpos, ypos, xdots, ydots, columns, rows, ecclevel, data)
        End Function
        Public Function AddBox(ByVal x_pos As Integer, ByVal y_pos As Integer, ByVal xend As Integer, ByVal yend As Integer, ByVal thickness As Integer) As Integer
            Return CPCL_AddBox(printer, x_pos, y_pos, xend, yend, thickness)
        End Function
        Public Function AddLine(ByVal x_pos As Integer, ByVal y_pos As Integer, ByVal xend As Integer, ByVal yend As Integer, ByVal thickness As Integer) As Integer
            Return CPCL_AddLine(printer, x_pos, y_pos, xend, yend, thickness)
        End Function
        Public Function AddImage(ByVal rotate As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal imagepath As String) As Integer
            Return CPCL_AddImage(printer, rotate, xpos, ypos, imagepath)
        End Function
        Public Function AddImageData(ByVal rotate As Integer, ByVal widthBytes As Integer, ByVal height As Integer, ByVal xpos As Integer, ByVal ypos As Integer, ByVal data As Byte()) As Integer
            Return CPCL_AddImageData(printer, rotate, widthBytes, height, xpos, ypos, data)
        End Function
        Public Function SetFontSize(ByVal width As Integer, ByVal height As Integer) As Integer
            Return CPCL_SetFontSize(printer, width, height)
        End Function
        Public Function SetDensity(ByVal density As Integer) As Integer
            Return CPCL_SetDensity(printer, density)
        End Function
        Public Function SetSpeed(ByVal speed As Integer) As Integer
            Return CPCL_SetSpeed(printer, speed)
        End Function
        Public Function SetTextSpacing(ByVal spacing As Integer) As Integer
            Return CPCL_SetTextSpacing(printer, spacing)
        End Function
        Public Function SetLeftMargin(ByVal margin As Integer) As Integer
            Return CPCL_SetLeftMargin(printer, margin)
        End Function
        Public Function SetTextBold(ByVal bold As Integer) As Integer
            Return CPCL_SetTextBold(printer, bold)
        End Function
        Public Function SetTextUnderline(ByVal udnderline As Integer) As Integer
            Return CPCL_SetTextUnderline(printer, udnderline)
        End Function
        Public Function Abort() As Integer
            Return CPCL_Abort(printer)
        End Function
        Public Function Print() As Integer
            Return CPCL_Print(printer)
        End Function
        Public Function NextLabelPos() As Integer
            Return CPCL_NextLabelPos(printer)
        End Function
        Public Function PreFeed(ByVal distance As Integer) As Integer
            Return CPCL_PreFeed(printer, distance)
        End Function
        Public Function PostFeed(ByVal distance As Integer) As Integer
            Return CPCL_PostFeed(printer, distance)
        End Function
    End Class
End Namespace