业精于勤荒于嬉,形成于思毁于随
只吃馒头不吃菜,省下钱来谈俩恋爱!This is my simply life!
博客园
首页
新随笔
新文章
联系
管理
订阅
随笔- 144 文章- 2 评论- 56
我的第一个用EVC写的程序!
//
EvcSymbolTest.cpp : Defines the entry point for the application.
//
#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])
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
];
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;
enum
tagUSERMSGS
{
UM_SCAN
=
WM_USER
+
0x200
,
UM_STARTSCANNING,
UM_STOPSCANNING
}
;
int
WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int
nCmdShow)
{
int
nResult;
hInst
=
hInstance;
nResult
=
DialogBoxParamW(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MAINProc,
0L
);
return
nResult;
}
BOOL CALLBACK MAINProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DWORD dwResult;
LPSCAN_BUFFER lpScanBuf;
static
HWND hctl_temp,hctl_barcode, hctl_qty, hctl_status,hctl_info;
switch
(uMsg)
{
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)
{
MessageBox(hwnd,TEXT(
"
不能打开激光器
"
),TEXT(
"
提示
"
),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,
0
,
0L
);
break
;
}
dwResult
=
SCAN_Enable(hScanner);
if
(dwResult
!=
E_SCN_SUCCESS)
{
MessageBox(hwnd,TEXT(
"
激光器不可用
"
),TEXT(
"
提示
"
),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,
0
,
0L
);
break
;
}
lpScanBuffer
=
SCAN_AllocateBuffer(bUseText, dwScanSize);
if
(lpScanBuffer
==
NULL)
{
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)
{
MessageBox(hwnd,TEXT(
"
SCAN_ReadLabelMsg发生错误
"
),TEXT(
"
提示
"
),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,
0
,
0L
);
}
else
{
//
只有到最后成功得到缓存后才置此标志为TRUE
bRequestPending
=
TRUE;
}
break
;
return
true
;
case
UM_STOPSCANNING:
//
在未停止扫描且未已正常得到缓存后才执行SCAN_Flush
if
(
!
bStopScanning
&&
bRequestPending)
{
SCAN_Flush(hScanner);
}
if
(
!
bRequestPending)
{
SCAN_Disable(hScanner);
if
(lpScanBuffer)
SCAN_DeallocateBuffer(lpScanBuffer);
SCAN_Close(hScanner);
EndDialog(hwnd,
0
);
}
bStopScanning
=
TRUE;
break
;
case
WM_ACTIVATE:
switch
(LOWORD(wParam))
{
case
WA_INACTIVE:
if
(bRequestPending)
{
dwResult
=
SCAN_Flush(hScanner);
}
break
;
default
:
if
(
!
bRequestPending
&&
lpScanBuffer
!=
NULL
&&
!
bStopScanning)
{
dwResult
=
SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
UM_SCAN,
dwScanTimeout,
NULL);
if
(dwResult
!=
E_SCN_SUCCESS)
{
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)
{
MessageBox(hwnd,TEXT(
"
未能正确得到数据
"
),TEXT(
"
提示
"
),MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,
0
,
0L
);
}
switch
(SCNBUF_GETSTAT(lpScanBuf))
{
case
E_SCN_SUCCESS:
hctl_temp
=
GetFocus();
if
(hctl_temp
==
hctl_barcode
||
hctl_temp
==
hctl_qty)
{
Edit_SetText(hctl_temp,(LPTSTR)SCNBUF_GETDATA(lpScanBuffer));
}
else
{
Edit_SetText(hctl_barcode,(LPTSTR)SCNBUF_GETDATA(lpScanBuffer));
SendMessage(hwnd,WM_KEYDOWN,VK_RETURN,NULL);
}
break
;
}
if
(GetFocus())
{
dwResult
=
SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
uMsg,
dwScanTimeout,
NULL);
if
( dwResult
!=
E_SCN_SUCCESS )
{
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))
{
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)
{
case
VK_RETURN:
hctl_temp
=
GetFocus();
if
(hctl_temp
==
hctl_barcode)
{
SetFocus(hctl_qty);
SendMessage(hctl_qty,EM_SETSEL,
0
,
-
1
);
}
else
if
(hctl_temp
==
hctl_qty)
{
if
(Save(hctl_barcode,hctl_qty)
==
TRUE)
{
SetWindowText(hctl_info,TEXT(
"
追加数据成功!
"
));
InitFormControl(hwnd);
}
else
{
SetWindowText(hctl_info,TEXT(
"
追加数据失败!
"
));
}
}
else
{
SetFocus(hctl_barcode);
}
break
;
case
VK_F1:
InitFormControl(hwnd);
break
;
case
VK_F2:
hctl_temp
=
GetFocus();
if
(hctl_temp
==
hctl_qty)
{
Edit_SetText(hctl_temp,TEXT(
"
-
"
));
Edit_SetSel(hctl_temp,
1
,
1
);
}
break
;
case
VK_F3:
break
;
}
break
;
}
return
FALSE;
}
//
初始化
void
InitFormControl(HWND hwnd)
{
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);
}
//
子类化文本框
LRESULT CALLBACK EditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int
id
=
GetWindowLong (hwnd, GWL_ID) ;
LPMSG msg;
WNDPROC proc;
switch
(uMsg)
{
case
WM_GETDLGCODE :
msg
=
(LPMSG) lParam;
switch
(msg
->
message)
{
case
WM_KEYDOWN:
switch
(msg
->
wParam)
{
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
;
}
}
/**/
/*
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)
{
proc
=
PreEditProc[
0
];
}
else
if
(id
==
IDC_EDIT_Qty)
{
proc
=
PreEditProc[
1
];
}
return
CallWindowProc (proc, hwnd, uMsg, wParam, lParam) ;
}
//
保存
BOOL Save(HWND hwnd_barcode,HWND hwnd_qty)
{
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))
{
MessageBox(NULL,TEXT(
"
请扫描或输入条码!
"
),TEXT(
"
提示
"
),MB_OK);
SetFocus(hwnd_barcode);
return
result;
}
else
{
lstrcpy (barcode,buffer);
}
memset(buffer,
0
,
sizeof
(buffer));
GetWindowText(hwnd_qty,buffer,
sizeof
(buffer));
if
(
!
lstrlen (buffer))
{
MessageBox(NULL,TEXT(
"
请输入数量!
"
),TEXT(
"
提示
"
),MB_OK);
SetFocus(hwnd_qty);
return
result;
}
else
//
判断数字
{
}
lstrcpy (qty,buffer);
memset(buffer,
0
,
sizeof
(buffer));
swprintf(buffer,TEXT(
"
%s,%s\n
"
),barcode,qty);
if
(file_handling
=
fopen(