📄 keytrac.c
字号:
//======================================================================
// KeyTrac - displays keyboard messages
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commctrl.h> // Command bar includes
#include "keytrac.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("KeyTrac");
HINSTANCE hInst; // Program instance handle
// Program-specific global data
KEYARRAY ka[16];
UINT wKeyMsg = 0;
INT nKeyCnt = 0, nFontHeight;
TCHAR szMsgTxt[64];
// 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 application.
rc = InitApp (hInstance);
if (rc) return rc;
// 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);
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
// 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 = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass(&wc) == 0) return 1;
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT("KeyTrac"), // Window title
WS_VISIBLE, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial Width
CW_USEDEFAULT, // Initial Height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // App instance
NULL); // Pointer to create
// parameters
// Return fail code 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) {
HWND hwndCB;
HDC hdc;
TEXTMETRIC tm;
// Create a command bar.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
// 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;
// Adjust the size of the client rect to take into account
// the command bar height.
GetClientRect (hWnd, &rect);
rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
// Create a drawing rectangle for the bottom line of the window.
rectOut = rect;
rectOut.top = rectOut.bottom - nFontHeight;
hdc = BeginPaint (hWnd, &ps);
if (nKeyCnt) {
for (i = 0; i < nKeyCnt; i++) {
// Scroll window up by one line.
ScrollDC (hdc, 0, -nFontHeight, &rect, &rect, NULL, NULL);
// Write key name, use opaque mode to erase background.
ExtTextOut (hdc, 5, rect.bottom - nFontHeight, ETO_OPAQUE,
&rectOut, ka[i].szMsgTxt,
lstrlen (ka[i].szMsgTxt), NULL);
// Write key variables.
wsprintf (szOut, TEXT ("wParam:%08x lParam:%08x shift: "),
ka[i].wParam, ka[i].lParam);
if (ka[i].wFlags & FLAG_LMENU)
lstrcat (szOut, TEXT ("lA "));
if (ka[i].wFlags & FLAG_RMENU)
lstrcat (szOut, TEXT ("rA "));
if (ka[i].wFlags & FLAG_LCONTROL)
lstrcat (szOut, TEXT ("lC "));
if (ka[i].wFlags & FLAG_RCONTROL)
lstrcat (szOut, TEXT ("rC "));
if (ka[i].wFlags & FLAG_LSHIFT)
lstrcat (szOut, TEXT ("lS "));
if (ka[i].wFlags & FLAG_RSHIFT)
lstrcat (szOut, TEXT ("rS "));
ExtTextOut (hdc, 125, rect.bottom - nFontHeight, 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;
switch (wMsg) {
case WM_KEYUP:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_KEYUP"));
break;
case WM_KEYDOWN:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_KEYDOWN"));
break;
case WM_CHAR:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_CHAR"));
break;
case WM_DEADCHAR:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_DEADCHAR"));
break;
case WM_SYSCHAR:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_SYSCHAR"));
break;
case WM_SYSDEADCHAR:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_SYSDEADCHAR"));
break;
case WM_SYSKEYDOWN:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_SYSKEYDOWN"));
break;
case WM_SYSKEYUP:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("WM_SYSKEYUP"));
break;
default:
lstrcpy (ka[nKeyCnt].szMsgTxt, TEXT ("unknown"));
break;
}
ka[nKeyCnt].wKeyMsg = wMsg;
ka[nKeyCnt].wParam = wParam;
ka[nKeyCnt].lParam = lParam;
// Capture the state of the shift flags.
ka[nKeyCnt].wFlags = 0;
if (GetKeyState (VK_LMENU))
ka[nKeyCnt].wFlags |= FLAG_LMENU;
if (GetKeyState (VK_RMENU))
ka[nKeyCnt].wFlags |= FLAG_RMENU;
if (GetKeyState (VK_LCONTROL))
ka[nKeyCnt].wFlags |= FLAG_LCONTROL;
if (GetKeyState (VK_RCONTROL))
ka[nKeyCnt].wFlags |= FLAG_RCONTROL;
if (GetKeyState (VK_LSHIFT))
ka[nKeyCnt].wFlags |= FLAG_LSHIFT;
if (GetKeyState (VK_RSHIFT))
ka[nKeyCnt].wFlags |= FLAG_RSHIFT;
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -