windows程序设计第五版 第六章 KEYVIEW1.c

来源:互联网 发布:2015年双11淘宝交易额 编辑:程序博客网 时间:2024/05/16 18:21
/*--------------------------------------------------------
KEYVIEW1.C -- Displays Keyboard and Character Messages
(c) Charles Petzold, 1998
--------------------------------------------------------*/


#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 ;
static int   cLinesMax, cLines ;
static PMSG  pmsg ;//PMSG就是MSG的指针形式
static RECT  rectScroll ;
LPCWSTR lpstr_1 = TEXT("cyClientMax") ;//++
LPTSTR szBuffer_3, szBuffer_4 ;//++
static TCHAR szTop[] = TEXT ("Message        Key       Char     ")
TEXT ("Repeat Scan Ext ALT Prev Tran") ;
static TCHAR szUnd[] = TEXT ("_______        ___       ____     ")
TEXT ("______ ____ ___ ___ ____ ____") ;


static TCHAR * szFormat[2] = { 


TEXT ("%-13s %3d %-15s%c%6u %4d %3s %3s %4s %4s"),//虚拟按键输出格式
TEXT ("%-13s            0x%04X%1s%c %6u %4d %3s %3s %4s %4s") } ;//字元代码输出格式


static TCHAR * szYes  = TEXT ("Yes") ;
static TCHAR * szNo   = TEXT ("No") ;
static TCHAR * szDown = TEXT ("Down") ;
static TCHAR * szUp   = TEXT ("Up") ;


static TCHAR * szMessage [] = { 
TEXT ("WM_KEYDOWN"),    TEXT ("WM_KEYUP"), 
TEXT ("WM_CHAR"),       TEXT ("WM_DEADCHAR"), 
TEXT ("WM_SYSKEYDOWN"), TEXT ("WM_SYSKEYUP"), 
TEXT ("WM_SYSCHAR"),    TEXT ("WM_SYSDEADCHAR") } ;
HDC          hdc ;
int          i, iType ;
PAINTSTRUCT  ps ;
TCHAR        szBuffer[128], szKeyName [32] ;
TEXTMETRIC   tm ;


switch (message)
{
case WM_CREATE:
case WM_DISPLAYCHANGE:
/*The WM_DISPLAYCHANGE message is sent to all windows when the 
display resolution has changed.
A window receives this message through its WindowProc function.       */


// Get maximum size of client area


cxClientMax = GetSystemMetrics (SM_CXMAXIMIZED) ;
cyClientMax = GetSystemMetrics (SM_CYMAXIMIZED) ;
/*百度百科里面有详细资料,他是定义顶层窗口最大化的省略尺寸*/


// Get character size for fixed-pitch font


hdc = GetDC (hwnd) ;


SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
GetTextMetrics (hdc, &tm) ;//字体结构定义
cxChar = tm.tmAveCharWidth ;//平均字符宽度
cyChar = tm.tmHeight + tm.tmInternalLeading;//字符高度 change


ReleaseDC (hwnd, hdc) ;


// Allocate memory for display lines
if (pmsg) //很有道理
free (pmsg) ;


cLinesMax = cyClientMax / cyChar ;//总行数赋值给cLinesMax = 42 ,为什么。。明明是28行。。
pmsg =( PMSG ) malloc (cLinesMax * sizeof (MSG)) ;//42个MSG空间
cLines = 0 ;




// fall through
case WM_SIZE:
if (message == WM_SIZE)
{
cxClient = LOWORD (lParam) ;//显示区域的大小
cyClient = HIWORD (lParam) ;
}
// Calculate scrolling rectangle


rectScroll.left   = 0 ;
rectScroll.right  = cxClient ;//窗口横坐标最大值
rectScroll.top    = cyChar ;//一个字符的高度
rectScroll.bottom = cyChar * (cyClient / cyChar) ;//取整后的窗口最大高度q


//----------------------------------------
szBuffer_3 =( LPTSTR ) malloc ( 100 * sizeof( TCHAR ) );
wsprintf( szBuffer_3, L"cLinesMax = %i, cyChar = %d, cyClient = %d ", cLinesMax, cyChar, cyClient ) ;
MessageBox( hwnd, szBuffer_3, lpstr_1, MB_OK ) ;//++
//这里我就是想看下,cyClient是个什么东西。。


InvalidateRect (hwnd, NULL, TRUE) ;
free ( szBuffer_3 ) ;//++


return 0 ;


case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
case WM_DEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
case WM_SYSDEADCHAR: 


// Rearrange storage array


for (i = cLinesMax - 1 ; i > 0 ; i--)
{
pmsg[i] = pmsg[i - 1] ;
}
// Store new message


pmsg[0].hwnd = hwnd ;
pmsg[0].message = message ;
pmsg[0].wParam = wParam ;
pmsg[0].lParam = lParam ;


cLines = min (cLines + 1, cLinesMax) ;


// Scroll up the display


ScrollWindow (hwnd, 0, -cyChar, &rectScroll, &rectScroll) ;


break ;        // ie, call DefWindowProc so Sys messages work


case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;


SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
SetBkMode (hdc, TRANSPARENT) ;//设置背景属性,不影响现有背景颜色
TextOut (hdc, 0, 0, szTop, lstrlen (szTop)) ;
TextOut (hdc, 0, 0, szUnd, lstrlen (szUnd)) ;


for (i = 0 ; i < min (cLines, cyClient / cyChar - 1) ; i++)
{
iType = pmsg[i].message == WM_CHAR ||
pmsg[i].message == WM_SYSCHAR ||
pmsg[i].message == WM_DEADCHAR ||
pmsg[i].message == WM_SYSDEADCHAR ;//字元代码的话,iType = 1


GetKeyNameText (pmsg[i].lParam, szKeyName,//这个就是所谓的获得 按键讯息吧
sizeof (szKeyName) / sizeof (TCHAR)) ;
/*The GetKeyNameText function retrieves a string that represents the name of a key. */


/*------------------------------------------------------
szBuffer_4 =( LPTSTR ) malloc ( 100 * sizeof( TCHAR ) );
wsprintf( szBuffer_4, L"i = %d", i ) ;
MessageBox( hwnd, szBuffer_4, lpstr_1, MB_OK ) ;//++*/
//------------------------------------------------------

TextOut (hdc, 0, (cyClient / cyChar - 1 - i ) * cyChar, szBuffer,//change
wsprintf (szBuffer, szFormat [iType],

szMessage [pmsg[i].message - WM_KEYFIRST],//这里是一个头文件的问题,很重要 ,

/* 

这里要插入解释 具体网址:http://blog.csdn.net/rjw1900/article/details/6089920,这里讲的很明白

*/           

pmsg[i].wParam,//代表字元代码,他是Unicode或者ANSI值
(PTSTR) (iType ? TEXT (" ") : szKeyName),//iType = 1 执行" ", 否则执行 虚拟键码
(TCHAR) (iType ? pmsg[i].wParam : ' '),//真时 输出字元代码, 否则输出' '
LOWORD (pmsg[i].lParam),//重复计数
HIWORD (pmsg[i].lParam) & 0xFF,//OEM扫描码。。目测没什么用
0x01000000 & pmsg[i].lParam ? szYes  : szNo,
0x20000000 & pmsg[i].lParam ? szYes  : szNo,
0x40000000 & pmsg[i].lParam ? szDown : szUp,
0x80000000 & pmsg[i].lParam ? szUp   : szDown)) ;
//free( szBuffer_4 ) ;//++++
}
EndPaint (hwnd, &ps) ;
return 0 ;


case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}