/*---------------------------------------------------------------------------
EDRTEST.C -- Program using EDRLIB dynamic-link library
(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/
#include <windows.h>
#include "EDRLIB.h"
HWND fwhWnd;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("StrProg") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("DLL Demonstration Program"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU /*| WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX*/,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static TCHAR szMsg = TEXT("Enter the Key");
static BOOL fState = FALSE , hState = FALSE;
static int iNumX,iNumY;
static int icxBlock,icyBlock;
static int iNumRect = 0;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect[6], delRect, startRect, stopRect;
POINT point[12];
int x, y ,i=0;
iNumX = 230;
iNumY = 20;
icxBlock = (370-230)/2;
icyBlock = (250-20)/3;
//初始化小键盘绘制的12个点
for(x = 0; x <= 2; x++)
{
for(y = 0; y <= 3; y++)
{
point[i].x = iNumX + icxBlock * x ;
point[i].y = iNumY + icyBlock * y ;
i++;
}
}
//初始化按键区域
SetRect(&rect[0], point[0].x, point[0].y, point[5].x, point[5].y);
SetRect(&rect[1], point[4].x, point[4].y, point[9].x, point[9].y);
SetRect(&rect[2], point[1].x, point[1].y, point[6].x, point[6].y);
SetRect(&rect[3], point[5].x, point[5].y, point[10].x, point[10].y);
SetRect(&rect[4], point[2].x, point[2].y, point[7].x, point[7].y);
SetRect(&rect[5], point[6].x, point[6].y, point[11].x, point[11].y);
//创建白色rect擦除提示文字
SetRect(&delRect,0,0,200,300);
//创建按钮rect
SetRect(&startRect, 20, 60, 150, 100);
SetRect(&stopRect, 20, 120, 150, 160);
switch (message)
{
case WM_CREATE:
SetWindowPos(hwnd ,NULL, 400,200,400,300,0);
// SetHook();
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
FillRect(hdc,&delRect,GetStockObject(WHITE_BRUSH));
//绘制小键盘 7 8 4 5 1 2 界面 ;
Rectangle(hdc, point[0].x, point[0].y, point[11].x, point[11].y);
MoveToEx(hdc, point[1].x, point[1].y, NULL);
LineTo(hdc , point[9].x, point[9].y);
MoveToEx(hdc, point[2].x, point[2].y, NULL);
LineTo(hdc , point[10].x, point[10].y);
MoveToEx(hdc, point[4].x, point[4].y, NULL);
LineTo(hdc , point[7].x, point[7].y);
//绘制start stop 按钮
Rectangle(hdc, 20, 60, 150, 100);
Rectangle(hdc, 20, 120, 150, 160);
//按键状态,输出提示
if(fState == TRUE)
{
TextOut(hdc, 30, 250, TEXT("Enter the Key"), 13);
FillRect(hdc, &rect[iNumRect], GetStockObject(GRAY_BRUSH));
}
if(hState == TRUE)
{
FillRect(hdc, &startRect, GetStockObject(GRAY_BRUSH));
}
TextOut(hdc ,rect[0].left + icxBlock/2, rect[0].top + icyBlock/2 - 7,TEXT("7"), sizeof(TCHAR));
TextOut(hdc ,rect[1].left + icxBlock/2, rect[1].top + icyBlock/2 - 7,TEXT("8"), sizeof(TCHAR));
TextOut(hdc ,rect[2].left + icxBlock/2, rect[2].top + icyBlock/2 - 7,TEXT("4"), sizeof(TCHAR));
TextOut(hdc ,rect[3].left + icxBlock/2, rect[3].top + icyBlock/2 - 7,TEXT("5"), sizeof(TCHAR));
TextOut(hdc ,rect[4].left + icxBlock/2, rect[4].top + icyBlock/2 - 7,TEXT("1"), sizeof(TCHAR));
TextOut(hdc ,rect[5].left + icxBlock/2, rect[5].top + icyBlock/2 - 7 ,TEXT("2"), sizeof(TCHAR));
TextOut(hdc ,startRect.left + 35, startRect.top + 15,TEXT("START"), 5);
TextOut(hdc ,stopRect.left + 35, stopRect.top + 15 ,TEXT("STOP"), 4);
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_KEYDOWN:
if(fState==TRUE && hState == FALSE)
{
switch(iNumRect)
{
case 0:
chKey[7] = (BYTE)(wParam);
break;
case 1:
chKey[8] = (BYTE)(wParam);
break;
case 2:
chKey[4] = (BYTE)(wParam);
break;
case 3:
chKey[5] = (BYTE)(wParam);
break;
case 4:
chKey[1] = (BYTE)(wParam);
break;
case 5:
chKey[2] = (BYTE)(wParam);
break;
default:
break;
}
fState = FALSE;
InvalidateRect(hwnd, NULL, FALSE);
}
return 0;
case WM_LBUTTONDOWN:
x = (LOWORD(lParam) - iNumX) / icxBlock;
y = (HIWORD(lParam) - iNumY) / icyBlock;
if(x <= 3 && y <=4 && x >= 0 && y >= 0 && hState == FALSE)
{
fState = TRUE;
if(x == 0 && y == 0)iNumRect = 0;
if(x == 1 && y == 0)iNumRect = 1;
if(x == 0 && y == 1)iNumRect = 2;
if(x == 1 && y == 1)iNumRect = 3;
if(x == 0 && y == 2)iNumRect = 4;
if(x == 1 && y == 2)iNumRect = 5;
}
if( LOWORD(lParam) >= startRect.left && LOWORD(lParam) <= startRect.right &&
HIWORD(lParam) >= startRect.top && HIWORD(lParam) <= startRect.bottom && hState == FALSE && fState == FALSE)
{
hState = TRUE;
SetHook();
}
if( LOWORD(lParam) >= stopRect.left && LOWORD(lParam) <= stopRect.right &&
HIWORD(lParam) >= stopRect.top && HIWORD(lParam) <= stopRect.bottom && hState == TRUE)
{
hState = FALSE;
UnHook();
}
InvalidateRect(hwnd, NULL, FALSE);
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}