using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;



namespace Sample_Print_CSharp
{
    public partial class Form1 : Form
    {
        [DllImport("WoosimPrinter.DLL")]
        public static extern int InitPrinter(string sPortName, int iBaudRate, int bProtocol);
        [DllImport("WoosimPrinter.DLL")]
        public static extern int ClosePrinter();
        [DllImport("WoosimPrinter.DLL")]
        public static extern int ControlCommand(byte[] Cmds, int iLength);
        [DllImport("WoosimPrinter.DLL")]
        public static extern int SaveSpool(string bstrData, int iFontValue, int bEmphasis);
        [DllImport("WoosimPrinter.DLL")]
        public static extern int BmpSaveSpool(string sBmpFilePath, int iXPosition, int iYPosition);
        [DllImport("WoosimPrinter.DLL")]
        public static extern int PrintSpool(int bDeleteSpool);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void ClearSpool();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void CardRead(int bTrack2);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void CardCancel();
        [DllImport("WoosimPrinter.DLL")]
        public static extern int InitWlanPrinter(string sIP_ADDR, int iPortNum, int nTimeout, int bProtocol);
               [DllImport("WoosimPrinter.DLL")]
        public static extern int BarcodeSaveSpool(int ucBarcodeType, int ucBarWidth, int ucBarHeight, bool HRI_On,
                                           string BarcodeData, int iPrtWidth, int iPrtHeight, int iXPos);
      
        [DllImport("user32.dll")]
        public static extern uint RegisterWindowMessage(string lpString);

        private uint UWM_RECEIVE_MSG = RegisterWindowMessage("WOOSIM_PRT_OK");
            

        /****************************************************************************
             CONST DEFINE
         ****************************************************************************/
        private const int ACK = 0x06; //                                       
        private const int NACK = 0x15; //                                       
        private const int ENQ = 0x05; //                                   
        private const int DATA = 0x44; //			                  
        private const int STATUS_REQUEST = 0x51; //                                     
        private const int STATUS_ANSWER = 0x53; //                                        
        private const int EOT = 0x04; //                                        
        private const int ETX = 0x03; //
        private const int CAN = 0x18; //   
        private const int MSR_FAIL = 0x4d;

        // Return Message 
        private const int SUCCESS = 1;
        private const int SOCKET_ERROR = -1;
        private const int CONNECT_FAIL = -2;
        private const int ALREADY_CONNECTED = -3;

        private const int NOT_OPEN_THE_PORT = -11;
        private const int BUFFER_OVERFLOW = -12;
        private const int OPERATOR_ERROR = -13;

       //Barcode type
        public const Int32 UPCA = 65;
        public const Int32 UPCE = 66;
        public const Int32 EAN13 = 67;
        public const Int32 EAN8 = 68;
        public const Int32 CODE39 = 69;
        public const Int32 ITF = 70;
        public const Int32 CODABAR = 71;
        public const Int32 CODE93 = 72;
        public const Int32 CODE128 = 73;

        //print width		
        private const int _2INCH = 384;
        private const int _3INCH = 576;
        private const int _4INCH = 832;


        public Form1()
        {
            InitializeComponent();

            UWM_RECEIVE_MSG = RegisterWindowMessage("WOOSIM_PRT_OK");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            byte[] retVal = new byte[1];
            byte[] CmdTmp = new byte[10];


            string sPort = this.comboBox2.Text + ":";
            int iBaudRate = int.Parse(comboBox3.Text);
            int bProtocol = int.Parse(comboBox1.Text);

            string sIP_ADDR = this.textIPaddress.Text;

                       
            if(checkBox1.Checked == false)
            {
                long val = InitPrinter(sPort, iBaudRate, bProtocol);

                 try
                {
                    switch (val)
                    {
                        case 1:
                            this.textBox1.Text = "Success";
                            break;
                        case -1:
                            this.textBox1.Text = "Already opened";
                            break;
                        case -2:
                            this.textBox1.Text = "Unable to open the port";
                            break;
                        case -3:
                            this.textBox1.Text = "Unable to configure the serial port";
                            break;
                        case -4:
                            this.textBox1.Text = "Unable to set the timeout parameters";
                            break;
                        case -5:
                            this.textBox1.Text = "No response from printer";
                            break;
                    }
                }
                catch
                {
                    MessageBox.Show(e.ToString());
                }

            } 
            else
            {
                long val = InitWlanPrinter(sIP_ADDR, 1470, 3000, bProtocol);

                try
                {
                    switch (val)
                    {
                        case 1:
                            this.textBox1.Text = "Success";
                            break;
                        case -1:
                            this.textBox1.Text = "SOCKET_ERROR";
                            break;
                        case -2:
                            this.textBox1.Text = "CONNECT_FAIL";
                            break;
                        case -3:
                            this.textBox1.Text = "ALREADY_CONNECTED";
                            break;
                        case -4:
                            this.textBox1.Text = "Unable to set the timeout parameters";
                            break;
                        case -5:
                            this.textBox1.Text = "No response from printer";
                            break;
                    }
                }
                catch
                {
                    MessageBox.Show(e.ToString());
                }
            }
           
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "Close Port";
            ClosePrinter();
        }

