EVC写的程序(转)

本文介绍了一个使用Windows API实现的条形码扫描程序。该程序通过对话框接收用户输入,并利用SCAN_API进行条码扫描。文章详细展示了如何处理条码数据、保存扫描结果到文件以及文本框的子类化等过程。

#include "stdafx.h"

None.gif#include "resource.h"

None.gif#include <windows.h>

None.gif#include <windowsx.h>

None.gif#include <ScanCAPI.h>

None.gif#include <stdio.h>

None.gif#include <string.h>

None.gif#include <wchar.h>

None.gif#define     countof(x) sizeof(x)/sizeof(x[0])

None.gif

None.gifBOOL CALLBACK MAINProc(HWND,UINT,WPARAM,LPARAM);

None.gifLRESULT CALLBACK EditProc(HWND,UINT,WPARAM,LPARAM);

None.gifBOOL Save(HWND hwnd_barcode,HWND hwnd_qty);

None.gifvoid InitFormControl(HWND);

None.gifWNDPROC PreEditProc[2];

None.gif

None.gifHINSTANCE hInst    = NULL;

None.gifHANDLE            hScanner                = NULL;

None.gifLPSCAN_BUFFER    lpScanBuffer            = NULL;

None.gifTCHAR            szScannerName[MAX_PATH] = TEXT("SCN1:");    // default scanner name

None.gifDWORD            dwScanSize                = 7095;                // default scan buffer size    

None.gifDWORD            dwScanTimeout            = 0;                // default timeout value (0 means no timeout)    

None.gifBOOL            bUseText                = TRUE;

None.gifBOOL            bTriggerFlag            = FALSE;

None.gifBOOL            bRequestPending            = FALSE;

None.gifBOOL            bStopScanning            = FALSE;

None.gif

None.gifenum tagUSERMSGS

ExpandedBlockStart.gifContractedBlock.gifdot.gif{

InBlock.gif    UM_SCAN    = WM_USER + 0x200,

InBlock.gif    UM_STARTSCANNING,

InBlock.gif    UM_STOPSCANNING

ExpandedBlockEnd.gif};

None.gif

None.gifint WINAPI WinMain(    HINSTANCE hInstance,

None.gif                    HINSTANCE hPrevInstance,

None.gif                    LPTSTR    lpCmdLine,

None.gif                    int       nCmdShow)

ExpandedBlockStart.gifContractedBlock.gifdot.gif{

InBlock.gif    int nResult;

InBlock.gif

InBlock.gif    hInst = hInstance;

InBlock.gif    nResult = DialogBoxParamW(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MAINProc,0L);

InBlock.gif

InBlock.gif    return nResult;

ExpandedBlockEnd.gif}

None.gif

None.gifBOOL CALLBACK MAINProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

ExpandedBlockStart.gifContractedBlock.gifdot.gif{

InBlock.gif    DWORD dwResult;

InBlock.gif    LPSCAN_BUFFER    lpScanBuf;

InBlock.gif    static HWND    hctl_temp,hctl_barcode, hctl_qty, hctl_status,hctl_info;

InBlock.gif    switch(uMsg)

ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{

InBlock.gif    case WM_INITDIALOG:

InBlock.gif        PostMessage(hwnd,UM_STARTSCANNING,0,0L);

InBlock.gif        hctl_barcode=GetDlgItem(hwnd,IDC_EDIT_Barcode);

InBlock.gif        hctl_qty=GetDlgItem(hwnd,IDC_EDIT_Qty);

InBlock.gif        hctl_status=GetDlgItem(hwnd,IDC_STATIC_STATUS);

InBlock.gif        hctl_info=GetDlgItem(hwnd,IDC_STATIC_INFO);

InBlock.gif        InitFormControl(hwnd);

InBlock.gif        //子类化

InBlock.gif        PreEditProc[0]=(WNDPROC) SetWindowLong (hctl_barcode, 

InBlock.gif                                             GWL_WNDPROC, (LONG) EditProc) ;

InBlock.gif        PreEditProc[1]=(WNDPROC) SetWindowLong (hctl_qty, 

InBlock.gif                                     GWL_WNDPROC, (LONG) EditProc) ;

InBlock.gif        break;

InBlock.gif    case UM_STARTSCANNING:

InBlock.gif        dwResult=SCAN_Open(szScannerName,&hScanner);

InBlock.gif        if(dwResult!=E_SCN_SUCCESS)

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            MessageBox(hwnd,TEXT("不能打开激光器"),TEXT("提示"),MB_OK);

InBlock.gif            SendMessage(hwnd,UM_STOPSCANNING,0,0L);

InBlock.gif            break;

ExpandedSubBlockEnd.gif        }

InBlock.gif        dwResult=SCAN_Enable(hScanner);

InBlock.gif        

InBlock.gif        if(dwResult!=E_SCN_SUCCESS)

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            MessageBox(hwnd,TEXT("激光器不可用"),TEXT("提示"),MB_OK);

InBlock.gif            SendMessage(hwnd,UM_STOPSCANNING,0,0L);

InBlock.gif            break;

ExpandedSubBlockEnd.gif        }

