using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace CSharp
{
    public partial class Form1 : Form
    {
        //1. Printable Data Handling APIs
        [DllImport("WoosimPrinter.DLL")]
        public static extern void ClearSpool();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void CompressedBmpSaveSpool(String bmpFilePath); 
        [DllImport("WoosimPrinter.DLL")]
        public static extern int ControlCommand(byte[] Cmds, int iLength); 
        [DllImport("WoosimPrinter.DLL")]
        public static extern void DataMatrixSaveSpool(int width, int height, int module, String barcodeData);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void GS1DatabarSaveSpool(int type, int n, String  barcodeData);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void LoadLogoSaveSpool(int n);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void MaxicodeSaveSpool(int mode, String barcodeData);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void MicroPDF417SaveSpool(int width, int column, int row, int ratio, String barcodeData, bool HRI);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void NormalBmpSaveSpool(String  bmpFilePath);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void OneDimensionBarcodeSaveSpool(int ucBarcodeType,
                                                    int ucBarWidth, int ucBarHeight, bool HRI_On, string BarcodeData);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_DotFeed(int dots);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_DrawBox(int iXPos, int iYPos, int width, int height, int thickness);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_DrawEllipse(int iXPos, int iYPos, int A_lenght, int B_lenght, int thickness);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_DrawLine(int iXPos, int iYPos, int iX2Pos, int iY2Pos, int thickness);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_LineFeed(int lines);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_Newline();	
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_Print();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_Print_StandardMode();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void PDF417SaveSpool(int width, int column, int level, int ratio, String barcodeData, bool HRI);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void PrintData();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void PrintDotFeed(int dots);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void PrintLineFeed(int lines);
        [DllImport("WoosimPrinter.DLL")]
        public static extern int PrintSpool(bool bDelete_Spool);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void PrintSpoolForTTF(String sData, int iXFontSize, int iYFontSize);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void QRCodeSaveSpool(int version, char level, int module, String barcodeData);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void TextSaveSpool(String textData);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void TruncatedPDF417SaveSpool(int width, int column, int level, int ratio, String barcodeData, bool HRI);

        //2. Printer Mode & Setting APIs
        [DllImport("WoosimPrinter.DLL")]
        public static extern void GetPrinterModelName();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void GetFirmwareVersion();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void GetPrinterStatus(int iTimeoutMsec);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void GetPrinterStatusEx(int iTimeoutMsec);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void InitLineSpace();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void InitPageMode(int iXPos, int iYPos, int width, int height);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void InitPrinterStatus();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_ClearCurrentData();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_SetArea(int iXPos,int iYPos, int width, int height);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_SetDirection(int n);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_SetPosition(int iXPos, int iYPos);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void Page_SetStandardMode();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetAbsPosition(int distance);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetCharCodeTable(int n, int MCU);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetCharSpace(int n);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetFontForTTF(String ttfFile);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetFontSize(int n);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetLineSpace(int n);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetPageMode();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetTextAlignment(int n);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetTextStyle(int underline, bool emphasize, int width, int height, bool reverse);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void SetUpsideDown(bool set);
                       
        //3. Misc Device Handling APIs
        [DllImport("WoosimPrinter.DLL")]
        public static extern void CancelMSRMode();
        [DllImport("WoosimPrinter.DLL")]
        public static extern bool ClosePrinterConnection();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void CancelSCRMode();
        [DllImport("WoosimPrinter.DLL")]
        public static extern int ConnectSerialPrinter(String sPortName, int iBaudRate, int iTimeoutMsec, bool bProtocol);
        [DllImport("WoosimPrinter.DLL")]
        public static extern int ConnectUSBPrinter(int iTimeoutMsec, bool Protocol);
        [DllImport("WoosimPrinter.DLL")]
        public static extern int ConnectWirelessPrinter(String sIP_ADDR, int iPortNum, int iTimeoutMsec, bool bProtcol);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void CutPaper(int mode);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void EnterMSRMode(int n);
        [DllImport("WoosimPrinter.DLL")]
        public static extern void EnterSCRMode();
        [DllImport("WoosimPrinter.DLL")]
        public static extern void FeedToMark();
        
        [DllImport("user32.dll")]
        public static extern uint RegisterWindowMessage(string lpString);

        private uint UWM_RECEIVE_MSG = RegisterWindowMessage("WOOSIM_PRT_OK");

        /****************************************************************************
        CONST DEFINE
        ****************************************************************************/
        //Return Message type                                 
        private const int DATA = 0x44; //
        private const int STATUS_ANSWER = 0x53; //  

        // Return Message        
        private const int SUCCESS = 1;
        private const int ALREADY_OPENED = -1;
        private const int NO_RESPONSE_FROM_PRINTER = -5;
        private const int TIMEOUT = -7;
        private const int NOT_OPEN_THE_PORT = -11;

        //Serial, Bluetooth Err message
        private const int UNABLE_TO_OPEN_THE_PORT = -2;
        private const int UNABLE_TO_CONFIGURE_THE_SERIAL_PORT = -3;
        private const int UNABLE_TO_SET_THE_TIMEOUT_PARAMETERS = -4;        

        //Wireless Err message
        private const int SOCKET_ERROR = -2;
        private const int CONNECT_FAIL = -3;

        //USB Err message
        private const int UNABLE_TO_MAKE_CONNECTION = -2;
        private const int UNABLE_TO_GET_INFOMATION = -3; 
        private const int NOT_WOOSIM_PRINTER = -4;

        private enum Barcode : int { UPCA = 65, UPCE, EAN13, EAN8, CODE39, ITF, CODABAR, CODE93, CODE128 }
        private enum WIDTH : int { _1INCH = 192, _2INCH = 384, _3INCH = 576, _4INCH = 832 }
        private enum TEXTWIDTH : int 
        { 
            TEXTWIDTH01, 
            TEXTWIDTH02, 
            TEXTWIDTH03, 
            TEXTWIDTH04, 
            TEXTWIDTH05, 
            TEXTWIDTH06, 
            TEXTWIDTH07, 
            TEXTWIDTH08
        }
        private enum TEXTHEIGHT : int
        {
            TEXTHEIGHT01,
            TEXTHEIGHT02,
            TEXTHEIGHT03,
            TEXTHEIGHT04,
            TEXTHEIGHT05,
            TEXTHEIGHT06,
            TEXTHEIGHT07,
            TEXTHEIGHT08
        }
        private enum MCU : int { M16C, ARM, RX }

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

        //printer width
        private const int _1INCH = 192;
        private const int _2INCH = 384;
        private const int _3INCH = 576;
        private const int _4INCH = 832;

        //MCU version
        private const int MCU_RX = 2;
        private const int MCU_ARM = 1;
        private const int MCU_M16C = 0;
        */
        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 3;
            comboBox4.SelectedIndex = 1;
            textBox2.Text = "192.168.0.10";
            textBox3.Text = "1470";
            panel1.Location = new Point(12, 35);
            panel2.Location = new Point(12, 35);
            panel3.Location = new Point(12, 35);
         }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                panel1.Show();
                panel2.Hide();
                panel3.Hide();
            }
            if (comboBox1.SelectedIndex == 1)
            {
                panel1.Hide();
                panel2.Show();
                panel3.Hide();
            }
            if (comboBox1.SelectedIndex == 2)
            {
                panel1.Hide();
                panel2.Hide();
                panel3.Show();
            }
        }

        //Make a Connection between Printer and terminal(PC, PDA..)
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)  //Serial
            {
                int BaudRate = Int32.Parse(comboBox3.SelectedItem.ToString());
              
                long val = ConnectSerialPrinter(comboBox2.SelectedItem.ToString(), BaudRate, 1000, false);

                try
                {
                    switch (val)
                    {
                        case SUCCESS:
                            this.label4.Text = "Success";
                            break;
                        case ALREADY_OPENED:
                            this.label4.Text = "Already opened";
                            break;
                        case UNABLE_TO_OPEN_THE_PORT:
                            this.label4.Text = "Unable to open the port";
                            break;
                        case UNABLE_TO_CONFIGURE_THE_SERIAL_PORT:
                            this.label4.Text = "Unable to configure the serial port";
                            break;
                        case UNABLE_TO_SET_THE_TIMEOUT_PARAMETERS:
                            this.label4.Text = "Unable to set the timeout parameters";
                            break;
                        case NO_RESPONSE_FROM_PRINTER:
                            this.label4.Text = "No response from printer";
                            break;
                        case TIMEOUT:
                            this.label4.Text = "TIMEOUT";
                            break;
                    }
                }
                catch
                {
                    MessageBox.Show(e.ToString());
                }
            }
            if (comboBox1.SelectedIndex == 1)  //WiFi
            {
                long val = 0;
                String sIP_ADDR = textBox2.Text.ToString();
                int sIP_PORT = Convert.ToInt32(textBox3.Text.ToString());
                
                unsafe
                {                   
                    {
                        val = ConnectWirelessPrinter(sIP_ADDR, sIP_PORT, 3000, false);
                    }
                }

                try
                {
                    switch (val)
                    {
                        case SUCCESS:
                            this.label4.Text = "Success";
                            break;
                        case SOCKET_ERROR:
                            this.label4.Text = "SOCKET_ERROR";
                            break;
                        case CONNECT_FAIL:
                            this.label4.Text = "CONNECT_FAIL";
                            break;
                        case ALREADY_OPENED:
                            this.label4.Text = "ALREADY_CONNECTED";
                            break;
                        case TIMEOUT:
                            this.label4.Text = "Unable to set the timeout parameters";
                            break;
                        case NO_RESPONSE_FROM_PRINTER:
                            this.label4.Text = "No response from printer";
                            break;
                    }
                }
                catch
                {
                    MessageBox.Show(e.ToString());
                }
            }
            if (comboBox1.SelectedIndex == 2)  //USB
            {
                long val = ConnectUSBPrinter(1000,false);

                try
                {
                    switch (val)
                    {
                        case SUCCESS:
                            this.label4.Text = "Success";
                            break;
                        case ALREADY_OPENED:
                            this.label4.Text = "Already opened";
                            break;
                        case UNABLE_TO_MAKE_CONNECTION:
                            this.label4.Text = "Unable to make a connection";
                            break;
                        case UNABLE_TO_GET_INFOMATION:
                            this.label4.Text = "Unable to get USB infomation";
                            break;
                        case NO_RESPONSE_FROM_PRINTER:
                            this.label4.Text = "No response from printer";
                            break;
                        case NOT_WOOSIM_PRINTER:
                            this.label4.Text = "Not \"WOOSIM\" printer";
                            break;
                        case TIMEOUT:
                            this.label4.Text = "TIMEOUT";
                            break;
                    }
                }
                catch
                {
                    MessageBox.Show(e.ToString());
                }
            }
            button1.Enabled = false;

        }

        //Disconnection & Initialization resourese in the Printer
        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            ClosePrinterConnection();
            label4.Text = "Connection closed";
        }

        //Exit this program
        private void button3_Click(object sender, EventArgs e)
        {
            button2_Click(sender, e);
            Application.Exit();
        }

        //Draw Lines & Boxes in Page mode
        private void button4_Click(object sender, EventArgs e)
        {
            //Initialization & make page
            InitPageMode(0, 0, (int)WIDTH._2INCH, (int)WIDTH._3INCH);

            //Draw Black & long box
            Page_DrawBox((int)WIDTH._2INCH - 80, 30, 30, 150, 20);
            Page_DrawBox((int)WIDTH._2INCH - 80, 200, 30, (int)WIDTH._3INCH - 200 - 30, 1);

            //Contained text "TEXT"
            Page_DrawBox(214, 224, 50, 258, 1);

            //Boxes in Box
            Page_DrawBox(60, 234, 104, 44, 1);
            Page_DrawBox(60, 312, 28, 150, 1);
            Page_DrawBox(98, 312, 28, 150, 1);
            Page_DrawBox(136, 312, 28, 150, 1);
            Page_DrawBox(50, 224, 124, 258, 3);

            //Draw vertical lines
            Page_DrawBox(205, 30, 0, 140, 1);
            Page_DrawBox(235, 30, 0, 140, 1);
            Page_DrawBox(265, 30, 0, 140, 1);

            //Draw horizontal lines
            Page_DrawBox(185, 55, 100, 0, 1);
            Page_DrawBox(185, 95, 100, 0, 1);
            Page_DrawBox(185, 135, 100, 0, 1);

            //Draw outline-Box
            Page_DrawBox(30, 0, (int)WIDTH._2INCH - 60, (int)WIDTH._3INCH, 1);

            Page_SetDirection(3);// Clockwise rotation
            Page_SetPosition(214, 30);//Y_pos, X_pos
            SetTextStyle(0, false, (int)TEXTWIDTH.TEXTWIDTH02, (int)TEXTHEIGHT.TEXTHEIGHT02, false);
            TextSaveSpool("Text");
            Page_SetPosition(270, 70);
            QRCodeSaveSpool(0, 'H', 2, "www.woosim.com/english");

            SetTextStyle(0, true, (int)TEXTWIDTH.TEXTWIDTH02, (int)TEXTHEIGHT.TEXTHEIGHT01, false);
            Page_SetPosition(126, 256); //Y_pos, X_pos
            TextSaveSpool("TEXT");

            Page_Print_StandardMode();

            Page_SetDirection(0);
            InitPageMode(0, 0, (int)WIDTH._2INCH, (int)WIDTH._2INCH);

            Page_DrawBox(0, 30, (int)WIDTH._2INCH, 0, 5);
            Page_DrawBox(30, 0, 0, (int)WIDTH._2INCH, 5);
            Page_DrawLine(30, 30, 170, 160, 3);
            Page_DrawLine(170, 160, 130, 190, 3);
            Page_DrawLine(130, 190, 210, 250, 3);
            Page_DrawLine(210, 250, 190, 270, 3);
            Page_DrawLine(190, 270, 270, 340, 3);
            Page_DrawLine(270, 340, 210, 370, 3);
            Page_Newline();
            Page_Print();

            Page_ClearCurrentData();
            Page_DrawBox(0, (int)WIDTH._1INCH, (int)WIDTH._2INCH, 0, 5);
            Page_DrawBox((int)WIDTH._1INCH, 0, 0, (int)WIDTH._2INCH, 5);
            Page_DrawBox((int)WIDTH._1INCH / 2, (int)WIDTH._1INCH, (int)WIDTH._1INCH, 0, 0);
            Page_DrawEllipse((int)WIDTH._1INCH, (int)WIDTH._1INCH, (int)WIDTH._1INCH / 2, (int)WIDTH._1INCH, 3);
            Page_Print_StandardMode();
            PrintSpool(true);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            InitPrinterStatus();
            SetCharCodeTable(255, 0);

            // TODO: Add your control notification handler code here
            TextSaveSpool("   매 출 전 표\r\n");
            TextSaveSpool("--------------------------------\r\n");

            //Company
            
            TextSaveSpool("상점명             Woosim Coffee\r\n");
            TextSaveSpool("사업자등록번호      119-81-36382\r\n");
            //Reg Number
            TextSaveSpool("대표자                  이 일 복\r\n");
            //Address
            TextSaveSpool("서울특별시 금천구 가산동 448번지\r\n대륭테크노타운 3차, 501호\r\n");
            //Tel
            TextSaveSpool("문의                02-2107-3709\r\n");
            TextSaveSpool("--------------------------------\r\n\r\n");
            TextSaveSpool("    우심 비자 카드\r\n");
            TextSaveSpool("--------------------------------\r\n");
            //Card number
            TextSaveSpool("카드번호     9445 4103 9533 2096\r\n");
            TextSaveSpool("유효 기간                2012/10\r\n");
            TextSaveSpool("--------------------------------\r\n");
            TextSaveSpool("거 래 일 자           2011/04/11\r\n");
            TextSaveSpool("거 래 종 류       [신용거래승인]\r\n");
            TextSaveSpool("단 말 번 호            123456789\r\n");
            TextSaveSpool("승 인 번 호            987654321\r\n");
            TextSaveSpool("결 제 금 액             50,000원\r\n");
            TextSaveSpool("--------------------------------\r\n");
            TextSaveSpool("서명: \r\n\r\n\r\n\r\n");
            TextSaveSpool("--------------------------------\r\n\r\n");

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

        private void button6_Click(object sender, EventArgs e)
        {
            string bmpPath = "./woosim.bmp";
            FileInfo _file = new FileInfo(bmpPath);

            if (_file.Exists)
            {
                InitPrinterStatus();
                SetCharCodeTable(0, 0);

                //Normal BMP file print
                TextSaveSpool("Print Bmp file \"woosim.bmp\""); PrintData();

                NormalBmpSaveSpool("./woosim.bmp");

                PrintLineFeed(2);

                //BMP file print using Compressed algorithm
                TextSaveSpool("Print Bmp file using compressed algorithm"); PrintData();
                CompressedBmpSaveSpool("./woosim.bmp");
                PrintLineFeed(2);

                //Print pre-download image.
                TextSaveSpool("Print pre-downloaded image file #0"); PrintData();
                LoadLogoSaveSpool(0); PrintData();

                PrintSpool(true);
            }
            else
                MessageBox.Show("Can`t find BMP file \"" + bmpPath.Substring(2) + "\"");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            InitPrinterStatus();
            TextSaveSpool("UPCA\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.UPCA, 2, 100, true, "012100000642"); PrintData();

            TextSaveSpool("UPCE\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.UPCE, 2, 100, true, "065100004328"); PrintData();

            TextSaveSpool("EAN13\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.EAN13, 2, 100, true, "123456478901"); PrintData();

            TextSaveSpool("EAN8\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.EAN8, 2, 100, true, "12345678"); PrintData();

            TextSaveSpool("CODE39\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.CODE39, 2, 100, true, "12345678901"); PrintData();

            TextSaveSpool("ITF\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.ITF, 2, 100, true, "123456"); PrintData();

            TextSaveSpool("CODABAR\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.CODABAR, 2, 100, true, "123456"); PrintData();

            TextSaveSpool("CODE93\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.CODE93, 2, 100, true, "12345678901"); PrintData();

            TextSaveSpool("CODE128\r\n");
            OneDimensionBarcodeSaveSpool((int)Barcode.CODE128, 2, 100, true, "12345678901"); PrintData();

            PrintSpool(true);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            InitPrinterStatus();

            TextSaveSpool("PDF417");
            PrintData();
            PDF417SaveSpool(2, 2, 2, 2, "0123456789", false);
            PrintData();

            TextSaveSpool("DataMatrix");
            PrintData();
            DataMatrixSaveSpool(36, 36, 2, "0123456789");
            PrintLineFeed(1);

            TextSaveSpool("QR Code");
            PrintData();
            QRCodeSaveSpool(0, 'H', 3, "0123456789");
            PrintDotFeed(30);

            TextSaveSpool("Micro PDF417");
            PrintData();
            MicroPDF417SaveSpool(2, 1, 0, 2, "01234567", false);
            PrintDotFeed(30);

            TextSaveSpool("Truncated PDF417");
            PrintData();
            TruncatedPDF417SaveSpool(2, 2, 2, 2, "0123456789", false);
            PrintData();

            TextSaveSpool("Maxicode");
            PrintData();
            MaxicodeSaveSpool(4, "0123456789");
            PrintData();

            PrintSpool(true);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            GetPrinterStatus(1000);
            //GetPrinterStatusEx(1000);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            GetPrinterModelName();
            GetFirmwareVersion();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            InitPageMode(0, 0, (int)WIDTH._2INCH, 700);
            SetTextStyle(0, true, (int)TEXTWIDTH.TEXTWIDTH02, (int)TEXTHEIGHT.TEXTHEIGHT02, false);
            TextSaveSpool("Woosim Cafe\r\n");

            SetTextStyle(0, false, (int)TEXTWIDTH.TEXTWIDTH01, (int)TEXTHEIGHT.TEXTHEIGHT01, false);
            TextSaveSpool("Coffee, tea and bread\r\n");
            Page_DotFeed(15);
            SetFontSize(1);
            Page_SetPosition(277, 105);
            TextSaveSpool("2015-01-27\r\n");
            SetFontSize(0);

            Page_SetPosition(290, 5);
            QRCodeSaveSpool(0, 'H', 2, "www.woosim.com/english");

            Page_DrawBox(0, 130, (int)WIDTH._2INCH, 0, 2);
            Page_DotFeed(5);
            TextSaveSpool(" #      Item      Num    Charge\r\n");
            TextSaveSpool(" 1      Latte      1     $ 3.35"); Page_Newline();
            TextSaveSpool(" 2  House Coffee   2     $ 3.98"); Page_Newline();
            TextSaveSpool(" 3       Tea	       1     $ 1.95"); Page_DotFeed(10);
            Page_DrawBox(0, 270, (int)WIDTH._2INCH, 0, 2); Page_Newline();

            Page_DrawBox(0, 310, 115, 0, 2); Page_DrawBox(260, 310, 124, 0, 2); Page_DotFeed(15);
            Page_SetPosition(123, 300);
            TextSaveSpool("Credit Card\r\n");

            TextSaveSpool("Number:         ");
            SetFontSize(1);
            TextSaveSpool("6688-9988-6644-3311"); Page_Newline();
            SetFontSize(0);
            TextSaveSpool("Payment:                $ 9.50 "); Page_Newline();

            Page_DrawBox(0, 400, 120, 0, 2); Page_DrawBox(250, 400, 134, 0, 2); Page_DotFeed(15);
            Page_SetPosition(145, 390);
            TextSaveSpool("Payment\r\n");

            TextSaveSpool("Price:                  $ 9.28"); Page_Newline();
            TextSaveSpool("Tax:                    $ 0.22"); Page_Newline();

            Page_DrawBox(0, 490, (int)WIDTH._2INCH, 0, 2); Page_DotFeed(15);
            TextSaveSpool("Total:                  $ 9.50"); Page_Newline();
            TextSaveSpool("Credit Card:            $ 9.50"); Page_Newline();

            Page_SetPosition(40, 580);
            OneDimensionBarcodeSaveSpool((int)Barcode.CODE128, 2, 50, true, "20150127123456789");
            Page_LineFeed(2);

            Page_Print_StandardMode();
            PrintSpool(true);
        }

        //Magnetic Card Read Mode Start
        private void button11_Click(object sender, EventArgs e)
        {
            int modeSelect = 0;
            
            //MSR sample is based 12Track magnetic card
            switch (comboBox4.SelectedIndex)
            {
                case 0:
                    modeSelect = 0;//Read 1T. If 23T card, read 2T.
                    break;

                case 1:
                    modeSelect = 1;//Read 2T. If 23T card, read 3T.
                    break;

                case 2:
                    modeSelect = 4;//Read 3T. It only use for 123T.
                    break;

                case 3:
                    modeSelect = 2;//Read 12T. If 23T card, read 23T.
                    break;

                case 4:
                    modeSelect = 3;//Read 123T. It only use for 123T.
                    break;
            }
            EnterMSRMode(modeSelect);
        }

        //Magnetic Card Read Mode Start
        private void button12_Click(object sender, EventArgs e)
        {
            CancelMSRMode();
        }

        //Message processing (like DefWindowProc in MFC)
        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == UWM_RECEIVE_MSG)
            {
                int chk;
                int length;

                chk = msg.WParam.ToInt32() >> 16;
                length = msg.WParam.ToInt32() & 0x0000FFFF;

                if (chk == STATUS_ANSWER)
                {
                    this.label4.Text = "STATUS_ANSWER";

                    string result;
                    unsafe
                    {
                        byte[] lParamByte = new byte[length];
                        Marshal.Copy(msg.LParam, lParamByte, 0, length);
                        result = string.Format("{0:X}", lParamByte[0]);
                    }
                    textBox1.Text = result;
                }
                else if (chk == DATA)
                {
                    this.label4.Text = "DATA";

                    string sScanData;

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

                    textBox1.Text = sScanData.ToString();

                }
            }

            base.WndProc(ref msg);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            SetFontForTTF("WSTTFL.ttf");
            PrintSpoolForTTF("Woosim System Inc.", 20, 30);
            PrintSpool(true);
        }
    }
}
