《Windows程序设计(第五版)源代码A(chap06).doc》由会员分享,可在线阅读,更多相关《Windows程序设计(第五版)源代码A(chap06).doc(27页珍藏版)》请在金锄头文库上搜索。
1、chap06-KeyView1/*- KEYVIEW1.C - Displays Keyboard and Character Messages (c) Charles Petzold, 1998 -*/#include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (KeyView
2、1) ; 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
3、_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 (szAppNa
4、me, 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 ms
5、g.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 ; static RECT rectScroll ; static TCHAR szTop = TEXT (Message Key Char ) TEXT (Repeat S
6、can Ext ALT Prev Tran) ; static TCHAR szUnd = TEXT (_ _ _ ) TEXT (_ _ _ _ _ _) ; static TCHAR * szFormat2 = 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
7、= 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 szBuffer128, szKey
8、Name 32 ; TEXTMETRIC tm ; switch (message) case WM_CREATE: case WM_DISPLAYCHANGE: / 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, Ge
9、tStockObject (SYSTEM_FIXED_FONT) ; GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cyChar = tm.tmHeight ; ReleaseDC (hwnd, hdc) ; / Allocate memory for display lines if (pmsg) free (pmsg) ; cLinesMax = cyClientMax / cyChar ; pmsg = malloc (cLinesMax * sizeof (MSG) ; cLines = 0 ; / fall thro
10、ugh 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) ; InvalidateRect (hwnd, NULL, TRUE) ; 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-) pmsgi = pmsgi - 1 ; / Store new message