// EvcSymbolTest.cpp : Defines the entry point for the application.
//
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "stdafx.h"
#include "resource.h"
#include <windows.h>
#include <windowsx.h>
#include <ScanCAPI.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#define countof(x) sizeof(x)/sizeof(x[0])
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BOOL CALLBACK MAINProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK EditProc(HWND,UINT,WPARAM,LPARAM);
BOOL Save(HWND hwnd_barcode,HWND hwnd_qty);
void InitFormControl(HWND);
WNDPROC PreEditProc[2];
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
HINSTANCE hInst = NULL;
HANDLE hScanner = NULL;
LPSCAN_BUFFER lpScanBuffer = NULL;
TCHAR szScannerName[MAX_PATH] = TEXT("SCN1:"); // default scanner name
DWORD dwScanSize = 7095; // default scan buffer size
DWORD dwScanTimeout = 0; // default timeout value (0 means no timeout)
BOOL bUseText = TRUE;
BOOL bTriggerFlag = FALSE;
BOOL bRequestPending = FALSE;
BOOL bStopScanning = FALSE;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
enum tagUSERMSGS
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
UM_SCAN = WM_USER + 0x200,
UM_STARTSCANNING,
UM_STOPSCANNING
};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int nResult;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
hInst = hInstance;
nResult = DialogBoxParamW(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MAINProc,0L);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return nResult;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BOOL CALLBACK MAINProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
DWORD dwResult;
LPSCAN_BUFFER lpScanBuf;
static HWND hctl_temp,hctl_barcode, hctl_qty, hctl_status,hctl_info;
switch(uMsg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case WM_INITDIALOG:
PostMessage(hwnd,UM_STARTSCANNING,0,0L);
hctl_barcode=GetDlgItem(hwnd,IDC_EDIT_Barcode);
hctl_qty=GetDlgItem(hwnd,IDC_EDIT_Qty);
hctl_status=GetDlgItem(hwnd,IDC_STATIC_STATUS);
hctl_info=GetDlgItem(hwnd,IDC_STATIC_INFO);
InitFormControl(hwnd);
//子类化
PreEditProc[0]=(WNDPROC) SetWindowLong (hctl_barcode,
GWL_WNDPROC, (LONG) EditProc) ;
PreEditProc[1]=(WNDPROC) SetWindowLong (hctl_qty,
GWL_WNDPROC, (LONG) EditProc) ;
break;
case UM_STARTSCANNING:
dwResult=SCAN_Open(szScannerName,&hScanner);
if(dwResult!=E_SCN_SUCCESS)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(hwnd,TEXT("不能打开激光器"),TEXT("提示"),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
break;
}
dwResult=SCAN_Enable(hScanner);
if(dwResult!=E_SCN_SUCCESS)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(hwnd,TEXT("激光器不可用"),TEXT("提示"),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
break;
}
lpScanBuffer = SCAN_AllocateBuffer(bUseText, dwScanSize);
if (lpScanBuffer==NULL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(hwnd,TEXT("未能分配扫描缓冲"),TEXT("提示"),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
return TRUE;
}
//SCAN_ReadLabelMsg扫描成功后向hwnd发送UM_SCAN的消息
dwResult=SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
UM_SCAN,
dwScanTimeout,
NULL);
if(dwResult!=E_SCN_SUCCESS)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(hwnd,TEXT("SCAN_ReadLabelMsg发生错误"),TEXT("提示"),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//只有到最后成功得到缓存后才置此标志为TRUE
bRequestPending=TRUE;
}
break;
return true;
case UM_STOPSCANNING:
//在未停止扫描且未已正常得到缓存后才执行SCAN_Flush
if (!bStopScanning && bRequestPending)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SCAN_Flush(hScanner);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!bRequestPending)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SCAN_Disable(hScanner);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (lpScanBuffer)
SCAN_DeallocateBuffer(lpScanBuffer);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SCAN_Close(hScanner);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EndDialog(hwnd, 0);
}
bStopScanning = TRUE;
break;
case WM_ACTIVATE:
switch(LOWORD(wParam))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case WA_INACTIVE:
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (bRequestPending)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dwResult=SCAN_Flush(hScanner);
}
break;
default:
if (!bRequestPending && lpScanBuffer != NULL && !bStopScanning)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dwResult=SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
UM_SCAN,
dwScanTimeout,
NULL);
if (dwResult!=E_SCN_SUCCESS)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(hwnd,TEXT("SCAN_ReadLabelMsg发生错误"),TEXT("提示"),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
}
}
break;
}
break;
case UM_SCAN:
bRequestPending=FALSE;
lpScanBuf=(LPSCAN_BUFFER)lParam;
if(lpScanBuf==NULL)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(hwnd,TEXT("未能正确得到数据"),TEXT("提示"),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
}
switch(SCNBUF_GETSTAT(lpScanBuf))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case E_SCN_SUCCESS:
hctl_temp=GetFocus();
if(hctl_temp==hctl_barcode ||hctl_temp==hctl_qty)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Edit_SetText(hctl_temp,(LPTSTR)SCNBUF_GETDATA(lpScanBuffer));
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Edit_SetText(hctl_barcode,(LPTSTR)SCNBUF_GETDATA(lpScanBuffer));
SendMessage(hwnd,WM_KEYDOWN,VK_RETURN,NULL);
}
break;
}
if (GetFocus())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dwResult = SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
uMsg,
dwScanTimeout,
NULL);
if ( dwResult != E_SCN_SUCCESS )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(hwnd,TEXT("SCAN_ReadLabelMsg未能正确得到数据"),TEXT("提示"),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
}
else
bRequestPending = TRUE;
}
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case IDCANCEL:
bRequestPending=TRUE;
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
bRequestPending=FALSE;
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
break;
}
return TRUE;
case WM_KEYDOWN:
switch (wParam)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case VK_RETURN:
hctl_temp=GetFocus();
if (hctl_temp==hctl_barcode)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetFocus(hctl_qty);
SendMessage(hctl_qty,EM_SETSEL,0,-1);
}
else if (hctl_temp==hctl_qty)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Save(hctl_barcode,hctl_qty)==TRUE)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetWindowText(hctl_info,TEXT("追加数据成功!"));
InitFormControl(hwnd);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetWindowText(hctl_info,TEXT("追加数据失败!"));
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetFocus(hctl_barcode);
}
break;
case VK_F1:
InitFormControl(hwnd);
break;
case VK_F2:
hctl_temp=GetFocus();
if (hctl_temp==hctl_qty)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Edit_SetText(hctl_temp,TEXT("-"));
Edit_SetSel(hctl_temp,1,1);
}
break;
case VK_F3:
break;
}
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return FALSE;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//初始化
void InitFormControl(HWND hwnd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
HWND hctl_barcode, hctl_qty;
hctl_barcode=GetDlgItem(hwnd,IDC_EDIT_Barcode);
Edit_SetText(hctl_barcode,TEXT(""));
hctl_qty=GetDlgItem(hwnd,IDC_EDIT_Qty);
Edit_SetText(hctl_qty,TEXT("1"));
SetFocus(hctl_barcode);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//子类化文本框
LRESULT CALLBACK EditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int id = GetWindowLong (hwnd, GWL_ID) ;
LPMSG msg;
WNDPROC proc;
switch (uMsg)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case WM_GETDLGCODE :
msg=(LPMSG) lParam;
switch(msg->message)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case WM_KEYDOWN:
switch(msg->wParam)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case VK_TAB:
case VK_RETURN:
case VK_F1:
case VK_F2:
case VK_F3:
SendMessage(GetParent(hwnd),msg->message,msg->wParam,msg->lParam);
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
switch(msg->message)
{
case EN_SETFOCUS:
hctl_qty=GetDlgItem(GetParent(hwnd),IDC_EDIT_Qty);
if (msg->hwnd==hctl_qty)
{
SetDlgItemInt(GetParent(hwnd),IDC_STATIC_STATUS,msg->message,FALSE);
//Edit_SetSel(hwnd,-1,GetWindowTextLength(hwnd));
Edit_SetSel(hwnd,0,-1);
//SNDMSG(hwnd, EM_SETSEL, 0, GetWindowTextLength(hwnd));
}
}
*/
}
if (id==IDC_EDIT_Barcode)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
proc=PreEditProc[0];
}
else if (id==IDC_EDIT_Qty)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
proc=PreEditProc[1];
}
return CallWindowProc (proc, hwnd, uMsg, wParam, lParam) ;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//保存
BOOL Save(HWND hwnd_barcode,HWND hwnd_qty)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int result=FALSE;
FILE * file_handling=NULL;
TCHAR barcode[255],qty[255];
TCHAR buffer[255];
char ansiBuffer[255];
GetWindowText(hwnd_barcode,buffer,sizeof(buffer));
if (!lstrlen (buffer))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(NULL,TEXT("请扫描或输入条码!"),TEXT("提示"),MB_OK);
SetFocus(hwnd_barcode);
return result;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lstrcpy (barcode,buffer);
}
memset(buffer,0,sizeof(buffer));
GetWindowText(hwnd_qty,buffer,sizeof(buffer));
if (!lstrlen (buffer))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(NULL,TEXT("请输入数量!"),TEXT("提示"),MB_OK);
SetFocus(hwnd_qty);
return result;
}
else //判断数字
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
lstrcpy (qty,buffer);
memset(buffer,0,sizeof(buffer));
swprintf(buffer,TEXT("%s,%s\n"),barcode,qty);
if (file_handling=fopen("\\bar.txt","a+"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
#ifdef UNICODE
//将Unicode转换为ANSI
wcstombs(ansiBuffer,buffer,sizeof(ansiBuffer));
result=fwrite(ansiBuffer,sizeof(char),strlen(ansiBuffer),file_handling);
#else
result=fwrite(buffer,sizeof(TCHAR),lstrlen(buffer),file_handling);
#endif
fclose(file_handling);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox(NULL,TEXT("打开文件失败!"),TEXT("提示"),MB_OK);
}
return result==0?FALSE:TRUE;
}
设备是Symbol(现在被Moto收购了)公司MC50,我弄了三天,其中抄了部分Symbol公司带的SDK中关于扫描条码的部分代码.编写的过程中遇到很多的问题,但还好都一点点解决了.
学习C/C++的路上迈出了第一步,对于C/C++的世界的认知还是很模糊,不知道什么时候才能真正的运用VC来开发,扔了VB和C#.
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。