键盘消息演示

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow
)
{
MSG Msg;
HWND hwnd;
WNDCLASS wndclass;
char lpszClassName[] = "窗体";
//是这个窗口的类名wndclass,不是窗口左上角的那个
char lpszTitle[] = "My Window";
HBRUSH hbrush;
hbrush = CreateSolidBrush(RGB(0, 255, 0));//定义绿色画刷

wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = hbrush;
//空值背景颜色,可用自定义画刷来调,绿色背景
wndclass.hInstance = hInstance;
wndclass.lpszClassName = lpszClassName;
wndclass.lpszMenuName = NULL;

if (!RegisterClass(&wndclass)) {
MessageBeep(0);
return 0;
}

hwnd = CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);

ShowWindow(hwnd, nCmdShow);
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)
{
//定义区
HDC hdc;
PAINTSTRUCT ps;
HPEN hpen;
HBRUSH hbrush;

//定义以下输出字符串
static char cUp[] = "You had hit the UP key";
static char cCtrl[] = "You had hit the Ctrl key";
static char cShift[] = "You had hit the Shift key";
static char cCtrl_A[] = "You had hit the Ctrl_A key";
static char cShift_B[] = "You had hit the Shift_B key";

//定义并初始化按键标志变量
static BOOL nUpKeyDown = FALSE,
nShiftKeyDown = FALSE,
nCtrlKeyDown = FALSE,
nCtrlAKeyDown = FALSE,
nShiftBKeyDown = FALSE;

//处理区
switch (message)
{
case WM_KEYDOWN:
{
//判断是否按下up,shift,ctrl键
switch (wParam)
{
case VK_UP:
nUpKeyDown = TRUE;
break;
case VK_CONTROL:
nCtrlKeyDown = TRUE;
break;
case VK_SHIFT:
nShiftKeyDown = TRUE;
break;
default:
break;
}
}
case WM_CHAR:
// 为了判断特殊按键和普通案件的组合情况
if (wParam == 65 || wParam == 97)
{
if (nCtrlKeyDown == TRUE)
{
nCtrlKeyDown = FALSE;
nCtrlAKeyDown = TRUE;
}
}
if (wParam == 98 || wParam == 66)
{
if (nShiftKeyDown == TRUE)
{
nShiftKeyDown = FALSE;
nShiftBKeyDown = TRUE;
}
}
break;
case WM_KEYUP:
InvalidateRect(hwnd, NULL, FALSE);
//刷新上一次用户去的RECT
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
hpen = (HPEN)GetStockObject(WHITE_PEN);
SelectObject(hdc, hbrush);
SelectObject(hdc, hpen);
SetTextColor(hdc, RGB(255, 0, 0));

//输出信息
if (nUpKeyDown == TRUE)
{
Rectangle(hdc, 0, 0, 300, 200);
TextOut(hdc, 0, 0, cUp, strlen(cUp));
nUpKeyDown = FALSE;
}
else if (nCtrlAKeyDown == TRUE)
{
Rectangle(hdc, 0, 0, 300, 200);
TextOut(hdc, 0, 100, cCtrl_A, strlen(cCtrl_A));
nCtrlAKeyDown = FALSE;
nCtrlKeyDown = FALSE;
}
else if (nCtrlKeyDown == TRUE)
{
Rectangle(hdc, 0, 0, 300, 200);
TextOut(hdc, 0, 0, cCtrl, strlen(cCtrl));
nCtrlKeyDown = FALSE;
}
else if (nShiftBKeyDown == TRUE)
{
Rectangle(hdc, 0, 0, 300, 200);
TextOut(hdc, 0, 0, cShift_B, strlen(cShift_B));
nShiftBKeyDown = FALSE;
nShiftKeyDown = FALSE;
}
else if (nShiftKeyDown == TRUE)
{
Rectangle(hdc, 0, 0, 300, 200);
TextOut(hdc, 0, 0, cShift, strlen(cShift));
nShiftKeyDown = FALSE;
}
//删除画笔画刷
DeleteObject(hpen);
DeleteObject(hbrush);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
//break很重要,没有的话窗口会址闪一下,因为直接执行完
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}

原文地址:https://www.cnblogs.com/nanfengnan/p/13734386.html