设计一个窗口,在该窗口中练习键盘的响应,要求如下:
(1) 单击键盘上的向上箭头时,窗口中显示“You had hitted the up key”
(2) 单击<Shift>键时,窗口中显示“You had hitted the SHIFT key”
(3) 单击<Ctrl>键时,窗口中显示“You had hitted the CTRL key”
(4) 单击<Ctrl+A>键时,窗口中显示“You had hitted the CTRL A key”
(5) 单击<Shift+B>键时,窗口中显示“You had hitted the SHIFT B key”
(6) 按下其它键时,清空屏幕
#include<windows.h>
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);
HWND hWndMain;
//主函数
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG Message;
if (!InitWindowsClass(hInstance))
return FALSE;
if (!InitWindows(hInstance, nCmdShow))
return FALSE;
while (GetMessage(&Message, 0, 0, 0)) //消息循环
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)
{
HDC hDC; //定义设备环境句柄.
PAINTSTRUCT ps; //定义包含绘图信息的结构体变量
HPEN hPen; //定义画笔句柄。
HBRUSH hBrush; //定义画刷句柄
//以下定义输出的字符串
static char cUp[] = "You had hitted the UP key";
static char cCtrl[] = "You had hitted the Ctrl key";
static char cShift[] = "You had hitted the SHIFT key";
static char cCtrl_A[] = "You had hitted the CTRL A key";
static char cShift_B[] = "You had hitted the SHIFT B key";
//下面定义并初始化按键标志变量。
static BOOL nUpKeyDown = FALSE,
nShiftKeyDown = FALSE,
nCtrlKeyDown = FALSE,
nCtrlAKeyDown = FALSE,
nShiftBKeyDown = FALSE,
elseKeyDown = FALSE,
elseChar = FALSE;
switch (iMessage)
{
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_UP: //当按上箭头键时,变量置为真。
nUpKeyDown = TRUE;
break;
case VK_SHIFT: //当按shift键时,变量置为真。
nShiftKeyDown = TRUE;
break;
case VK_CONTROL: //当按control键时,变量置为真
nCtrlKeyDown = TRUE;
break;
default:
elseKeyDown = TRUE;
break;
}
}
break;
case WM_KEYUP:
InvalidateRect(hWnd, NULL, FALSE); //产生WM_PAINT消息
break;
case WM_CHAR:
if (wParam == (65 & VK_CONTROL)) //当按下Ctrl+a键时
{
if (nCtrlKeyDown == TRUE)
{
nCtrlAKeyDown = TRUE;
nCtrlKeyDown = FALSE;
}
}
else if (wParam == 98 || wParam == 66) //当按下b键时
{
if (nShiftKeyDown == TRUE) //检查shift键是否处于按下状态。
{
nShiftBKeyDown = TRUE; //当SHIFT键按下时,变量置为真
nShiftKeyDown = FALSE;
}
}
else
{
elseChar = TRUE;
}
break;
case WM_PAINT: //处理绘图消息.
hDC = BeginPaint(hWnd, &ps);
hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH); //创建 白画刷。
hPen = (HPEN)GetStockObject(WHITE_PEN); //创建白画笔。
SelectObject(hDC, hPen); //选入白画刷
SelectObject(hDC, hBrush); //选入白画笔
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;
}
else if (nCtrlKeyDown == TRUE&&nCtrlAKeyDown == FALSE)
{
Rectangle(hDC, 0, 0, 300, 200);
TextOut(hDC, 0, 60, 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 (nShiftBKeyDown == FALSE&&nShiftKeyDown == TRUE)
{
Rectangle(hDC, 0, 0, 300, 200);
TextOut(hDC, 0, 0, cShift, strlen(cShift));
nShiftKeyDown = FALSE;
}
else if (elseChar == TRUE || elseKeyDown == TRUE)
{
Rectangle(hDC, 0, 0, 300, 200);
elseChar = FALSE;
elseKeyDown == FALSE;
break;
}
//删除画笔和画刷
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0); return 0;
default:
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow) // 初始化窗口
{
HWND hWnd;
hWnd = CreateWindow("WinFill", "练习六", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); // 生成窗口
if (!hWnd)
return FALSE;
hWndMain = hWnd;
ShowWindow(hWnd, nCmdShow); //显示窗口
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance) //定义窗口类
{
WNDCLASS WndClass;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, "END");
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = "WinFill";
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
return RegisterClass(&WndClass);
}
编写一个鼠标应用程序,按下鼠标左键在窗口中移动时,将按下左键时所在点和当前点所形成的矩形涂成灰色,开始光标为十字型,拉伸过程中将光标设置为沙漏型。然后,若双击鼠标的左键,则将前面所绘制的矩形拉伸到整个窗口。若单击鼠标的右键,则灰色消失,窗口恢复到初始状态,光标恢复为十字型。
#include<windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);
HWND hWndMain;
//主函数
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG Message;
if (!InitWindowsClass(hInstance))
return FALSE;
if (!InitWindows(hInstance, nCmdShow))
return FALSE;
while (GetMessage(&Message, 0, 0, 0)) //消息循环
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL operate = FALSE, ready = TRUE;//operate为按下左键标识,ready为绘制初始图形标识
static POINT BeginP, EndP;
static RECT rect = { 0,0,0,0 };
HDC hdc;
PAINTSTRUCT PtStr;
HBRUSH hBrush;
switch (message)
{
case WM_LBUTTONDOWN:
if ((!operate) && ready)
{
operate = TRUE; //左键击活俘获
ready = FALSE;
SetCapture(hwnd); //把所有的鼠标信息输入到被左键击活的窗口.
SetCursor(LoadCursor(NULL, IDC_CROSS)); //载入光标
BeginP.x = LOWORD(lParam);
BeginP.y = HIWORD(lParam);
}
return 0;
case WM_MOUSEMOVE:
if (operate)
{
EndP.x = LOWORD(lParam);
EndP.y = HIWORD(lParam);
rect.left = BeginP.x < EndP.x ? BeginP.x : EndP.x;
rect.right = BeginP.x > EndP.x ? BeginP.x : EndP.x;
rect.top = BeginP.y < EndP.y ? BeginP.y : EndP.y;
rect.bottom = BeginP.y > EndP.y ? BeginP.y : EndP.y;
SetCursor(LoadCursor(NULL, IDC_WAIT)); //载入沙漏光标
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_LBUTTONUP:
operate = FALSE;
SetCursor(LoadCursor(NULL, IDC_ARROW));
ReleaseCapture(); //把鼠标从当前窗口中释放出来
return 0;
case WM_PAINT:
if (ready == FALSE) //非初始状态,曾经按下了左键
{
hdc = BeginPaint(hwnd, &PtStr);
hBrush = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
SelectObject(hdc, hBrush);
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
DeleteObject(hBrush);
EndPaint(hwnd, &PtStr);
}
else //初始状态,或者用单击右键恢复到初始状态
{
hdc = BeginPaint(hwnd, &PtStr);
GetClientRect(hwnd, &rect);
//使用当前缺省画笔、画刷进行绘图
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
EndPaint(hwnd, &PtStr);
}
return 0;
case WM_LBUTTONDBLCLK:
GetClientRect(hwnd, &rect);//设置绘图区域为整个用户区
InvalidateRect(hwnd, NULL, TRUE);
SetCursor(LoadCursor(NULL, IDC_ARROW));
ReleaseCapture(); //把鼠标从当前窗口中释放出来
return 0;
case WM_RBUTTONDOWN:
if (ready == FALSE)
{
ready = TRUE;
InvalidateRect(hwnd, NULL, TRUE);
SetCursor(LoadCursor(NULL, IDC_ARROW));
ReleaseCapture(); //把鼠标从当前窗口中释放出来
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow) // 初始化窗口
{
HWND hWnd;
hWnd = CreateWindow("WinFill", "练习六(2)", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); // 生成窗口
if (!hWnd)
return FALSE;
hWndMain = hWnd;
ShowWindow(hWnd, nCmdShow); //显示窗口
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance) //定义窗口类
{
WNDCLASS WndClass;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, "END");
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = "WinFill";
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
return RegisterClass(&WndClass);
}