InBlock.gif        lpScanBuffer = SCAN_AllocateBuffer(bUseText, dwScanSize);

InBlock.gif        if (lpScanBuffer==NULL)

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            MessageBox(hwnd,TEXT("未能分配扫描缓冲"),TEXT("提示"),MB_OK);

InBlock.gif            SendMessage(hwnd,UM_STOPSCANNING,0,0L);

InBlock.gif            return TRUE;

ExpandedSubBlockEnd.gif        }

InBlock.gif        //SCAN_ReadLabelMsg扫描成功后向hwnd发送UM_SCAN的消息

InBlock.gif        dwResult=SCAN_ReadLabelMsg(hScanner,

InBlock.gif            lpScanBuffer,

InBlock.gif            hwnd,

InBlock.gif            UM_SCAN,

InBlock.gif            dwScanTimeout,

InBlock.gif            NULL);

InBlock.gif        if(dwResult!=E_SCN_SUCCESS)

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            MessageBox(hwnd,TEXT("SCAN_ReadLabelMsg发生错误"),TEXT("提示"),MB_OK);

InBlock.gif            SendMessage(hwnd,UM_STOPSCANNING,0,0L);

ExpandedSubBlockEnd.gif        }

InBlock.gif        else

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            //只有到最后成功得到缓存后才置此标志为TRUE

InBlock.gif            bRequestPending=TRUE;

ExpandedSubBlockEnd.gif        }

InBlock.gif        break;

InBlock.gif        return true;

InBlock.gif    case UM_STOPSCANNING:

InBlock.gif        //在未停止扫描且未已正常得到缓存后才执行SCAN_Flush

InBlock.gif        if (!bStopScanning && bRequestPending)    

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            SCAN_Flush(hScanner);

ExpandedSubBlockEnd.gif        }

InBlock.gif

InBlock.gif        if (!bRequestPending)            

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{                             

InBlock.gif            SCAN_Disable(hScanner);

InBlock.gif

InBlock.gif            if (lpScanBuffer)

InBlock.gif                SCAN_DeallocateBuffer(lpScanBuffer);

InBlock.gif

InBlock.gif            SCAN_Close(hScanner);

InBlock.gif

InBlock.gif            EndDialog(hwnd, 0);

ExpandedSubBlockEnd.gif        }

InBlock.gif        bStopScanning = TRUE;

InBlock.gif        break;

InBlock.gif    case WM_ACTIVATE:

InBlock.gif        switch(LOWORD(wParam))

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif        case WA_INACTIVE:

InBlock.gif

InBlock.gif            if (bRequestPending)

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                dwResult=SCAN_Flush(hScanner);

ExpandedSubBlockEnd.gif            }

InBlock.gif            break;

InBlock.gif        default:

InBlock.gif            if (!bRequestPending && lpScanBuffer != NULL && !bStopScanning)

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                dwResult=SCAN_ReadLabelMsg(hScanner,

InBlock.gif                    lpScanBuffer,

InBlock.gif                    hwnd,

InBlock.gif                    UM_SCAN,

InBlock.gif                    dwScanTimeout,

InBlock.gif                    NULL);

InBlock.gif                if (dwResult!=E_SCN_SUCCESS)

ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{

InBlock.gif                    MessageBox(hwnd,TEXT("SCAN_ReadLabelMsg发生错误"),TEXT("提示"),MB_OK);

InBlock.gif                    SendMessage(hwnd,UM_STOPSCANNING,0,0L);

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            break;

ExpandedSubBlockEnd.gif        }

InBlock.gif        break;

InBlock.gif    case UM_SCAN:

InBlock.gif        bRequestPending=FALSE;

InBlock.gif        lpScanBuf=(LPSCAN_BUFFER)lParam;

InBlock.gif        if(lpScanBuf==NULL)

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            MessageBox(hwnd,TEXT("未能正确得到数据"),TEXT("提示"),MB_OK);

InBlock.gif            SendMessage(hwnd,UM_STOPSCANNING,0,0L);

ExpandedSubBlockEnd.gif        }

InBlock.gif        switch(SCNBUF_GETSTAT(lpScanBuf))

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif        case E_SCN_SUCCESS:

InBlock.gif            hctl_temp=GetFocus();

InBlock.gif            if(hctl_temp==hctl_barcode ||hctl_temp==hctl_qty)

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                Edit_SetText(hctl_temp,(LPTSTR)SCNBUF_GETDATA(lpScanBuffer));