        private void button4_Click(object sender, EventArgs e)
        {
           
            CardRead(0); //card read function      // 0 : 2Track      // 1: 3Track

        }

        private void button3_Click(object sender, EventArgs e)
        {
           
            CardRead(1); //card read function      // 0 : 2Track      // 1: 3Track
        }

        private void button6_Click(object sender, EventArgs e)
        {
         
            byte[] Card23 = { 0x1b, 0x4d, 0x45 };
            ControlCommand(Card23, 3);
            PrintSpool(1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CardCancel();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ///////////////////////
            // Print bitmap   //
            ///////////////////////
            //printer init
            byte[] printinit = { 0x1b, 0x40 };
            ControlCommand(printinit, 2);

            //page mode
            byte[] Page_Mode1 = { 0x1b, 0x4c };
            ControlCommand(Page_Mode1, 2);

            //page area setting
            //1inch printer width : 192dot = 0xC0
            //2inch printer width : 384dot = 0x180
            //3inch printer width : 576dot = 0x240
            //4inch printer width : 832dot = 0x340


            //page area set
            //3inch printer width : 576dot = 0x240
            //Page Size setting ESC W   384x176x2 bitmap
            // 1dot = 0.0125cm  
            // calculation : X : 0x180 => 384(decimal,2inch width)  
            //	              Y : 0xa7  => 167(decimal) => 167*0.0125 = 2.08cm
            byte[] PageSize_Set1 = { 0x1b, 0x57, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0xa7, 0x00 };
            ControlCommand(PageSize_Set1, 10);

            //page area clear
            byte[] Page_Init1 = { 0x18 };
            ControlCommand(Page_Init1, 1);

            //bitmap 
            BmpSaveSpool("woosim.bmp", 0, 0);

            //page area print
            byte[] Feed2 = { 0x0c };
            ControlCommand(Feed2, 1);

            PrintSpool(1);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            ///////////////////////
            // Barcode    
            //

            //Barcode Type
            //#define UPCA									    65
            //#define UPCE									    66
            //#define EAN13									    67
            //#define EAN8								    	68
            //#define CODE39									69
            //#define ITF										70
            //#define CODABAR									71
            //#define CODE93									72
            //#define CODE128									73

            ////Printer width		
            //#define _2INCH									384
            //#define _3INCH									576
            //#define _4INCH									832

            //Printer init
            byte[] Print_init = { 0x1b, 0x40 };
            ControlCommand(Print_init, 2);

            //Barcode Print
            BarcodeSaveSpool(UPCA, 2, 100, true, "12345678901", _2INCH, 200, 50);
            BarcodeSaveSpool(UPCE, 2, 100, true, "02345678901", _2INCH, 200, 50);
            BarcodeSaveSpool(EAN13, 2, 100, true, "12345678901", _2INCH, 200, 100);
            BarcodeSaveSpool(EAN8, 2, 100, true, "12345678", _2INCH, 200, 150);
            BarcodeSaveSpool(CODE39, 2, 100, true, "12345678901", _2INCH, 200, 200);
            BarcodeSaveSpool(ITF, 2, 100, true, "123456", _2INCH, 200, 250);
            BarcodeSaveSpool(CODABAR, 2, 100, true, "123456", _2INCH, 200, 0);
            BarcodeSaveSpool(CODE93, 2, 100, true, "12345678901", _2INCH, 200, 0);
            BarcodeSaveSpool(CODE128, 2, 100, true, "12345678901", _2INCH, 200, 0);

            PrintSpool(1);


            /*
            ///////////////////////
            // Barcode command
            //
               
            
           //Printer initialization ESC @
            byte[] printinit = { 0x1b, 0x40 };
            ControlCommand(printinit, 2);

            //print 
            byte[] Feed2 = { 0x0c };
            ControlCommand(Feed2, 1);

            //Print Barcode
            // Print initialization
            ControlCommand(printinit, 2);

            //Barcode height GS h
            byte[] GS_h = { 0x1d, 0x68, 0x44 };
            ControlCommand(GS_h, 3);

            //Barcode width GS w
            byte[] GS_w = { 0x1d, 0x77, 0x02 };
            ControlCommand(GS_w, 3);

            // HRI  character print  GS H
            byte[] GS_H = { 0x1d, 0x48, 0x01 };
            ControlCommand(GS_H, 3);

            SaveSpool("UPC-A\r\n", 0, 0);
            byte[] GS_k41 = { 0x1d, 0x6b, 0x41, 0x0b, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x33, 0x33 };
            ControlCommand(GS_k41, 15);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            SaveSpool("UPC-E\r\n", 0, 0);
            byte[] GS_k42 = { 0x1d, 0x6b, 0x42, 0x0b, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x33 };
            ControlCommand(GS_k42, 15);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            SaveSpool("EAN13\r\n", 0, 0);
            byte[] GS_k43 = { 0x1d, 0x6b, 0x43, 0x0b, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x33, 0x33 };
            ControlCommand(GS_k43, 15);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            SaveSpool("EAN8\r\n", 0, 0);
            byte[] GS_k44 = { 0x1d, 0x6b, 0x44, 0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
            ControlCommand(GS_k44, 12);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            SaveSpool("code39\r\n", 0, 0);
            byte[] GS_k45 = { 0x1d, 0x6b, 0x45, 0x0b, 0x41, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x5a };
            ControlCommand(GS_k45, 15);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            SaveSpool("ITF\r\n", 0, 0);
            byte[] GS_k46 = { 0x1d, 0x6b, 0x46, 0x06, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 };
            ControlCommand(GS_k46, 10);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            SaveSpool("codabar\r\n", 0, 0);
            byte[] GS_k47 = { 0x1d, 0x6b, 0x47, 0x0b, 0x41, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x44 };
            ControlCommand(GS_k47, 15);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            SaveSpool("code93\r\n", 0, 0);
            byte[] GS_k48 = { 0x1d, 0x6b, 0x48, 0x0b, 0x21, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x7d };
            ControlCommand(GS_k48, 15);
            ControlCommand(GS_H, 3);
            ControlCommand(Feed2, 1);

            PrintSpool(1);
            */


        }

        private void button9_Click(object sender, EventArgs e)
        {
            //Print init ESC @
            byte[] printinit = { 0x1b, 0x40 };
            ControlCommand(printinit, 2);

            //Title
            SaveSpool("  SALES  SLIP\n", 3, 1);
            SaveSpool("--------------------------------\n", 0, 0);
            //Company
            SaveSpool("MERCHANT NAME      Woosim Coffee\n", 0, 1);
            //Reg Number
            SaveSpool("MASTER                LEE IL BOK\n", 0, 0);
            //Address
            SaveSpool("ADDRESS\n #501, DaerungTechnotown3th 448,\n  Gasan-dong, Geumchun-gu, Seoul\n  Korea\n", 0, 0);
            //Tel
            SaveSpool("HELP DESK       (+82-2)2107-3714\n", 0, 0);
            SaveSpool("--------------------------------\n\n", 0, 0);
            SaveSpool("        WOOSIM  CARD \n", 1, 0);
            SaveSpool("--------------------------------\n", 0, 0);
            SaveSpool("CARD NO.     4428-8819-0118-****\n", 0, 0);
            SaveSpool("EXPIRY                   2012/10\n", 0, 0);
            SaveSpool("--------------------------------\n", 0, 0);
            SaveSpool("TRANS. YEAR & DATE   2008/04/15\n", 0, 0);
            SaveSpool("TRANS CLASS             [Credit]\n", 0, 0);
            SaveSpool("TERMINAL ID.           123456789\n", 0, 0);
            SaveSpool("APPROVAL NO.           987654321\n", 0, 0);
            SaveSpool("AMOUNT                   500.00$\n", 0, 0);
            SaveSpool("--------------------------------\n", 0, 0);
            SaveSpool("SIGNATURE: \n\n", 0, 0);
            SaveSpool("--------------------------------\n\n", 0, 0);

            PrintSpool(1);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            ClosePrinter();
            Application.Exit();
         }

        private void button11_Click(object sender, EventArgs e)
        {
            //Printer initialization ESC @
            byte[] Print_init ={ 0x1b, 0x40 };
            ControlCommand(Print_init, 2);

            //Barcode print 
            byte[] FF = { 0x0c };

            SaveSpool("PDF417\r\n", 0, 0);

            //Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE / 3 = Micro PDF417 / 4 = Trunc PDF417)
            byte[] GS_Z = { 0x1d, 0x5a, 0x00 };
            ControlCommand(GS_Z, 3);

            byte[] GS_W = { 0x1d, 0x77, 0x03 };
            ControlCommand(GS_W, 3);


            //Print 2D barcode
            byte[] ESC_Z = {0x1b, 0x5a,
		                    0x01 , 0x00, 0X03, 0x0f, 0x00, 
					        0x41,0x42,0x43,0x44,0x45,0x31,0x32,0x33,0x34,0x35,0x61,0x62,0x63,0x64,0x65};
            ControlCommand(ESC_Z, 22);

            ControlCommand(FF, 1);

            byte[] LF ={ 0x0a };
            ControlCommand(LF, 1);


            SaveSpool("Micro PDF417\r\n", 0, 0);
            //Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE / 3 = Micro PDF417 / 4 = Trunc PDF417)
            byte[] GS_Z1 = { 0x1d, 0x5a, 0x03 };
            ControlCommand(GS_Z1, 3);

            ControlCommand(GS_W, 3);

            ControlCommand(ESC_Z, 22);

            ControlCommand(LF, 1);

            SaveSpool("Truncated PDF417\r\n", 0, 0);
            //Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE / 3 = Micro PDF417 / 4 = Trunc PDF417)
            byte[] GS_Z2 = { 0x1d, 0x5a, 0x04 };
            ControlCommand(GS_Z2, 3);

            ControlCommand(GS_W, 3);

            ControlCommand(ESC_Z, 22);

            ControlCommand(LF, 1);

            SaveSpool("DATAMAREIX\r\n", 0, 0);
            //Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE / 3 = Micro PDF417 / 4 = Trunc PDF417)
            byte[] GS_Z3 = { 0x1d, 0x5a, 0x01 };
            ControlCommand(GS_Z3, 3);


            //Print 2D barcode
            byte[] ESC_Z1 = {0x1b, 0x5a,
		                    0x00 , 0x00, 0X03, 0x0f, 0x00, 
					        0x41,0x42,0x43,0x44,0x45,0x31,0x32,0x33,0x34,0x35,0x61,0x62,0x63,0x64,0x65};
            ControlCommand(ESC_Z1, 22);

            ControlCommand(LF, 1);

            SaveSpool("QR-CODE\r\n", 0, 0);
            //Select 2D barcode type GS Z  n (0 = PDF417 / 1 = DATAMATRIX / 2 = QR-CODE / 3 = Micro PDF417 / 4 = Trunc PDF417)
            byte[] GS_Z4 = { 0x1d, 0x5a, 0x02 };
            ControlCommand(GS_Z4, 3); 


            //Print 2D barcode
            byte[] ESC_Z2 = {0x1b, 0x5a,
		                    0x00 , 0x4d, 0X03, 0x0f, 0x00, 
					        0x41,0x42,0x43,0x44,0x45,0x31,0x32,0x33,0x34,0x35,0x61,0x62,0x63,0x64,0x65};
            ControlCommand(ESC_Z2, 22);

            ControlCommand(LF, 1);
            PrintSpool(1);
        }

        // message 
        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == UWM_RECEIVE_MSG)
            {
                if (msg.WParam == (IntPtr)DATA)
                {
                    this.textBox1.Text = "DATA";

                    string sScanData;

                    unsafe
                    {
                        sbyte* pScanData = (sbyte*)msg.LParam.ToPointer();
                        sScanData = new string(pScanData);
                    }

                    textBox2.Text = sScanData.ToString();

                }
                else if (msg.WParam == (IntPtr)EOT)
                {
                    this.textBox1.Text = "EOT";
                }
                else if (msg.WParam == (IntPtr)ETX)
                {
                    this.textBox1.Text = "ETX";
                }
                else if (msg.WParam == (IntPtr)ACK)
                {
                    this.textBox1.Text = "ACK";
                }
                else if (msg.WParam == (IntPtr)NACK)
                {
                    this.textBox1.Text = "NACK";
                }
                else if (msg.WParam == (IntPtr)MSR_FAIL)
                {
                    this.textBox1.Text = "MSR_FAIL";
                }
            }
            base.WndProc(ref msg);
        }
      
    }
}

