www.pudn.com > KeyTrac.rar > KeyTrac.cpp


//====================================================================== 
// KeyTrac - displays keyboard messages 
// 
// Written for the book Programming Windows CE 
// Copyright (C) 2003 Douglas Boling 
//====================================================================== 
#include                  // For all that Windows stuff 
#include                 // Command bar includes 
#include "keytrac.h"                 // Program-specific stuff 
 
// The include and lib files for the Pocket PC are conditionally 
// included so that this example can share the same project file.  This 
// is necessary since this example must have a menu bar on the Pocket 
// PC to have a SIP button. 
#if defined(WIN32_PLATFORM_PSPC) 
#include                 // Add Pocket PC includes. 
#pragma comment( lib, "aygshell" )   // Link Pocket PC lib for menu bar. 
#endif 
 
//---------------------------------------------------------------------- 
// Global data 
// 
const TCHAR szAppName[] = TEXT ("KeyTrac"); 
HINSTANCE hInst;                     // Program instance handle 
 
// Program-specific global data 
MYKEYARRAY ka[16]; 
int nKeyCnt = 0; 
int nFontHeight; 
 
// Array associates key messages with text tags 
KEYNAMESTRUCT knArray[] = {{WM_KEYDOWN,     TEXT ("WM_KEYDOWN")},  
                           {WM_KEYUP,       TEXT ("WM_KEYUP")}, 
                           {WM_CHAR,        TEXT ("WM_CHAR")}, 
                           {WM_SYSCHAR,     TEXT ("WM_SYSCHAR")}, 
                           {WM_SYSKEYUP,    TEXT ("WM_SYSKEYUP")}, 
                           {WM_SYSKEYDOWN,  TEXT ("WM_SYSKEYDOWN")}, 
                           {WM_DEADCHAR,    TEXT ("WM_DEADCHAR")}, 
                           {WM_SYSDEADCHAR, TEXT ("WM_SYSDEADCHAR")}}; 
// Message dispatch table for MainWindowProc 
const struct decodeUINT MainMessages[] = { 
    WM_CREATE, DoCreateMain, 
    WM_PAINT, DoPaintMain, 
    WM_KEYUP, DoKeysMain, 
    WM_KEYDOWN, DoKeysMain, 
    WM_CHAR, DoKeysMain, 
    WM_DEADCHAR, DoKeysMain, 
    WM_SYSCHAR, DoKeysMain, 
    WM_SYSDEADCHAR, DoKeysMain, 
    WM_SYSKEYDOWN, DoKeysMain, 
    WM_SYSKEYUP, DoKeysMain, 
    WM_DESTROY, DoDestroyMain, 
}; 
 
//====================================================================== 
// Program entry point 
// 
 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                    LPWSTR lpCmdLine, int nCmdShow) { 
    MSG msg; 
    int rc = 0; 
    HWND hwndMain; 
 
    // Initialize this instance. 
    hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow); 
    if (hwndMain == 0) 
        return 0x10; 
     
    // Application message loop 
    while (GetMessage (&msg, NULL, 0, 0)) { 
        TranslateMessage (&msg); 
        DispatchMessage (&msg); 
    } 
 
    // Instance cleanup 
    return TermInstance (hInstance, msg.wParam); 
} 
//---------------------------------------------------------------------- 
// InitInstance - Instance initialization 
// 
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) { 
    WNDCLASS wc; 
    HWND hWnd; 
 
#if defined(WIN32_PLATFORM_PSPC) 
    // If Pocket PC, allow only one instance of the application 
    hWnd = FindWindow (szAppName, NULL); 
    if (hWnd) { 
        SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));     
        return 0; 
    } 
