📄 sl11demo.c
字号:
#include <windows.h>
#pragma hdrstop
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "slusb.h"
#include "resource.h"
long PASCAL WndProc (HWND hwnd, UINT message, UINT wParam,LONG lParam);
LONG cxChar,cyChar;
char szGenString[80];
TCHAR szTitle[80];
HGLOBAL hDibBits=NULL;
LPBITMAPINFO lpBMI=NULL;
LPBYTE bImgBuf,bBuf=NULL;
WORD button=0;
void Idle(void)
{
MSG msg;
if(PeekMessage (&msg, NULL, 0, 0,PM_REMOVE ))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
void CleanUp()
{
if(hDibBits!=NULL)
{
GlobalUnlock(hDibBits);
GlobalFree(hDibBits);
}
CloseUsbDev();
}
VOID _cdecl mb_exit(LPCSTR format,...)
{
char szMes[80];
va_list args;
va_start(args,format);
wvsprintf(szMes,format,args);
MessageBox(NULL,szMes,"SL11DEMO Error",MB_ICONSTOP);
va_end(args);
CleanUp();
exit(1);
}
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,LPSTR lpszCmdLine, int nCmdShow)
{
static char szAppName [20];
HWND hwnd ;
MSG msg;
WNDCLASS wndclass ;
DWORD time;
HDC hdc;
DWORD size;
WORD flag;
GetModuleFileName(hInstance,szGenString,sizeof(szGenString));
strcpy(szAppName,strrchr(szGenString,'\\')+1);
*(strrchr(szAppName,'.'))='\0';
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = DLGWINDOWEXTRA ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = NULL;//LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = GetStockObject (GRAY_BRUSH) ;
wndclass.lpszMenuName = "SL11DEMO" ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
else
mb_exit("Can't load more than one instance of %s",szAppName);
GetModuleFileName(hInstance,szGenString,sizeof(szGenString));
strcpy(strrchr(szGenString,'.'),".ini");
if (!FindUsbDev(flag=GetPrivateProfileInt("vid","vid",0x4ce,szGenString)))
mb_exit("Find USB dev: %04x fail",flag);
flag = GetPrivateProfileInt("time","time",FALSE,szGenString);
hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT,0,
CW_USEDEFAULT,0,
NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, SW_SHOW);
SetCursor(LoadCursor(NULL,IDC_ARROW));
PostMessage(hwnd,WM_COMMAND,CM_SCAN,0l);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
switch(button)
{
case CM_SCAN:
button=0;
case CM_START:
if (!UsbVendorCmdRead(3,0,0,4,&size)) mb_exit("Write command Failed\n");
Idle();
if (hDibBits==NULL)
{
if((hDibBits=GlobalAlloc(GMEM_MOVEABLE,size))==NULL)
mb_exit("Couldn't allocate memory for DIB");
bBuf = (LPBYTE)GlobalLock(hDibBits);
if (bBuf==NULL)
mb_exit("allocate memory error");
}
time=GetTickCount();
if (!UsbDataRead(size,bBuf)) mb_exit("Read Data Failed\n");
time = GetTickCount() - time;
bImgBuf = bBuf;
bImgBuf += sizeof(BITMAPFILEHEADER);
lpBMI = (LPBITMAPINFO)bImgBuf;
bImgBuf += sizeof(BITMAPINFOHEADER)+lpBMI->bmiHeader.biClrUsed * sizeof(RGBQUAD);
if(lpBMI->bmiHeader.biSizeImage==0)
lpBMI->bmiHeader.biSizeImage=lpBMI->bmiHeader.biWidth*lpBMI->bmiHeader.biHeight;
SetWindowPos(hwnd,HWND_TOP,0,0,
lpBMI->bmiHeader.biWidth+(GetSystemMetrics(SM_CXBORDER)*2),
lpBMI->bmiHeader.biHeight+(GetSystemMetrics(SM_CYBORDER)*2)+GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYMENU),
SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOZORDER);
InvalidateRect(hwnd,NULL,TRUE);
hdc=GetDC(hwnd);
RealizePalette(hdc);
ReleaseDC(hwnd,hdc);
if(flag)
{ if (!time) time=1L;
wsprintf(szTitle,"%d %d bps",size,(1000*size)/time);
SetWindowText(hwnd,szTitle);
}
InvalidateRect(hwnd,NULL,FALSE);
ShowWindow (hwnd, SW_SHOW);
break;
}
}
return msg.wParam ;
}
long PASCAL WndProc (HWND hwnd, UINT message, UINT wParam,LONG lParam)
{
static int cxClient,cyClient;
HDC hdc ;
PAINTSTRUCT ps ;
TEXTMETRIC tm;
switch (message)
{
case WM_CREATE:
GetTextMetrics((hdc=GetDC(hwnd)),&tm);
cyChar=tm.tmHeight+tm.tmExternalLeading;
cxChar=(tm.tmAveCharWidth/2)*3;
ReleaseDC(hwnd,hdc);
return 0;
case WM_COMMAND:
switch (wParam)
{
case CM_SCAN:
button=wParam;
break;
case CM_START:
button = wParam;
break;
case CM_STOP:
button = 0;
break;
case CM_EXIT:
PostMessage(hwnd,WM_CLOSE,0,0L);
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
StretchDIBits(hdc, 0,0, cxClient, cyClient, 0, 0,
cxClient, cyClient, bImgBuf,
(LPBITMAPINFO)lpBMI,
DIB_RGB_COLORS,SRCCOPY);
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_DESTROY:
CleanUp();
PostQuitMessage (0) ;
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -