using System;
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;
using System.Net;

namespace TextDLL
{
    public partial class Form1 : Form
    {

        [DllImport("NMKPCL.dll")]   //打开端口
        public static extern int NM_OpenPort(int DevType);

        [DllImport("NMKPCL.dll")]
        public static extern int NM_OpenNet(UInt32 IP, int port);

        [DllImport("NMKPCL.dll")]
        public static extern int NM_SetLabelWidth(uint lwidth);

        [DllImport("NMKPCL.dll")]
        public static extern int NM_SetLabelHeight(uint lheight, uint gapH);


        [DllImport("NMKPCL.dll")]  // 打印 图片 不限制打印高度为 300 mm
        public static extern int NM_PrintBitmap(string strFile);

        [DllImport("NMKPCL.dll")]// 全幅打印 不限制打印高度 宽度对  104 mm  
        // public static extern int NM_PrintBitmapEx(string strFile);  int pageWidth,int pageHeight,int xOffset,int yOffset,char* pFileName
        public static extern int NM_PrintBitmapEx(uint pageWidth, uint pageHeight, uint xOffset, uint yOffset, string strFile);

        [DllImport("NMKPCL.dll")]
        public static extern int NM_SendCommand(byte[] aa);

        [DllImport("NMKPCL.dll")]
        public static extern int NM_ClosePort();

        [DllImport("NMKPCL.dll")]
        public static extern int NM_PrintLabel(uint number, uint cpnumber);


        [DllImport("NMKPCL.dll")]  // 
        public static extern int NM_OpenBlx(string strFile);

         [DllImport("NMKPCL.dll", EntryPoint = "NM_BlxPrint", CallingConvention = CallingConvention.Cdecl)]
        public static extern int NM_BlxPrint(int count);

         [DllImport("NMKPCL.dll", EntryPoint = "NM_BlxPrintEx", CallingConvention = CallingConvention.Cdecl)]
         public static extern int NM_BlxPrintEx(int count,int artcount);

         [DllImport("NMKPCL.dll")]  // 
         public static extern int NM_PrintBlx(string strFile,int count,  __arglist);


        [DllImport("NMKPCL.dll")]  // 
        public static extern int NM_UpdateText(string componentName,string content);

        [DllImport("NMKPCL.dll")]  // 
        public static extern int NM_UpdateCode(string componentName,string content);

        [DllImport("NMKPCL.dll")]  // 
        public static extern int NM_UpdatePicture(string  componentName,string buf ,int size  );




//        int _stdcall NM_UpdateText(char* componentName,char* content)
//{
//        PCLInstance::GetInstance()->UpdateText(componentName,content);
//    return 0;
//}
//int _stdcall NM_UpdateCode(char* componentName,char* content)
//{
//        PCLInstance::GetInstance()->UpdateCode(componentName,content);

//    return 0;
//}
//int _stdcall NM_UpdatePicture(char* componentName,char* buf,int size)
//{
//            PCLInstance::GetInstance()->UpdatePicture(componentName,buf,size);

//    return 0;


        public Form1()
        {
            InitializeComponent();
        }

        //public int Send(int step)
        //{
        //    int aa = NM_OpenPort(255);
        //    string cmd = "SET CUTTER OFF\r\n";  //切刀关

        //    switch (step)
        //    {
        //        case 1:
        //            aa = NM_OpenPort(255);
        //            break;

        //        case 3:
        //            aa = NM_SetLabelWidth(104 * 8);   // 100 mm 
        //            break;

        //        case 31:
        //            aa = NM_SetLabelWidth(104 * 8);   // 100 mm 
        //            break;
        //        case 4:
        //            aa = NM_SetLabelHeight(150 * 8, 0);  //150mm
        //            break;
        //        case 5:
        //           // string aaa = txtFilePath.Text;   //"d:\\111.bmp";
        //           // aa = NM_PrintBitmap(aaa);
        //            break;

        //        case 7:
        //            aa = NM_ClosePort();
        //            break;

        //        case 12:
        //            cmd = "SET CUTTER OFF\r\n";  //切刀关
        //            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(cmd);

        //            aa = NM_SendCommand(byteArray);
        //            break;

        //        case 16:
        //            cmd = "SET CUTTER ON\r\n";   //切刀开
        //            byte[] byteArray1 = System.Text.Encoding.ASCII.GetBytes(cmd);
        //            aa = NM_SendCommand(byteArray1);
        //            break;


        //    }
        //    return aa;
        //}