ExpandedSubBlockEnd.gif            }

InBlock.gif            else

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                Edit_SetText(hctl_barcode,(LPTSTR)SCNBUF_GETDATA(lpScanBuffer));

InBlock.gif                SendMessage(hwnd,WM_KEYDOWN,VK_RETURN,NULL);

ExpandedSubBlockEnd.gif            }

InBlock.gif            break;

ExpandedSubBlockEnd.gif        }

InBlock.gif        if (GetFocus())

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif            dwResult = SCAN_ReadLabelMsg(hScanner,

InBlock.gif                                         lpScanBuffer,

InBlock.gif                                         hwnd,

InBlock.gif                                         uMsg,

InBlock.gif                                         dwScanTimeout,

InBlock.gif                                         NULL);

InBlock.gif            if ( dwResult != E_SCN_SUCCESS )

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                MessageBox(hwnd,TEXT("SCAN_ReadLabelMsg未能正确得到数据"),TEXT("提示"),MB_OK);

InBlock.gif                SendMessage(hwnd,UM_STOPSCANNING,0,0L);

ExpandedSubBlockEnd.gif            }

InBlock.gif            else

InBlock.gif                bRequestPending = TRUE;

ExpandedSubBlockEnd.gif        }

InBlock.gif        return TRUE;

InBlock.gif    case WM_COMMAND:

InBlock.gif        switch (LOWORD(wParam))

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif        case IDCANCEL:

InBlock.gif            bRequestPending=TRUE;

InBlock.gif            SendMessage(hwnd,UM_STOPSCANNING,0,0L);

InBlock.gif            bRequestPending=FALSE;

InBlock.gif            SendMessage(hwnd,UM_STOPSCANNING,0,0L);

InBlock.gif            break;

ExpandedSubBlockEnd.gif        }

InBlock.gif        return TRUE;

InBlock.gif    case WM_KEYDOWN:

InBlock.gif        switch (wParam)

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif        case VK_RETURN:

InBlock.gif            hctl_temp=GetFocus();

InBlock.gif            if (hctl_temp==hctl_barcode)

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                SetFocus(hctl_qty);

InBlock.gif                SendMessage(hctl_qty,EM_SETSEL,0,-1);

ExpandedSubBlockEnd.gif            }

InBlock.gif            else if (hctl_temp==hctl_qty)

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                if (Save(hctl_barcode,hctl_qty)==TRUE)

ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{

InBlock.gif                    SetWindowText(hctl_info,TEXT("追加数据成功!"));

InBlock.gif                    InitFormControl(hwnd);

ExpandedSubBlockEnd.gif                }

InBlock.gif                else

ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{

InBlock.gif                    SetWindowText(hctl_info,TEXT("追加数据失败!"));

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            else

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                SetFocus(hctl_barcode);

ExpandedSubBlockEnd.gif            }

InBlock.gif            break;

InBlock.gif        case VK_F1:

InBlock.gif            InitFormControl(hwnd);

InBlock.gif            break;

InBlock.gif        case VK_F2:

InBlock.gif            hctl_temp=GetFocus();

InBlock.gif            if (hctl_temp==hctl_qty)

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif                Edit_SetText(hctl_temp,TEXT("-"));

InBlock.gif                Edit_SetSel(hctl_temp,1,1);

ExpandedSubBlockEnd.gif            }

InBlock.gif            break;

InBlock.gif        case VK_F3:

InBlock.gif            break;

ExpandedSubBlockEnd.gif        }

InBlock.gif        break;

ExpandedSubBlockEnd.gif    }

InBlock.gif

InBlock.gif    return FALSE;

ExpandedBlockEnd.gif}

None.gif

None.gif//初始化

None.gifvoid InitFormControl(HWND hwnd)

ExpandedBlockStart.gifContractedBlock.gifdot.gif{

InBlock.gif    HWND hctl_barcode, hctl_qty;

InBlock.gif    hctl_barcode=GetDlgItem(hwnd,IDC_EDIT_Barcode);

InBlock.gif    Edit_SetText(hctl_barcode,TEXT(""));

InBlock.gif    hctl_qty=GetDlgItem(hwnd,IDC_EDIT_Qty);

InBlock.gif    Edit_SetText(hctl_qty,TEXT("1"));

InBlock.gif    SetFocus(hctl_barcode);

ExpandedBlockEnd.gif}

None.gif

None.gif//子类化文本框

None.gifLRESULT CALLBACK EditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

