Windows程序设计实验---KeyBoard
程序员文章站
2022-05-26 16:14:15
...
实验描述:
Design a program to realize the application of keyboard.
- When the Ctrl key is held on, ellipse will be drawn; When the Shift key is held on, rectangle will be drawn. (按下Ctrl键- - -画圆,按下Shift键- - -画矩形)
- And then when press the right(left) arrow on the keyboard, the width(height) of the ellipse or the rectangle will be increased by 10; when press Home(End) key, the ellipse or rectangle move to left(right); When press PageUp(PageDown) key, the ellipse or rectangle move to up(down). (通过 ↑ ↓ ← → 调整所画图形大小,通过PgUp PgDn Home End 调整图形位置)
源代码如下:
#include <windows.h>
#include <stdlib.h>
#include <string.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
RECT rect;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("DEMO");
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_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT("This program requires Window NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT("DEMO"),
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 msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static BOOL bCircle = FALSE, bRect = FALSE;
switch (message)
{
case WM_KEYDOWN:
if(wParam == VK_CONTROL)
{
bCircle = TRUE;
bRect = FALSE;
rect.left = 0;
rect.right = 0;
rect.top = 0;
rect.bottom = 0;
}
else if(wParam == VK_SHIFT)
{
bRect = TRUE;
bCircle = FALSE;
rect.left = 0;
rect.right = 0;
rect.top = 0;
rect.bottom = 0;
}
else if(wParam == VK_RIGHT)
{
rect.right += 10;
if(bRect == TRUE || bCircle == TRUE)
InvalidateRect(hwnd, NULL, TRUE);
}
else if(wParam == VK_DOWN)
{
rect.bottom += 10;
if(bRect == TRUE || bCircle == TRUE)
InvalidateRect(hwnd, NULL, TRUE);
}
else if(wParam == VK_PRIOR)
{
rect.top -= 10;
rect.bottom -= 10;
if(bRect == TRUE || bCircle == TRUE)
InvalidateRect(hwnd, NULL, TRUE);
}
else if(wParam == VK_NEXT)
{
rect.top += 10;
rect.bottom += 10;
if(bRect == TRUE || bCircle == TRUE)
InvalidateRect(hwnd, NULL, TRUE);
}
else if(wParam == VK_HOME)
{
rect.left -= 10;
rect.right -= 10;
if(bRect == TRUE || bCircle == TRUE)
InvalidateRect(hwnd, NULL, TRUE);
}
else if(wParam == VK_END)
{
rect.left += 10;
rect.right += 10;
if(bRect == TRUE || bCircle == TRUE)
InvalidateRect(hwnd, NULL, TRUE);
}
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
if(bCircle == TRUE)
Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);
if(bRect == TRUE)
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
EndPaint (hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
结果如图(结果会延迟出现):
画椭圆(按下Ctrl键后,开始画椭圆):
画矩形(按下Shift键后,开始画矩形):