        private void btn1_Click(object sender, EventArgs e)
        {
            int port = 0;

            if (int.TryParse(txt1prot.Text , out port) == false)
            {
                MessageBox.Show("端口数据应当为整型数据");
                return;
            }
            int  aa = NM_OpenPort(255);
            txtret1.Text = aa.ToString();
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            //2、NM_OpenNet

            string ipStr = txt2IP.Text;
            IPAddress ip;
            if (IPAddress.TryParse(ipStr, out ip) ==false )
            {
                MessageBox.Show("非法IP");
                return;
            }
            int port = 0;

            if (int.TryParse(txt2prot.Text, out port) == false)
            {
                MessageBox.Show("端口数据应当为整型数据");
                return;
            }
            string[] str = ipStr.Split('.');

            UInt32 uiIp = 0;
            UInt32 ll = 0;

            if (uint.TryParse(str[0] ,out ll) == false) return;
            uiIp = ll << 24;

            if (uint.TryParse(str[1], out ll) == false) return;
            ll=ll << 16;
            uiIp = uiIp +ll;

            if (uint.TryParse(str[2], out ll) == false) return;
            ll = ll << 8;
            uiIp =uiIp + ll;

            if (uint.TryParse(str[3], out ll) == false) return;
            uiIp  = uiIp +ll;

            int aa = NM_OpenNet(uiIp, port);   // 100 mm 

            txtret2.Text = aa.ToString();

        }

        private void btn3_Click(object sender, EventArgs e)
        {
            int  aa = NM_ClosePort();
            txtret3.Text = aa.ToString();
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            uint uW = 0;
            txtret5.Text = "";
            if (uint.TryParse(txt5W.Text, out uW) == false) return;
            int aa = NM_SetLabelWidth(uW);   //  104 * 8  100 mm 

            txtret5.Text = aa.ToString();
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            uint uH = 0;
            uint Ug = 0;
            txtret4.Text = "";
            if (uint.TryParse(txt8H.Text, out uH) == false) return;
            if (uint.TryParse(txt8G.Text, out Ug) == false) return;

            int aa = NM_SetLabelHeight(uH, Ug);  //150mm*8
            txtret4.Text = aa.ToString();
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            txtret6.Text = "";
            string cmd = txt6cmd.Text + "\r\n";  // "SET CUTTER ON\r\n";   //切刀开
            byte[] byteArray1 = System.Text.Encoding.ASCII.GetBytes(cmd);
            int aa = NM_SendCommand(byteArray1);
            txtret6.Text = aa.ToString();
        }

        private void btn7_Click(object sender, EventArgs e)
        {

            uint uNn = 0;
            uint UCN = 0;
            txtret7.Text = "";
            if (uint.TryParse(txt7count.Text, out uNn) == false) return;
            if (uint.TryParse(txt7copy.Text, out UCN) == false) return;

            int aa = NM_PrintLabel(uNn, UCN);  //150mm*8
            txtret7.Text = aa.ToString();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //txt8file.Text = Application.StartupPath + @"\456.BLX";
            //txt10file.Text = Application.StartupPath + @"\999.BLX";
        }

        private void btn8_Click(object sender, EventArgs e)
        {
               txtret8.Text = "";
              string aaa = txt8file.Text;   //"d:\\111.bmp";
              int  aa = NM_OpenBlx(aaa);
              txtret8.Text = aa.ToString();

        }

        private void btn9_Click(object sender, EventArgs e)
        {
              txtret9.Text = "";
             string str =txt9list.Text.Trim();
            string[] strl = str.Split(',');
            int aa = NM_BlxPrint(0);
             txtret9.Text = aa.ToString();
        }

        private void btn10_Click(object sender, EventArgs e)
        {
            //txtret10.Text = "";
            //string aaa = txt10file.Text;   //"d:\\111.bmp";
            //string str = txt9list.Text.Trim();
            //string[] strl = str.Split(',');
            //int aa = NM_PrintBlx(aaa, strl.Length, __arglist(strl[0], strl[1]));
            //txtret10.Text = aa.ToString();
        }

        private void btn30_Click(object sender, EventArgs e)
        {
            
            txtret30.Text = "";
            string lsold = txtold30.Text.Trim();
            string lsnew = txtnew30.Text.Trim();

            int aa = NM_UpdateText(lsold, lsnew);  
            txtret30.Text = aa.ToString();
        }

        private void btn31_Click(object sender, EventArgs e)
        {
            txtret31.Text = "";
            string lsold = txtold31.Text.Trim();
            string lsnew = txtnew31.Text.Trim();

            int aa = NM_UpdateText(lsold, lsnew);
            txtret31.Text = aa.ToString();
        }

        private void btn40_Click(object sender, EventArgs e)
        {
            txtret40.Text = "";
            string lsold = txtold40.Text.Trim();
            string lsnew = txtnew40.Text.Trim();

            int aa = NM_UpdateCode(lsold, lsnew);
            txtret40.Text = aa.ToString();
        }

        private void btn41_Click(object sender, EventArgs e)
        {
            txtret41.Text = "";
            string lsold = txtold41.Text.Trim();
            string lsnew = txtnew41.Text.Trim();

            int aa = NM_UpdateCode(lsold, lsnew);
            txtret41.Text = aa.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           int lcount = 1;

            if (int.TryParse(textBox4.Text,out lcount) ==false ) { lcount = 1; }
            
            int aa = NM_BlxPrintEx(lcount,0);
             txtret9.Text = aa.ToString();

            
        }

    }
}