#endif 
    hInst = hInstance;  // Save program instance handle 
 
    // Register application main window class. 
    wc.style = 0;                             // Window style 
    wc.lpfnWndProc = MainWndProc;             // Callback function 
    wc.cbClsExtra = 0;                        // Extra class data 
    wc.cbWndExtra = 0;                        // Extra window data 
    wc.hInstance = hInstance;                 // Owner handle 
    wc.hIcon = NULL,                          // Application icon 
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor 
    wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); 
    wc.lpszMenuName =  NULL;                  // Menu name 
    wc.lpszClassName = szAppName;             // Window class name 
 
    if (RegisterClass(&wc) == 0) return 0; 
     
    // Create main window. 
    hWnd = CreateWindowEx (WS_EX_NODRAG, szAppName, TEXT ("KeyTrac"), 
                      WS_VISIBLE | WS_CAPTION | WS_SYSMENU, 
                         CW_USEDEFAULT, CW_USEDEFAULT,  
                         CW_USEDEFAULT, CW_USEDEFAULT,  
                         NULL, NULL, hInstance, NULL);          
         
    // Fail if window not created 
    if (!IsWindow (hWnd)) return 0;  
 
    // Standard show and update calls 
    ShowWindow (hWnd, nCmdShow); 
    UpdateWindow (hWnd); 
    return hWnd; 
} 
//---------------------------------------------------------------------- 
// TermInstance - Program cleanup 
// 
int TermInstance (HINSTANCE hInstance, int nDefRC) { 
    return nDefRC; 
} 
//====================================================================== 
// Message handling procedures for MainWindow 
// 
//---------------------------------------------------------------------- 
// MainWndProc - Callback function for application window 
// 
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,  
                              LPARAM lParam) { 
    INT i; 
    // 
    // Search message list to see if we need to handle this 
    // message. If in list, call procedure. 
    // 
    for (i = 0; i < dim(MainMessages); i++) { 
        if (wMsg == MainMessages[i].Code) 
            return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam); 
    } 
    return DefWindowProc (hWnd, wMsg, wParam, lParam); 
} 
//---------------------------------------------------------------------- 
// DoCreateMain - Process WM_CREATE message for window. 
// 
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,  
                      LPARAM lParam) { 
    HDC hdc; 
    TEXTMETRIC tm; 
 
#if defined(WIN32_PLATFORM_PSPC) && (_WIN32_WCE >= 300) 
    SHMENUBARINFO mbi;                      // For Pocket PC, create 
    memset(&mbi, 0, sizeof(SHMENUBARINFO)); // menu bar so that we 
    mbi.cbSize = sizeof(SHMENUBARINFO);     // have a sip button 
    mbi.hwndParent = hWnd; 
    mbi.dwFlags = SHCMBF_EMPTYBAR;          // No menu 
    SHCreateMenuBar(&mbi); 
#endif 
 
    // Get the height of the default font. 
    hdc = GetDC (hWnd); 
    GetTextMetrics (hdc, &tm); 
    nFontHeight = tm.tmHeight + tm.tmExternalLeading; 
    ReleaseDC (hWnd, hdc); 
    return 0; 
} 
//---------------------------------------------------------------------- 
// DoPaintMain - Process WM_PAINT message for window. 
// 
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,  
                     LPARAM lParam) { 
    PAINTSTRUCT ps; 
    RECT rect, rectOut; 
    TCHAR szOut[256]; 
    HDC hdc; 
    INT i, j; 
    LPCTSTR pKeyText; 
 
    GetClientRect (hWnd, &rect); 
 
    // Create a drawing rectangle for the top line of the window. 
    rectOut = rect; 
    rectOut.bottom = rectOut.top + nFontHeight; 
 
    hdc = BeginPaint (hWnd, &ps);  
 
    if (nKeyCnt) { 
        for (i = 0; i < nKeyCnt; i++) { 
            // Create string containing wParam, lParam, and shift data. 
            wsprintf (szOut, TEXT ("wP:%08x lP:%08x shift: %s"),  
                      ka[i].wParam, ka[i].lParam, ka[i].szShift); 
 
            // Look up name of key message. 
            for (j = 0; j < dim (knArray); j++) 
                if (knArray[j].wMsg == ka[i].wKeyMsg) 
                    break; 
            // See if we found the message. 
            if (j < dim (knArray)) 
                pKeyText = knArray[j].pName; 
            else 
                pKeyText = TEXT ("Unknown"); 
            // Scroll the window one line. 
            ScrollDC (hdc, 0, nFontHeight, &rect, &rect, NULL, NULL); 
 
            // See if wide or narrow screen. 
            if (GetSystemMetrics (SM_CXSCREEN) < 480) { 
                // If Pocket PC, display info on 2 lines 
                ExtTextOut (hdc, 10, rect.top, ETO_OPAQUE, &rectOut, 
                            szOut, lstrlen (szOut), NULL); 
 
                // Scroll the window another line. 
                ScrollDC(hdc, 0, nFontHeight, &rect, &rect, NULL, NULL); 
                ExtTextOut (hdc, 5, rect.top, ETO_OPAQUE, &rectOut, 
                            pKeyText, lstrlen (pKeyText), NULL); 
            } else { 
                // Wide screen, print all on one line. 
                ExtTextOut (hdc, 5, rect.top, ETO_OPAQUE, &rectOut, 
                            pKeyText, lstrlen (pKeyText), NULL); 
                ExtTextOut (hdc, 100, rect.top, 0, NULL, 
                            szOut, lstrlen (szOut), NULL); 
            } 
        } 
        nKeyCnt = 0; 
    } 
    EndPaint (hWnd, &ps);  
    return 0; 
} 
//---------------------------------------------------------------------- 
// DoKeysMain - Process all keyboard messages for window. 
// 
LRESULT DoKeysMain (HWND hWnd, UINT wMsg, WPARAM wParam,  
                    LPARAM lParam) { 
 
    if (nKeyCnt >= 16) return 0; 
 
    ka[nKeyCnt].wKeyMsg = wMsg; 
    ka[nKeyCnt].wParam = wParam; 
    ka[nKeyCnt].lParam = lParam; 
 
    // Capture the state of the shift flags. 
    ka[nKeyCnt].szShift[0] = TEXT ('\0'); 
    if (GetKeyState (VK_LMENU))  
        lstrcat (ka[nKeyCnt].szShift, TEXT ("lA ")); 
    if (GetKeyState (VK_RMENU))  
        lstrcat (ka[nKeyCnt].szShift, TEXT ("rA ")); 
    if (GetKeyState (VK_MENU))  
        lstrcat (ka[nKeyCnt].szShift, TEXT ("A ")); 
    if (GetKeyState (VK_LCONTROL)) 
        lstrcat (ka[nKeyCnt].szShift, TEXT ("lC ")); 
    if (GetKeyState (VK_RCONTROL)) 
        lstrcat (ka[nKeyCnt].szShift, TEXT ("rC ")); 
    if (GetKeyState (VK_CONTROL)) 
        lstrcat (ka[nKeyCnt].szShift, TEXT ("C ")); 
 
    if (GetKeyState (VK_LSHIFT)) 
        lstrcat (ka[nKeyCnt].szShift, TEXT ("lS ")); 
    if (GetKeyState (VK_RSHIFT)) 
        lstrcat (ka[nKeyCnt].szShift, TEXT ("rS ")); 
    if (GetKeyState (VK_SHIFT)) 
        lstrcat (ka[nKeyCnt].szShift, TEXT ("S ")); 
 
    nKeyCnt++; 
    InvalidateRect (hWnd, NULL, FALSE); 
    return 0; 
} 
//---------------------------------------------------------------------- 
// DoDestroyMain - Process WM_DESTROY message for window. 
// 
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                       LPARAM lParam) { 
    PostQuitMessage (0); 
    return 0; 
}