wince 盘点机c# 源码

using System;

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

namespace ScanDemo
{   
    public partial class Form1 : Form
    {
        Scanner scanner = new Scanner();




        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
 
            this.KeyPreview = true;

            scanner.DecodeEvent += new EventHandler<DecodeEventArgs>(scanner_DecodeEvent);
            scanner.Start();

            try
            {

                Banci.DataSource = getBanciSelect();
                Banci.ValueMember = "Banci";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        void scanner_DecodeEvent(object sender, DecodeEventArgs e)
        {
            Win32.sndPlaySound(Properties.Resources.Scan, Win32.SND_ASYNC | Win32.SND_MEMORY);

            this.BeginInvoke((Action<string>)delegate(string barcode)
            {
                if (tiaomahao.Focused)
                {
                    tiaomahao.Text = barcode;
                    return;
                }
                if (Geshu.Focused)
                {
                    Geshu.Text = barcode;
                    return;
                }
                if (Bangshu.Focused)
                {
                    Bangshu.Text = barcode;
                    return;
                }
                if (Banci.Focused)
                {
                    Banci.Text = barcode;
                    return;
                }
                if (Jihao.Focused)
                {
                    Jihao.Text = barcode;
                    return;
                }
                if (Gongxu.Focused)
                {
                    Gongxu.Text = barcode;
                    return;
                }
                if (Renyuanbianma.Focused)
                {
                    Renyuanbianma.Text = barcode;
                    return;
                }
                if (Renyuanmingcheng.Focused)
                {
                    Renyuanmingcheng.Text = barcode;
                    return;
                }


            }, e.Barcode);
        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            scanner.Stop();
        }


        //private void timer1_Tick(object sender, EventArgs e)
        //{
        //    //10秒没扫到东西则关闭扫描头
        //    if (scanner.IsContinuousMode && (++scanCount >= 10))
        //    {
        //        if (scanner.SwitchTriggerMode())
        //        {
        //            scanCount = 0;
        //            btnSwitch.Text = "Auto Mode";
        //            timer1.Enabled = false;
        //        }
        //    }
        //}
             
      
        //private void btnSwitch_Click(object sender, EventArgs e)
        //{
        //    if (scanner.SwitchTriggerMode())
        //    {
        //        if (scanner.IsContinuousMode)
        //        {
        //            btnSwitch.Text = "Normal Mode";
        //            timer1.Enabled = true;
        //        }
        //        else
        //        {

        //            btnSwitch.Text = "Auto Mode";
        //            timer1.Enabled = false;
        //        }

        //    }
        //}

        private void btnbSave_Click(object sender, EventArgs e)
        {
            savedata();
            this.tiaomahao.Text = "";
            this.Bangshu.Text = "";
            this.Geshu.Text = "";
            this.tiaomahao.Focus();

          //  MessageBox.Show("保存成功");


        }

        private void tiaomahao_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    if (getdata(this.tiaomahao.Text ))
                        Geshu.Focus();
                    else
                    {
                        tiaomahao.Focus();
                        MessageBox.Show("没查到");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            ConnSetFm f = new ConnSetFm();
            f.ShowDialog();
        }

        //----------------以下为sql操作-----------------------------------------

        /// <summary>
        /// 返回资料--用下拉框选择
        /// </summary>
        /// <returns></returns>
        public DataTable getBanciSelect()
        {
            string sql;
            sql = "SELECT Banci FROM dbo.ClassPara";

            return SqlHelper.GetDataSetSql(sql);
        }

        //按条码查资料
        public bool getdata(string tiaomahao)
        {
            try
            {
                SqlParameter[] parameters =
                { 
                    new SqlParameter("@tiaomahao", tiaomahao)
                };
                DataTable dt = SqlHelper.GetDataSetPro("p_getdata", parameters);

                if (dt.Rows.Count > 0)
                {

                      Geshu.Text=dt.Rows[0][0].ToString();
                      Bangshu.Text  = dt.Rows[0][1].ToString(); 
                      return true;
                }
                else
                {
                     return false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("查询失败:" + e.Message);
                return false;
            }
        }
        /// <summary>
        /// 保存资料
        /// </summary>
        /// <returns></returns>
        public void savedata()
        {
            try
            {
                string sql;

                sql = "p_savedata";
                SqlParameter[] para = new SqlParameter[]
                {
                    new SqlParameter("@tiaomahao", this.tiaomahao.Text),
                    new SqlParameter("@Banci",this.Banci.Text),
                    new SqlParameter("@Jihao", this.Jihao.Text),
                    new SqlParameter("@Gongxu", this.Gongxu.Text),
                    new SqlParameter("@Renyuanbianma", this.Renyuanbianma.Text),
                    new SqlParameter("@Renyuanmingcheng", this.Renyuanmingcheng.Text),
                    new SqlParameter("@Geshu", this.Geshu.Text),
                    new SqlParameter("@Bangshu", this.Bangshu.Text)
                };

                SqlHelper.ExecutePro(sql, para);

            }
             catch (Exception e)
             {
                 MessageBox.Show("保存失败:"+e.Message);
             }
        }

        private void Geshu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Bangshu.Focus();
            }
        }

        private void Bangshu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Banci.Focus();
            }
        }

        private void Banci_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Jihao.Focus();
            }
        }

        private void Jihao_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Gongxu.Focus();
            }
        }

        private void Gongxu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Renyuanbianma.Focus();
            }
        }

        private void Renyuanbianma_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.Renyuanmingcheng.Focus();
            }
        }

        private void Renyuanmingcheng_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnbSave.Focus();
            }
        }
    }
}

//-------------------
using System;

using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ScanDemo
{
    public class DecodeEventArgs : EventArgs
    {
        private string barcode;
        private byte type;

        public DecodeEventArgs(string barcodeData, byte typeData)
        {
            barcode = barcodeData;
            type = typeData;
        }

        public string Barcode
        {
            get { return barcode; }
            set { barcode = value; }
        }

        public byte Type
        {
            get { return type; }
            set { type = value; }
        }

    }

    class Scanner
    {
        public event EventHandler<DecodeEventArgs> DecodeEvent;

        private bool needClose = false;

        private IntPtr[] hEvent = new IntPtr[2];
        private IntPtr hNotify = IntPtr.Zero;

        private Thread scanThread;

        private bool isContinuousMode;
        public bool IsContinuousMode
        {
            get { return isContinuousMode; }
            set { isContinuousMode = value; }
        }

        public bool Start()
        {

            if (Device.SCA_GetPowerStatus() == 0)
            {
                Device.SCA_EnableModule();
                needClose = true;
            }

            Win32.MSGQUEUEOPTIONS options = new Win32.MSGQUEUEOPTIONS();
            options.dwSize = 20;
            options.dwFlags = 2;
            options.dwMaxMessages = 3;
            options.cbMaxMessage = 252;
            options.bReadAccess = true;

            IntPtr hMsgQ = Win32.CreateMsgQueue(null, options);
            if (hMsgQ == IntPtr.Zero)
            {
                return false;
            }

            hNotify = Device.SCA_RegisterNotification(hMsgQ);
            if (hNotify == IntPtr.Zero)
            {
                return false;
            }

            hEvent[0] = Win32.CreateEvent(IntPtr.Zero, false, false, null);
            hEvent[1] = hMsgQ;

            scanThread = new Thread(new ThreadStart(this.ScanThreadPorc));
            scanThread.Start();

            return true;

        }

        public void Stop()
        {
            try
            {
                Win32.EventModify(hEvent[0], Win32.EVENT_SET);
                if (!scanThread.Join(3000))
                {
                    scanThread.Abort();
                }
            }
            catch (Exception ex)
            {
            }
            Win32.CloseHandle(hEvent[0]);

            Device.SCA_UnRegisterNotification(hNotify);
            Win32.CloseHandle(hEvent[1]);

            if (needClose)
            {
                Device.SCA_DisableModule();
            }
            else  if (isContinuousMode)
            {
                Device.SCA_SetTriggerMode(Device.TriggerMode.Normal);
            }

        }

        public bool SwitchTriggerMode()
        {
            if (isContinuousMode)
            {
                if (Device.SCA_SetTriggerMode(Device.TriggerMode.Normal))
                {
                    isContinuousMode = false;
                    return true;
                }
            }
            else
            {
                if (Device.SCA_SetTriggerMode(Device.TriggerMode.Continuous))
                {
                    isContinuousMode = true;
                    return true;
                }

            }

            return false;
        }


        private void ScanThreadPorc()
        {
            while (true)
            {
                uint evt = Win32.WaitForMultipleObjects(2, hEvent, false, Win32.INFINITE);
                switch (evt)
                {
                    case 0://return thread
                        return;
                    case 1://disable network 
                        uint bytesRead;
                        uint flags;

                        byte[] buf = new byte[256];

                        if (Win32.ReadMsgQueue(hEvent[1], buf, 256, out bytesRead, Win32.INFINITE, out flags))
                        {
                             EventHandler<DecodeEventArgs> temp = DecodeEvent;
                             if (temp != null)
                             {
                                 temp(this, new DecodeEventArgs(Encoding.Default.GetString(buf, 2, (int)bytesRead), buf[1]));
                             }
                        }
                        break;
                }
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nylgwsf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值