Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.IO.Ports
Imports System.Text.RegularExpressions
Imports TSPL_VB.Printer

Public Class TSPL_Demo
    Inherits Form
    Public PageModePrinterList As New List(Of String)()
    Private printer As HPRTPrinter = Nothing
    Private isPageModePrinter As Boolean
    Private imagePath As String
    Private Index As Integer

    Private Sub btnConnect_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConnect.Click
        Dim result As Integer = 0
        Dim modelsetting As String = Me.cmbModel.Text
        Dim portsetting As String = Me.txtPortsetting.Text
        printer = New HPRTPrinter(modelsetting)
        result = printer.DemPortOpen(portsetting)
        If Constants.E_SUCCESS = result Then
            isPageModePrinter = PageModePrinterList.Contains(modelsetting)
        Else
            Dim errorMsg As String = printer.FormatError(result)
            Throw New Exception(errorMsg)
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnConnect.Click

    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbPort.SelectedIndexChanged
        If cmbPort.SelectedItem = "USB" Then
            txtPortsetting.Text = "USB"
        Else
            txtPortsetting.Text = "NET 192.168.0.1"
        End If
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles btnSelfTest.Click
        printer.DemSelfTest()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles btnFormFeed.Click
        printer.DemFormFeed()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnText.Click
        Dim x, y, font, rota, x_mul, y_mul, alignment, result As Integer
        Dim data As String


        If Edit_text_x.Text = "" Then
            MessageBox.Show("X Start not null!")
        ElseIf Edit_text_y.Text = "" Then
            MessageBox.Show("Y Start not null!")
        ElseIf Edit_text_font.Text = "" Then
            MessageBox.Show("font not null!")
        ElseIf Edit_text_rotation.Text = "" Then
            MessageBox.Show("rotation not null!")
        ElseIf Edit_text_x_mul.Text = "" Then
            MessageBox.Show("x_mul not null!")
        ElseIf Edit_text_y_mul.Text = "" Then
            MessageBox.Show("y_mul not null!")
        ElseIf Edit_text_alignment.Text = "" Then
            MessageBox.Show("alignment not null!")
        ElseIf Edit_text_data.Text = "" Then
            MessageBox.Show("data not null!")
        Else
            x = Val(Edit_text_x.Text)
            y = Val(Edit_text_y.Text)
            font = Val(Edit_text_font.Text)
            rota = Val(Edit_text_rotation.Text)
            x_mul = Val(Edit_text_x_mul.Text)
            y_mul = Val(Edit_text_y_mul.Text)
            alignment = Val(Edit_text_alignment.Text)
            data = Edit_text_data.Text
            printer.DemClearBuffer()
            printer.DemSetup(100, 80, 2, 6, 1, 2, 0)
            result = printer.DemText(x, y, font, rota, x_mul, y_mul, alignment, data)
            printer.DemPrint(1, 1)

            'Dim num1() As Byte = {&H53, &H45, &H4C, &H46, &H54, &H45, &H53, &H54, &HD, &HA}
            'Dim readnum As Integer
            'printer.DemDirectIO(num1, num1.Length, num1, 0, readnum)
        End If
    End Sub

    Private Sub btn_bar_Click(sender As Object, e As EventArgs) Handles btn_bar.Click
        Dim x, y, type, height, readable, rota, narrow, wide, result As Integer
        Dim data As String
        If Edit_bar_x.Text = "" Then
            MessageBox.Show("X Start not null!")
        ElseIf Edit_bar_y.Text = "" Then
            MessageBox.Show("Y Start not null!")
        ElseIf Edit_bar_type.Text = "" Then
            MessageBox.Show("barcode type not null!")
        ElseIf Edit_bar_height.Text = "" Then
            MessageBox.Show("height not null!")
        ElseIf Edit_bar_read.Text = "" Then
            MessageBox.Show("readable not null!")
        ElseIf Edit_bar_rotation.Text = "" Then
            MessageBox.Show("rotation not null!")
        ElseIf Edit_bar_narrow.Text = "" Then
            MessageBox.Show("narrow not null!")
        ElseIf Edit_bar_wide.Text = "" Then
            MessageBox.Show("wide not null!")
        ElseIf Edit_bar_data.Text = "" Then
            MessageBox.Show("data not null!")
    
        Else
            x = Val(Edit_bar_x.Text)
            y = Val(Edit_bar_y.Text)
            type = Val(Edit_bar_type.Text)
            height = Val(Edit_bar_height.Text)
            readable = Val(Edit_bar_read.Text)
            rota = Val(Edit_bar_rotation.Text)
            narrow = Val(Edit_bar_narrow.Text)
            wide = Val(Edit_bar_wide.Text)
            data = Edit_bar_data.Text
          
            printer.DemClearBuffer()
            printer.DemSetup(100, 80, 2, 6, 1, 2, 0)
            result = printer.DemBarCode(x, y, type, height, readable, rota, narrow, wide, data)
            printer.DemPrint(1, 1)
        End If
    End Sub

    Private Sub btnQR_Click(sender As Object, e As EventArgs) Handles btnQR.Click
        Dim x, y, eccleve, width, mode, rota, model, mask, result As Integer
        Dim data As String
        If Edit_QR_x.Text = "" Then
            MessageBox.Show("X Start not null!")
        ElseIf Edit_QR_y.Text = "" Then
            MessageBox.Show("Y Start not null!")
        ElseIf Edit_QR_eccl.Text = "" Then
            MessageBox.Show("eccleve not null!")
        ElseIf Edit_QR_width.Text = "" Then
            MessageBox.Show("width not null!")
        ElseIf Edit_QR_mode.Text = "" Then
            MessageBox.Show("mode not null!")
        ElseIf Edit_QR_rotation.Text = "" Then
            MessageBox.Show("rotation not null!")
        ElseIf Edit_QR_data.Text = "" Then
            MessageBox.Show("data not null!")
        Else
            x = Val(Edit_QR_x.Text)
            y = Val(Edit_QR_y.Text)
            eccleve = Val(Edit_QR_eccl.Text)
            width = Val(Edit_QR_width.Text)
            mode = Val(Edit_QR_mode.Text)
            rota = Val(Edit_QR_rotation.Text)
            data = Edit_QR_data.Text
            model = Val(Edit_QR_model.Text)
            mask = Val(Edit_QR_mask.Text)
            printer.DemClearBuffer()
            printer.DemSetup(100, 80, 2, 6, 1, 2, 0)
            result = printer.DemQrCode(x, y, eccleve, width, mode, rota, model, mask, data)
            printer.DemPrint(1, 1)
        End If
    End Sub

    Private Sub btnBitMap_Click(sender As Object, e As EventArgs) Handles btnBitMap.Click
        Dim x, y, mode As Integer
        Dim data As String
        If Edit_BM_x.Text = "" Then
            MessageBox.Show("X Start not null!")
        ElseIf Edit_BM_y.Text = "" Then
            MessageBox.Show("Y Start not null!")
        ElseIf Edit_BM_mode.Text = "" Then
            MessageBox.Show("mode not null!")
        ElseIf Edit_BM_data.Text = "" Then
            MessageBox.Show("data not null!")
        Else
            x = Val(Edit_BM_x.Text)
            y = Val(Edit_BM_y.Text)
            mode = Val(Edit_BM_mode.Text)
            data = Edit_BM_data.Text
            printer.DemClearBuffer()
            printer.DemSetup(100, 80, 2, 6, 1, 2, 0)
            printer.DemBitMap(x, y, mode, data)
            printer.DemPrint(1, 1)
        End If
    End Sub

    Private Sub btnBox_Click(sender As Object, e As EventArgs) Handles btnBox.Click
        Dim x, y, x_end, y_end, wide As Integer
        If Edit_box_x.Text = "" Then
            MessageBox.Show("X Start not null!")
        ElseIf Edit_box_y.Text = "" Then
            MessageBox.Show("Y Start not null!")
        ElseIf Edit_box_x_end.Text = "" Then
            MessageBox.Show("x end not null!")
        ElseIf Edit_box_y_end.Text = "" Then
            MessageBox.Show("y end not null!")
        ElseIf Edit_box_width.Text = "" Then
            MessageBox.Show("width not null!")
        Else
            x = Val(Edit_box_x.Text)
            y = Val(Edit_box_y.Text)
            x_end = Val(Edit_box_x_end.Text)
            y_end = Val(Edit_box_y_end.Text)
            wide = Val(Edit_box_width.Text)
            printer.DemClearBuffer()
            printer.DemSetup(100, 80, 2, 6, 1, 2, 0)
            printer.DemBox(x, y, x_end, y_end, wide)
            printer.DemPrint(1, 1)
        End If
    End Sub

    Private Sub btnDirection_Click(sender As Object, e As EventArgs) Handles btnDirection.Click
        Dim rota As Integer
        If Edir_Dir_rotation.Text = "" Then
            MessageBox.Show("rotation not null!")
        Else
            rota = Val(Edir_Dir_rotation.Text)
            printer.DemDirection(rota)
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        printer.DemPortClose()

    End Sub
End Class