Windows程序设计实验---KeyBoard

实验描述:
Design a program to realize the application of keyboard.

  1. When the Ctrl key is held on, ellipse will be drawn; When the Shift key is held on, rectangle will be drawn. (按下Ctrl键- - -画圆,按下Shift键- - -画矩形)
  2. 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键后,开始画矩形):
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃不起饭的小陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值