本程序含有不明bug,有时候退格之后,再击键会不听使唤。。。也许是因为,windows其他程序对内核的占用而导致了本程序部分键盘码丢失吧,具体原因不明。
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("KeyView1") ;
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 ("Keyboard Message Viewer #1"),
WS_OVERLAPPEDWINDOW,
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 int cxClientMax, cyClientMax, cxClient, cyClient, cxChar, cyChar , cLinesMax;
HDC hdc ;
int i = 0, iType, j ;
PAINTSTRUCT ps ;
static TCHAR szBuffer[70] ; //szKeyName [32] ;
PTCHAR pcha ;
TEXTMETRIC tm ;
static RECT rectScroll ;
static int * tpmsg, *ina;
static int isz = 0;
TCHAR szTop[ 20 ] = L"请用键盘输入:" ;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; //等宽字体
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;
ReleaseDC (hwnd, hdc) ;
return 0;
/*case WM_DISPLAYCHANGE: //窗口分辨率改变时会发送这个消息
// Get maximum size of client area
cxClientMax = GetSystemMetrics (SM_CXMAXIMIZED) ;
cyClientMax = GetSystemMetrics (SM_CYMAXIMIZED) ;
//窗口最大化缺省值
// Get character size for fixed-pitch font */
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
// Calculate scrolling rectangle
if( tpmsg )
free( tpmsg );
cLinesMax = cxClient / cxChar ;
tpmsg = ( int * ) malloc ( cLinesMax * sizeof( int ) ) ;
rectScroll.left = 0 ;
rectScroll.right = cxClient ;
rectScroll.top = cyChar ;
rectScroll.bottom = 2 * cyChar;
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
case WM_CHAR:
switch( wParam )
{
case VK_RETURN:
break;
case VK_BACK:
isz--;
InvalidateRect( hwnd, &rectScroll, TRUE ) ; //是第二行变为无效区域,并把WM_PAINT消息放入队列
UpdateWindow( hwnd ) ; //判断是否有无效区域( WM_PAINT ),如果有就立刻刷新屏幕
break;
default:
szBuffer[ isz++ ] = wParam ;
InvalidateRect( hwnd, &rectScroll, TRUE ) ; //是第二行变为无效区域,并把WM_PAINT消息放入队列
UpdateWindow( hwnd ) ; //判断是否有无效区域( WM_PAINT ),如果有就立刻刷新屏幕
break;
}
return 0;
case WM_PAINT:
hdc = BeginPaint( hwnd, &ps ) ;
SelectObject( hdc, GetStockObject( SYSTEM_FIXED_FONT ) );
SetBkMode( hdc, TRANSPARENT ) ;
if( isz > 69 )
MessageBox( hwnd, TEXT("数组越界"), TEXT("isz"), MB_OK ) ;
pcha = ( PTCHAR ) malloc ( ( 70 ) * sizeof( TCHAR ) ) ;
wcscpy( pcha, szBuffer ) ; //tchar 版本的 strcpy函数
TextOut( hdc, 0, 0, szTop, lstrlen( szTop ) ) ;
TextOut( hdc, 0, cyChar, pcha, isz ) ;
free( pcha ) ;
EndPaint( hwnd, &ps ) ;
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}