ExpandedBlockStart.gifContractedBlock.gifdot.gif{

InBlock.gif     int id = GetWindowLong (hwnd, GWL_ID) ;

InBlock.gif     LPMSG msg;

InBlock.gif     WNDPROC proc;

InBlock.gif     switch (uMsg)

ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{

InBlock.gif     case WM_GETDLGCODE :

InBlock.gif        msg=(LPMSG) lParam;

InBlock.gif        switch(msg->message)

ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{

InBlock.gif        case WM_KEYDOWN:

InBlock.gif            switch(msg->wParam)

ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{

InBlock.gif            case VK_TAB:

InBlock.gif            case VK_RETURN:

InBlock.gif            case VK_F1:

InBlock.gif            case VK_F2:

InBlock.gif            case VK_F3:

InBlock.gif                SendMessage(GetParent(hwnd),msg->message,msg->wParam,msg->lParam);

InBlock.gif                break;

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*

InBlock.gif        switch(msg->message)

InBlock.gif        {

InBlock.gif        case EN_SETFOCUS:

InBlock.gif            hctl_qty=GetDlgItem(GetParent(hwnd),IDC_EDIT_Qty);

InBlock.gif            if (msg->hwnd==hctl_qty)

InBlock.gif            {

InBlock.gif                SetDlgItemInt(GetParent(hwnd),IDC_STATIC_STATUS,msg->message,FALSE);

InBlock.gif                //Edit_SetSel(hwnd,-1,GetWindowTextLength(hwnd));

InBlock.gif                Edit_SetSel(hwnd,0,-1);

InBlock.gif                //SNDMSG(hwnd, EM_SETSEL, 0, GetWindowTextLength(hwnd));

InBlock.gif            }

InBlock.gif        }

ExpandedSubBlockEnd.gif        */

ExpandedSubBlockEnd.gif     }

InBlock.gif     if (id==IDC_EDIT_Barcode)

ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{

InBlock.gif         proc=PreEditProc[0];

ExpandedSubBlockEnd.gif     }

InBlock.gif     else if (id==IDC_EDIT_Qty)

ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{

InBlock.gif        proc=PreEditProc[1];

ExpandedSubBlockEnd.gif     }

InBlock.gif     return CallWindowProc (proc, hwnd, uMsg, wParam, lParam) ;

ExpandedBlockEnd.gif}

None.gif

None.gif//保存

None.gifBOOL Save(HWND hwnd_barcode,HWND hwnd_qty)

ExpandedBlockStart.gifContractedBlock.gifdot.gif{

InBlock.gif    int result=FALSE;

InBlock.gif    FILE * file_handling=NULL;

InBlock.gif    TCHAR barcode[255],qty[255];

InBlock.gif    TCHAR buffer[255];

InBlock.gif    char ansiBuffer[255];

InBlock.gif    GetWindowText(hwnd_barcode,buffer,sizeof(buffer));

InBlock.gif    if (!lstrlen (buffer))

ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{

InBlock.gif        MessageBox(NULL,TEXT("请扫描或输入条码!"),TEXT("提示"),MB_OK);

InBlock.gif        SetFocus(hwnd_barcode);

InBlock.gif        return result;

ExpandedSubBlockEnd.gif    }

InBlock.gif    else

ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{

InBlock.gif        lstrcpy (barcode,buffer);

ExpandedSubBlockEnd.gif    }

InBlock.gif    memset(buffer,0,sizeof(buffer));

InBlock.gif    GetWindowText(hwnd_qty,buffer,sizeof(buffer));

InBlock.gif    if (!lstrlen (buffer))

ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{

InBlock.gif        MessageBox(NULL,TEXT("请输入数量!"),TEXT("提示"),MB_OK);

InBlock.gif        SetFocus(hwnd_qty);

InBlock.gif        return result;

ExpandedSubBlockEnd.gif    }

InBlock.gif    else                    //判断数字

ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{

InBlock.gif        

ExpandedSubBlockEnd.gif    }

InBlock.gif    lstrcpy (qty,buffer);

InBlock.gif    memset(buffer,0,sizeof(buffer));

InBlock.gif    swprintf(buffer,TEXT("%s,%s\n"),barcode,qty);

InBlock.gif    if (file_handling=fopen("\\bar.txt","a+"))

ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{

InBlock.gif#ifdef UNICODE

InBlock.gif        //将Unicode转换为ANSI

InBlock.gif        wcstombs(ansiBuffer,buffer,sizeof(ansiBuffer));

InBlock.gif        result=fwrite(ansiBuffer,sizeof(char),strlen(ansiBuffer),file_handling);

InBlock.gif#else

InBlock.gif        result=fwrite(buffer,sizeof(TCHAR),lstrlen(buffer),file_handling);

InBlock.gif#endif

InBlock.gif        fclose(file_handling);

ExpandedSubBlockEnd.gif    }

InBlock.gif    else

ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{

InBlock.gif        MessageBox(NULL,TEXT("打开文件失败!"),TEXT("提示"),MB_OK);

ExpandedSubBlockEnd.gif    }

InBlock.gif    return result==0?FALSE:TRUE;

ExpandedBlockEnd.gif}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值