⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keydemo.cpp

📁 c++从入门到精通
💻 CPP
字号:

#include <windows.h>
#include <stdlib.h>
#include <string.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;				//定义画笔句柄。

	switch(iMessage)
	{
	case WM_KEYDOWN:
		{
			switch(wParam)
			{
			case VK_UP://当按上箭头键时,弹出一个MessageBox对话框,标识Up键按下
				MessageBox(NULL,TEXT("You had hitted the Up key"),
						TEXT("MessageBox"),0);
				break;
			case VK_DOWN://当按下箭头键时,弹出一个MessageBox对话框,标识Down键按下
				MessageBox(NULL,TEXT("You had hitted the Down key"),
					TEXT("MessageBox"),0);
				break;
			case VK_LEFT://当按左箭头键时,弹出一个MessageBox对话框,标识Left键按下
				MessageBox(NULL,TEXT("You had hitted the Left key"),
					TEXT("MessageBox"),0);
				break;
			case VK_RIGHT://当按右箭头键时,弹出一个MessageBox对话框,标识RIGHT键按下
				MessageBox(NULL,TEXT("You had hitted the Right key"),
					TEXT("MessageBox"),0);
				break;
			case VK_SHIFT://当按shift键时,弹出一个MessageBox对话框,标识Shift键按下
				MessageBox(NULL,TEXT("You had hitted the shift key"),
					TEXT("MessageBox"),0);
				break;
			case VK_CONTROL://当按control键时,弹出一个MessageBox对话框,标识Ctrl键按下
				MessageBox(NULL,TEXT("You had hitted the Ctrl key"),
					TEXT("MessageBox"),0);
				break;
			default:
				break;
			}
		}
		break;
	case WM_KEYUP:
		InvalidateRect(hWnd,NULL,FALSE);//刷新用户区。
		break;
	case WM_CHAR:
		if((wParam>=48)&&(wParam<=57))	
		{
			MessageBox(NULL,TEXT("You had hitted the Number key"),
				TEXT("MessageBox"),0);
			
		}
		else if((wParam>=65)&&(wParam<=90))//当按下A~Z键时
		{
			MessageBox(NULL,TEXT("You had hitted the A~Z key"),
				TEXT("MessageBox"),0);
		}
		else if((wParam>=96)&&(wParam<=122))//当按下a~z键时
		{
			MessageBox(NULL,TEXT("You had hitted the a~z key"),
				TEXT("MessageBox"),0);
		}
		break;
	case WM_PAINT://处理绘图消息.
		hDC=BeginPaint(hWnd,&ps);
				 
		hPen=CreatePen//创建新画笔
		(  
		 PS_SOLID,  //确定画笔的样式
	 	 3, 		//画笔的宽度,取3表示3个象素宽
		 RGB(0,0,0) //画笔的颜色为蓝色
		);
		HFONT hFont;
		hFont=CreateFont(//定义字体句柄.
			30,//字体高度.
			20,	//宽度.
			0,  //文本倾斜度为0,表示水平.
			0,  //字体倾斜度为0.
			400,//字体粗度.400为正常.
			0,  //是斜体字?,
			0,  //无下划线.
			0,	//无删除线.
			ANSI_CHARSET,//所用的字符集为ANSI_CHARSET.
			OUT_DEFAULT_PRECIS,//删除精度为缺省值.
			CLIP_DEFAULT_PRECIS,//裁剪精度为缺省值.
			DEFAULT_QUALITY,//输出质量为缺省值.
			DEFAULT_PITCH|FF_DONTCARE,//字间距和字体系列使用缺省值.
			"Arial");//字体名称.
		SelectObject(hDC,hFont);
		SelectObject(hDC,hPen);	//选入白画刷
		SetTextColor(hDC,RGB(255,0,0));//设置字体颜色为红色。
		//在客户区上方的输出一行文本
		TextOut(hDC,200,60,"键盘消息测试小程序!",19);
		//删除画笔和自定义字体
		DeleteObject(hPen);
		DeleteObject(hFont);
		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("keytest",
					"键盘测试小程序",
					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="keytest";
	WndClass.lpszMenuName=NULL;
	WndClass.style=CS_HREDRAW|CS_VREDRAW;
	return RegisterClass(&WndClass);
	WndClass.style=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -