本代码在欣技9300PDA手持机实现的:
1.初始化扫码服务
Reader.ReaderEngineAPI.InitReader();
if (Global.MsgWindow == null)
Global.MsgWindow = new MsgWindow();
2.在需要扫码的页面添加扫码事件
Global.MsgWindow.OnReadeBarCode += new EventHandler<MyEventArg>(MsgWindow_OnReadeBarCode); //扫描事件(窗体加载事件中)
this.Closing += new CancelEventHandler(SmokeOrderDesc_Form_Closing); //当前窗体释放扫描(窗体加载事件中)
#region
扫码事件 private void MsgWindow_OnReadeBarCode(object sender, MyEventArg e) { 。。。
}
#endregion
3.离开扫码的页面需要解除扫码事件
Global.MsgWindow.OnReadeBarCode -= new EventHandler<MyEventArg>(MsgWindow_OnReadeBarCode);
注意:需要用到2个dll类库ReaderDll_CE.dll和Reader_Ce_Net.DLL,创建两个公共类GLobal和MsgWindow。
Global.cs文件代码如下:
public class Global
{
#region 全局变量
static public MsgWindow MsgWindow = null;
#endregion
}
MsgWindow.cs文件代码如下:
public class Win32API
{
#region 调用非托管代码
[DllImport("coredll.dll", SetLastError = true)]
public static extern uint RegisterWindowMessage(string lpstring);
#endregion
}
public class MsgWindow : MessageWindow
{
#region 属性
public static readonly string WM_DECODEDATA = "WM_DECODEDATA";
public UInt32 decodeMsg = 0;
int bl = 0;
public string CodeDesc = string.Empty; public event EventHandler<MyEventArg> OnReadeBarCode;
#endregion
#region 构造方法
public MsgWindow()
{
decodeMsg = Win32API.RegisterWindowMessage(WM_DECODEDATA);
}
#endregion
#region 扫码代码
protected override void WndProc(ref Message m)
{
if (m.Msg == decodeMsg)
{
switch (m.WParam.ToInt32())
{
case Reader.ReaderEngineAPI.DC_READER_BC:
bl = Reader.ReaderEngineAPI.GetDecodeData(ref CodeDesc);
MyEventArg arg = new MyEventArg();
arg.BL = bl; arg.CodeDesc = CodeDesc;
if (OnReadeBarCode != null)
{
OnReadeBarCode(null, arg);
}
break;
default: break;
}
}
base.WndProc(ref m);
}
#endregion
}
public class MyEventArg : EventArgs
{
#region 属性
/// <summary>
/// 扫描返回值
/// </summary>
public int BL;
/// <summary>
/// 条码结果
/// </summary>
public string CodeDesc;
#endregion
}