using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsCE.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_length, int B_length, 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 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 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("WoosimPrinter.DLL")]
        public static extern void SetPositionFromMark(int distance);

        /****************************************************************************
        CONST DEFINE
        ****************************************************************************/

        // 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; 

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

        //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;

        private static MsgWnd m_MsgWnd = null;

        public Form1()
        {
            InitializeComponent();
            m_MsgWnd = new MsgWnd();
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 3;
            comboBox4.SelectedIndex = 1;

            label4.Text = "";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                comboBox2.Show();
                label2.Show();
                comboBox3.Show();
                textBox2.Hide();
                textBox3.Hide();
                label3.Text = "Baud";
            }
            else
            {
                comboBox2.Hide();
                label2.Hide();
                comboBox3.Hide();
                textBox2.Show();
                textBox3.Show();
                label3.Text = "Port";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Initialization & make page
            InitPageMode(0, 0, _2INCH, _3INCH);

            //Draw Black & long box
            Page_DrawBox(_2INCH - 80, 30, 30, 150, 20);
            Page_DrawBox(_2INCH - 80, 200, 30, _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, _2INCH - 60, _3INCH, 1);

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

            SetTextStyle(0, true, 1, 0, false);
            Page_SetPosition(126, 256); //Y_pos, X_pos
            TextSaveSpool("TEXT");

            Page_Print_StandardMode();
            
            Page_SetDirection(0);
            InitPageMode(0, 0, _2INCH, _2INCH);

            Page_DrawBox(0, 30, _2INCH, 0, 5);
            Page_DrawBox(30, 0, 0, _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, _1INCH, _2INCH, 0, 5);
            Page_DrawBox(_1INCH, 0, 0, _2INCH, 5);
            Page_DrawBox(_1INCH / 2, _1INCH, _1INCH, 0, 0);
            Page_DrawEllipse(_1INCH, _1INCH, _1INCH / 2, _1INCH, 3);
            Page_Print_StandardMode();
            PrintSpool(true);
        }

        private void button2_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");

            PrintSpool(true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string bmpPath = "\\Program files\\CSharp\\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("\\Program files\\CSharp\\woosim.bmp");

                PrintLineFeed(2);

                //BMP file print using Compressed algorithm
                TextSaveSpool("Print Bmp file using compressed algorithm"); PrintData();
                CompressedBmpSaveSpool("\\Program files\\CSharp\\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 button4_Click(object sender, EventArgs e)
        {
            InitPrinterStatus();
            TextSaveSpool("UPCA\r\n");
            OneDimensionBarcodeSaveSpool(UPCA, 2, 100, true, "012100000642"); PrintData();

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

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

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

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

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

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

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

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

            PrintSpool(true);
        }

        private void button5_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 button6_Click(object sender, EventArgs e)
        {
            GetPrinterStatus(1000);
        }

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

        private void button12_Click(object sender, EventArgs e)
        {
            InitPageMode(0, 0, _2INCH, 700);
            SetTextStyle(0, true, 1, 1, false);
            TextSaveSpool("Woosim Cafe\r\n");

            SetTextStyle(0, false, 0, 0, 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, _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, _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, _2INCH, 0, 2); Page_DotFeed(15);
            TextSaveSpool("Total:                  $ 9.50"); Page_Newline();
            TextSaveSpool("Credit Card:            $ 9.50"); Page_Newline();

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

            Page_Print_StandardMode();
            PrintSpool(true);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int modeSelect = 0;
            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);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            CancelMSRMode();
        }

        private void button10_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 1:
                            this.label4.Text = "Success";
                            button10.Enabled = false;
                            break;
                        case -1:
                            this.label4.Text = "Already opened";
                            break;
                        case -2:
                            this.label4.Text = "Unable to open the port";
                            break;
                        case -3:
                            this.label4.Text = "Unable to configure the serial port";
                            break;
                        case -4:
                            this.label4.Text = "Unable to set the timeout parameters";
                            break;
                        case -5:
                            this.label4.Text = "No response from printer";
                            break;
                        case -7:
                            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 1:
                            this.label4.Text = "Success";
                            button10.Enabled = false;
                            break;
                        case -1:
                            this.label4.Text = "ALREADY_OPENED";
                            break;
                        case -2:
                            this.label4.Text = "SOCKET_ERROR";
                            break;
                        case -3:
                            this.label4.Text = "CONNECT_FAIL";
                            break;
                        case -5:
                            this.label4.Text = "No response from printer";
                            break;
                        case -7:
                            this.label4.Text = "TIMEOUT";
                            break;
                    }
                }
                catch
                {
                    MessageBox.Show(e.ToString());
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            button10.Enabled = true;
            ClosePrinterConnection();
            label4.Text = "Connection closed";
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            Close();
        }        
    }



    public class MsgWnd : MessageWindow
    {
        [DllImport("coredll.dll")]
        public static extern uint RegisterWindowMessage(string lpString);

        private uint WM_RECEIVE_MSG = RegisterWindowMessage("WOOSIM_PRT_OK");
       
        private const int DATA = 0x44; 
        private const int STATUS_ANSWER = 0x53; 

        public MsgWnd()
        {           
        }

        protected override void WndProc(ref Message msg)
        {
            int chk;
            int length;

            if (msg.Msg == WM_RECEIVE_MSG)
            {
                chk = msg.WParam.ToInt32() >> 16;
                length = msg.WParam.ToInt32() & 0x0000FFFF;

                if (chk == DATA)
                {
                    MessageBox.Show("DATA");

                    string sScanData;

                    unsafe
                    {
                        sbyte* pScanData = (sbyte*)msg.LParam.ToPointer();

                        sScanData = new string(pScanData);
                    }

                    MessageBox.Show(sScanData);

                }

                else if (chk == STATUS_ANSWER)
                {
                    MessageBox.Show("STATUS");

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

                        sScanData = String.Format("{0:X}", pScanData[0]);
                    }

                    MessageBox.Show(sScanData);
                }
            }
            base.WndProc(ref msg);
        }
    }

}