using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using ChuPiao.AutoPrint.Machine;
using ChuPiao.AutoPrint.Machine.Models;
using ChuPiao.Common.Models;
using System.Collections;

namespace ChuPiao.AutoPrint.Desktop.KeyBoard
{
    public partial class MachineKeyBoard : Form
    {
        private List<ILotMachine> _machineList = new List<ILotMachine>();

        public List<ILotMachine> MachineList
        {
            get { return _machineList; }
            set { _machineList = value; }
        }

        #region 快捷键处理程序
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        }


        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd, // handle to window 
            int id, // hot key identifier 
            KeyModifiers fsModifiers, // key-modifier options 
            Keys vk // virtual-key code 
            );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd, // handle to window 
            int id // hot key identifier 
            );



        protected override void WndProc(ref Message m)//监视Windows消息 
        {
            const int WM_HOTKEY = 0x0312; //如果m.Msg的值为0x0312那么表示用户按下了热键 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    ProcessHotkey(m);
                    break;
            }
            base.WndProc(ref m); //将系统消息传递自父类的WndProc 
        }


        private void ProcessHotkey(Message m)
        {
            IntPtr id = m.WParam;
            KJKey(((Keys)id).ToString().ToLower());
        }

        private void Form_RegisterKey()
        {
            RegisterHotKey(Handle, (int)Keys.F1, 0, Keys.F1);
            RegisterHotKey(Handle, (int)Keys.F2, 0, Keys.F2);
            RegisterHotKey(Handle, (int)Keys.F3, 0, Keys.F3);
            RegisterHotKey(Handle, (int)Keys.F4, 0, Keys.F4);
            RegisterHotKey(Handle, (int)Keys.F5, 0, Keys.F5);
            RegisterHotKey(Handle, (int)Keys.F6, 0, Keys.F6);
            RegisterHotKey(Handle, (int)Keys.F7, 0, Keys.F7);
            RegisterHotKey(Handle, (int)Keys.F8, 0, Keys.F8);
            RegisterHotKey(Handle, (int)Keys.F9, 0, Keys.F9);
            RegisterHotKey(Handle, (int)Keys.F10, 0, Keys.F10);
            RegisterHotKey(Handle, (int)Keys.F11, 0, Keys.F11);
            RegisterHotKey(Handle, (int)Keys.F12, 0, Keys.F12);

            RegisterHotKey(Handle, (int)Keys.D0, 0, Keys.D0);
            RegisterHotKey(Handle, (int)Keys.D1, 0, Keys.D1);
            RegisterHotKey(Handle, (int)Keys.D2, 0, Keys.D2);
            RegisterHotKey(Handle, (int)Keys.D3, 0, Keys.D3);
            RegisterHotKey(Handle, (int)Keys.D4, 0, Keys.D4);
            RegisterHotKey(Handle, (int)Keys.D5, 0, Keys.D5);
            RegisterHotKey(Handle, (int)Keys.D6, 0, Keys.D6);
            RegisterHotKey(Handle, (int)Keys.D7, 0, Keys.D7);
            RegisterHotKey(Handle, (int)Keys.D8, 0, Keys.D8);
            RegisterHotKey(Handle, (int)Keys.D9, 0, Keys.D9);


            RegisterHotKey(Handle, (int)Keys.NumPad0, 0, Keys.NumPad0);
            RegisterHotKey(Handle, (int)Keys.NumPad1, 0, Keys.NumPad1);
            RegisterHotKey(Handle, (int)Keys.NumPad2, 0, Keys.NumPad2);
            RegisterHotKey(Handle, (int)Keys.NumPad3, 0, Keys.NumPad3);
            RegisterHotKey(Handle, (int)Keys.NumPad4, 0, Keys.NumPad4);
            RegisterHotKey(Handle, (int)Keys.NumPad5, 0, Keys.NumPad5);
            RegisterHotKey(Handle, (int)Keys.NumPad6, 0, Keys.NumPad6);
            RegisterHotKey(Handle, (int)Keys.NumPad7, 0, Keys.NumPad7);
            RegisterHotKey(Handle, (int)Keys.NumPad8, 0, Keys.NumPad8);
            RegisterHotKey(Handle, (int)Keys.NumPad9, 0, Keys.NumPad9);

            RegisterHotKey(Handle, (int)Keys.A, 0, Keys.A);
            RegisterHotKey(Handle, (int)Keys.B, 0, Keys.B);
            RegisterHotKey(Handle, (int)Keys.C, 0, Keys.C);
            RegisterHotKey(Handle, (int)Keys.D, 0, Keys.D);
            RegisterHotKey(Handle, (int)Keys.E, 0, Keys.E);
            RegisterHotKey(Handle, (int)Keys.F, 0, Keys.F);
            RegisterHotKey(Handle, (int)Keys.G, 0, Keys.G);
            RegisterHotKey(Handle, (int)Keys.H, 0, Keys.H);
            RegisterHotKey(Handle, (int)Keys.I, 0, Keys.I);
            RegisterHotKey(Handle, (int)Keys.J, 0, Keys.J);
            RegisterHotKey(Handle, (int)Keys.K, 0, Keys.K);
            RegisterHotKey(Handle, (int)Keys.L, 0, Keys.L);
            RegisterHotKey(Handle, (int)Keys.M, 0, Keys.M);
            RegisterHotKey(Handle, (int)Keys.N, 0, Keys.N);
            RegisterHotKey(Handle, (int)Keys.O, 0, Keys.O);
            RegisterHotKey(Handle, (int)Keys.P, 0, Keys.P);
            RegisterHotKey(Handle, (int)Keys.Q, 0, Keys.Q);
            RegisterHotKey(Handle, (int)Keys.R, 0, Keys.R);
            RegisterHotKey(Handle, (int)Keys.S, 0, Keys.S);
            RegisterHotKey(Handle, (int)Keys.T, 0, Keys.T);
            RegisterHotKey(Handle, (int)Keys.U, 0, Keys.U);
            RegisterHotKey(Handle, (int)Keys.V, 0, Keys.V);
            RegisterHotKey(Handle, (int)Keys.W, 0, Keys.W);
            RegisterHotKey(Handle, (int)Keys.S, 0, Keys.X);
            RegisterHotKey(Handle, (int)Keys.Y, 0, Keys.Y);
            RegisterHotKey(Handle, (int)Keys.Z, 0, Keys.Z);


            RegisterHotKey(Handle, (int)Keys.Enter,0,Keys.Enter);
            RegisterHotKey(Handle, (int)Keys.Down, 0, Keys.Down);
            RegisterHotKey(Handle, (int)Keys.Up, 0, Keys.Up);
            RegisterHotKey(Handle, (int)Keys.Right, 0, Keys.Right);
            RegisterHotKey(Handle, (int)Keys.Left, 0, Keys.Left);
            RegisterHotKey(Handle, (int)Keys.Escape, 0, Keys.Escape);
            RegisterHotKey(Handle, (int)Keys.Delete, 0, Keys.Delete);

            RegisterHotKey(Handle, (int)Keys.End, 0, Keys.End);

            RegisterHotKey(Handle, (int)Keys.OemMinus, 0, Keys.OemMinus);
            RegisterHotKey(Handle, (int)Keys.Oemplus, 0, Keys.Oemplus);
            RegisterHotKey(Handle, (int)Keys.Back, 0, Keys.Back);
            RegisterHotKey(Handle, (int)Keys.Add, 0, Keys.Add);
            RegisterHotKey(Handle, (int)Keys.Subtract, 0, Keys.Subtract);
        }
        private void Form_UnRegisterKey()
        {
  
            UnregisterHotKey(Handle, (int)Keys.F1);
            UnregisterHotKey(Handle, (int)Keys.F2);
            UnregisterHotKey(Handle, (int)Keys.F3);
            UnregisterHotKey(Handle, (int)Keys.F4);
            UnregisterHotKey(Handle, (int)Keys.F5);
            UnregisterHotKey(Handle, (int)Keys.F6);
            UnregisterHotKey(Handle, (int)Keys.F7);
            UnregisterHotKey(Handle, (int)Keys.F8);
            UnregisterHotKey(Handle, (int)Keys.F9);
            UnregisterHotKey(Handle, (int)Keys.F10);
            UnregisterHotKey(Handle, (int)Keys.F11);
            UnregisterHotKey(Handle, (int)Keys.F12);

            UnregisterHotKey(Handle, (int)Keys.D0);
            UnregisterHotKey(Handle, (int)Keys.D1);
            UnregisterHotKey(Handle, (int)Keys.D2);
            UnregisterHotKey(Handle, (int)Keys.D3);
            UnregisterHotKey(Handle, (int)Keys.D4);
            UnregisterHotKey(Handle, (int)Keys.D5);
            UnregisterHotKey(Handle, (int)Keys.D6);
            UnregisterHotKey(Handle, (int)Keys.D7);
            UnregisterHotKey(Handle, (int)Keys.D8);
            UnregisterHotKey(Handle, (int)Keys.D9);

            UnregisterHotKey(Handle, (int)Keys.NumPad0);
            UnregisterHotKey(Handle, (int)Keys.NumPad1);
            UnregisterHotKey(Handle, (int)Keys.NumPad2);
            UnregisterHotKey(Handle, (int)Keys.NumPad3);
            UnregisterHotKey(Handle, (int)Keys.NumPad4);
            UnregisterHotKey(Handle, (int)Keys.NumPad5);
            UnregisterHotKey(Handle, (int)Keys.NumPad6);
            UnregisterHotKey(Handle, (int)Keys.NumPad7);
            UnregisterHotKey(Handle, (int)Keys.NumPad8);
            UnregisterHotKey(Handle, (int)Keys.NumPad9);

            UnregisterHotKey(Handle, (int)Keys.A);
            UnregisterHotKey(Handle, (int)Keys.B);
            UnregisterHotKey(Handle, (int)Keys.C);
            UnregisterHotKey(Handle, (int)Keys.D);
            UnregisterHotKey(Handle, (int)Keys.E);
            UnregisterHotKey(Handle, (int)Keys.F);
            UnregisterHotKey(Handle, (int)Keys.G);
            UnregisterHotKey(Handle, (int)Keys.H);
            UnregisterHotKey(Handle, (int)Keys.I);
            UnregisterHotKey(Handle, (int)Keys.J);
            UnregisterHotKey(Handle, (int)Keys.K);
            UnregisterHotKey(Handle, (int)Keys.L);
            UnregisterHotKey(Handle, (int)Keys.M);
            UnregisterHotKey(Handle, (int)Keys.N);
            UnregisterHotKey(Handle, (int)Keys.O);
            UnregisterHotKey(Handle, (int)Keys.P);
            UnregisterHotKey(Handle, (int)Keys.Q);
            UnregisterHotKey(Handle, (int)Keys.R);
            UnregisterHotKey(Handle, (int)Keys.S);
            UnregisterHotKey(Handle, (int)Keys.T);
            UnregisterHotKey(Handle, (int)Keys.U);
            UnregisterHotKey(Handle, (int)Keys.V);
            UnregisterHotKey(Handle, (int)Keys.W);
            UnregisterHotKey(Handle, (int)Keys.X);
            UnregisterHotKey(Handle, (int)Keys.Y);
            UnregisterHotKey(Handle, (int)Keys.Z);


            UnregisterHotKey(Handle, (int)Keys.Enter);
            UnregisterHotKey(Handle, (int)Keys.Down);
            UnregisterHotKey(Handle, (int)Keys.Up);
            UnregisterHotKey(Handle, (int)Keys.Right);
            UnregisterHotKey(Handle, (int)Keys.Left);
            UnregisterHotKey(Handle, (int)Keys.Escape);


            UnregisterHotKey(Handle, (int)Keys.Delete);

            UnregisterHotKey(Handle, (int)Keys.End); //清屏键

            UnregisterHotKey(Handle, (int)Keys.OemMinus);
            UnregisterHotKey(Handle, (int)Keys.Oemplus);
            UnregisterHotKey(Handle, (int)Keys.Back);


            UnregisterHotKey(Handle, (int)Keys.Add);
            UnregisterHotKey(Handle, (int)Keys.Subtract);

        }

        #endregion

        #region 按键的处理
        private void KJKey(string K)
        {
            //int keyspacetime = 300; 
            //List<KeycodeInfo> keycodeInfos = new List<KeycodeInfo>();

            string tmpstr = K;
            switch (K)
            {
                case "back":
                    tmpstr = "break";
                    break;
                case "escape":
                    tmpstr = "esc";
                    break;
                case "return":
                    tmpstr = "enter";
                    break;
                case "shiftkey":
                    tmpstr = "shift";
                    break;
                case "d0":
                case "numpad0":
                    tmpstr = "0";
                    break;
                case "numpad1":
                case "d1":
                    tmpstr = "1";
                    break;
                case "numpad2":
                case "d2":
                    tmpstr = "2";
                    break;
                case "d3":
                case "numpad3":
                    tmpstr = "3";
                    break;
                case "d4":
                case "numpad4":
                    tmpstr = "4";
                    break;
                case "d5":
                case "numpad5":
                    tmpstr = "5";
                    break;
                case "d6":
                case "numpad6":
                    tmpstr = "6";
                    break;
                case "d7":
                case "numpad7":
                    tmpstr = "7";
                    break;
                case "d8":
                case "numpad8":
                    tmpstr = "8";
                    break;
                case "d9":
                case "numpad9":
                    tmpstr = "9";
                    break;

                default:
                    break;
            }
            string datestr = DateTime.Now.ToString("MMdd");
            switch (tmpstr)
            {
                case "multiply":
                    ShowKeyToLED("shift");
                    DealKeyDown("shift");
                    ShowKeyToLED("8");
                    DealKeyDown("8");
                    ShowKeyToLED("unshift");
                    DealKeyDown("unshift");

                    break;
                //case "+":
                //    ShowKeyToLED("shift");
                //    DealKeyDown("shift");
                //    ShowKeyToLED("=");
                //    DealKeyDown("=");
                //    ShowKeyToLED("unshift");
                //    DealKeyDown("unshift");

                //    break;
                case "currentdatareport":
                    ShowKeyToLED("CurrentDataReport");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("1");
                    DealKeyDown("0");
                    Thread.Sleep(500);
                    DealKeyDown("enter");
                    DealKeyDown("1");
                    Thread.Sleep(500);
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;

                case "refurbish_money":
                    ShowKeyToLED("Refurbish_Money");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(500);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;

                case "paydatareport":
                    ShowKeyToLED("PayDataReport");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("9");
                    Thread.Sleep(200);
                    DealKeyDown("1");
                    Thread.Sleep(200);
                    DealKeyDown("enter");
                    Thread.Sleep(5000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;

                case "currentdatareport_forpl3":
                    ShowKeyToLED("CurrentDataReport_ForPL3");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("1");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentdatareport_for4c":
                    ShowKeyToLED("CurrentDataReport_For4C");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("3");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentdatareport_fordlt":
                    ShowKeyToLED("CurrentDataReport_ForDLT");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("5");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentdatareport_forpl5":
                    ShowKeyToLED("CurrentDataReport_ForPL5");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("7");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentdatareport_forqxc":
                    ShowKeyToLED("CurrentDataReport_ForQXC");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("6");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentdatareport_forzc":
                    ShowKeyToLED("CurrentDataReport_ForZC");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("2");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentdatareport_for6c":
                    ShowKeyToLED("CurrentDataReport_For6C");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("4");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentdatareport_forr9":
                    ShowKeyToLED("CurrentDataReport_ForR9");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("f3");
                    Thread.Sleep(500);
                    DealKeyDown("0");
                    DealKeyDown("7");
                    Thread.Sleep(1000);
                    DealKeyDown("f3");
                    DealKeyDown("0");
                    DealKeyDown("8");
                    DealKeyDown("enter");
                    Thread.Sleep(4000);
                    DealKeyDown("enter");
                    Thread.Sleep(1000);
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    break;
                case "currentmonthreport":
                    ShowKeyToLED("CurrentMonthReport");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("n");
                    Thread.Sleep(100);
                    DealKeyDown("0");
                    DealKeyDown("9");


                    string tmpSTR = DateTime.Now.ToString("MM01");
                    for (int j = 0; j <tmpSTR.Length; j++)
                    {
                        DealKeyDown(tmpSTR.Substring(j, 1));
                    }

                    for (int i = 0; i < 1; i++)
                    {
                        for (int j = 0; j < datestr.Length; j++)
                        {
                            DealKeyDown(datestr.Substring(j, 1));
                        }
                    }
                    DealKeyDown("enter");
                    DealKeyDown("5");
                    break;

                case "currentweekreport":
                    ShowKeyToLED("CurrentWeekReport");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("esc");
                    DealKeyDown("n");
                    Thread.Sleep(100);
                    DealKeyDown("0");
                    DealKeyDown("9");

                    //DayOfWeek WObj = ;
                    //WObj.

                    string tmpStartWeek = DateTime.Now.AddDays(1 - ((int)DateTime.Now.DayOfWeek)).ToString("MMdd");
                    for (int j = 0; j < tmpStartWeek.Length; j++)
                    {
                        DealKeyDown(tmpStartWeek.Substring(j, 1));
                    }


                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = 0; j < datestr.Length; j++)
                        {
                            DealKeyDown(datestr.Substring(j, 1));
                        }
                    }
                    DealKeyDown("enter");
                    DealKeyDown("5");
                    break;

                default:
                    ShowKeyToLED(tmpstr);
                    DealKeyDown(tmpstr);
                    break;
            }



        }

        private void ShowKeyToLED(string str)
        {
            richTextBox1.AppendText(str + "\r\n");
        }
        private void DealKeyDown(string key)
        {
            List<KeycodeInfo> keycodeInfos = new List<KeycodeInfo>();
            keycodeInfos.Add(new KeycodeInfo(key, 300));
            foreach (ILotMachine lotmachine in MachineList)
            {
                lotmachine.ClearBuffer();
                lotmachine.SendKeys(keycodeInfos);
            }
        }
        //private void DealKeyDown(List<KeycodeInfo> keycodeInfos)
        //{
        //    foreach (ILotMachine lotmachine in MachineList)
        //    {
        //        lotmachine.SendKeys(keycodeInfos);
        //    }
        //}

        //private void DealKeyDown(string str)
        //{
        //    List<string> TmpList = new List<string>();
        //    TmpList.Add(str);

        //    foreach (ILotMachine item in MachineList)
        //    {
        //        StaticComDrive.SendCpjKeyToCom(item.MachineId, TmpList);
        //    }
        //    TmpList = null;
        //}


        #endregion

        #region 标签变色的处理程序及OnClick
        private void Lable_MouseLeave(object sender, EventArgs e)
        {
            Color c = Color.FromArgb(255, 255, 255, 255);//Color.FromName("InactiveCaptionText"); //Color.FromArgb(255, 0, 0);
            Label l = (Label)sender;
            l.BackColor = c;
            l.BorderStyle = BorderStyle.Fixed3D;
        }
        private void Lable_MouseEnter(object sender, EventArgs e)
        {
            Color c = Color.FromArgb(204, 204, 204, 204);
            Label l = (Label)sender;
            l.BackColor = c;
            l.BorderStyle = BorderStyle.None;
        }

        private void Lable_Click(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            string showstr = l.Tag == null ? l.Text : l.Tag.ToString();
            KJKey(showstr.ToLower());
        }
        #endregion
        /// <summary>
        /// 以前的配置状态
        /// </summary>
        Dictionary<int, bool> Old_AllowPrint = new Dictionary<int, bool>(); 
        private void MainFrm_Load(object sender, EventArgs e)
        {
            //TODO
           // Form_RegisterKey();
            Old_AllowPrint.Clear();
            List<string> tmpList=new List<string>();
            foreach (ILotMachine item in _machineList)
            {
                tmpList.Add(item.MachineInfo.M_Number);
                //item.MachineInfo.M_AllowPaperout = true;

                item.ClearBuffer();
                //Old_AllowPrint.Add(item.MachineId, Base.MutiMachineConfigObj[item.MachineId].AllowPrint);
                //Base.MutiMachineConfigObj[item.MachineId].AllowPrint = true;

            }
            this.Text = string.Concat(string.Join(" ", tmpList.ToArray()), "彩票机键盘");
        }

        private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (ILotMachine item in MachineList)
            {
                //item.MachineInfo.M_AllowPaperout = false;
                //Base.MutiMachineConfigObj[item.MachineId].AllowPrint = Old_AllowPrint[item.MachineId];
            }
           
           // Form_UnRegisterKey();

        }

        public MachineKeyBoard(ILotMachine item)
        {
            _machineList = new List<ILotMachine>();
            _machineList.Add(item);
            InitializeComponent();
        }
        public MachineKeyBoard(List<ILotMachine> items)
        {
            _machineList = items;
            InitializeComponent();
        }
       

        private void MainFrm_KeyUp(object sender, KeyEventArgs e)
        {           
            KJKey(e.KeyCode.ToString().ToLower());              
        }


        private List<KeyValuePair<string, string>> strToList(string str)
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

            if (string.IsNullOrEmpty(str))
            {
                return list;
            }

            string[] arr = str.Split(new char[] { ',' });

            if (arr.Length % 2 != 0)
            {
                return list;
            }

            for (int i = 0; i < arr.Length; i += 2)
            {
                list.Add(new KeyValuePair<string, string>(arr[i], arr[i + 1]));
            }

            return list;
        }

        private Dictionary<string, int> strToDic(string str)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();

            if (string.IsNullOrEmpty(str))
            {
                return dic;
            }

            string[] arr = str.Split(new char[] { ',' });

            if (arr.Length % 2 != 0)
            {
                return dic;
            }

            for (int i = 0; i < arr.Length; i += 2)
            {
                dic.Add(arr[i], Int32.Parse(arr[i + 1]));
            }

            return dic;
        }

        private void label50_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<string, string>> list1 = strToList(this.textBox1.Text.Trim());
            Dictionary<string, int> dic1 = strToDic(this.textBox2.Text.Trim());

            List<KeycodeInfo> keycodeInfos = new List<KeycodeInfo>();
            foreach(KeyValuePair<string,string> pair in list1)
            {
                keycodeInfos.Add(new KeycodeInfo(pair.Key, dic1[pair.Value]));
            }

            foreach (ILotMachine lotmachine in MachineList)
            {
                lotmachine.ClearBuffer();
                lotmachine.SendKeys(keycodeInfos);
            }
        }  
    }
}