MFC鼠标事件、键盘事件、自定义消息与运动小球实现

整体效果展示

注:鼠标点击时的黄色小球为录屏软件自带的

在这里插入图片描述

1、编程实现测试程序,要求实现

(1)按下键盘任意键,屏幕显示按键信息,包括任意2个特殊键

特殊键为“PgUp”, “F1”, “F2”, “F3”, “F4”, “F5”, “F6”, “F7”, “F8”, “F9”, “F10”, “F11”, “F12”

运行效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

代码
// 响应非特殊键
void CMFCApplication1View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	// 63-------?
	if (nChar == 63)
		SendMessage(WM_MY_MESSAGE, 50, 100);
	else {
		CString str;
		str.Format(L"%c Key Entered", nChar);
		MessageBox(str);
	}
	CView::OnChar(nChar, nRepCnt, nFlags);
}

// 响应特殊键
BOOL CMFCApplication1View::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN) {
		if (pMsg->wParam == VK_UP)
			MessageBox(L"up key Entered");
		else if (pMsg->wParam == VK_F1)
			MessageBox(L"F1 key Entered");
		else if (pMsg->wParam == VK_F2)
			MessageBox(L"F2 key Entered");
		else if (pMsg->wParam == VK_F3)
			MessageBox(L"F3 key Entered");
		else if (pMsg->wParam == VK_F4)
			MessageBox(L"F4 key Entered");
		else if (pMsg->wParam == VK_F5)
			MessageBox(L"F5 key Entered");
		else if (pMsg->wParam == VK_F6)
			MessageBox(L"F6 key Entered");
		else if (pMsg->wParam == VK_F7)
			MessageBox(L"F7 key Entered");
		else if (pMsg->wParam == VK_F8)
			MessageBox(L"F8 key Entered");
		else if (pMsg->wParam == VK_F9)
			MessageBox(L"F9 key Entered");
		else if (pMsg->wParam == VK_F10)
			MessageBox(L"F10 key Entered");
		else if (pMsg->wParam == VK_F11)
			MessageBox(L"F11 key Entered");
		else if (pMsg->wParam == VK_F12)
			MessageBox(L"F12 key Entered");
	}
	return CView::PreTranslateMessage(pMsg);
}

(2)单击鼠标左键,屏幕显示鼠标信息

说明:

单击鼠标左键后,屏幕显示鼠标位置信息,

松开后,自动刷新客户区

注:黄色的圆为录频软件自带的

运行效果

在这里插入图片描述

代码
void CMFCApplication1View::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CDC *pDC = GetDC();
	CString str;
	str.Format(L"you click here(%d,%d)", point.x, point.y);
	pDC->TextOutW(point.x, point.y,str);
	ReleaseDC(pDC);
	CView::OnLButtonDown(nFlags, point);
}



void CMFCApplication1View::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	// 重新绘制客户区
	CDC *pDC = GetDC();
	CRect rect;
	GetClientRect(&rect);
	CBrush brush;
	brush.CreateSolidBrush(RGB(255, 255, 255));
	pDC->SelectObject(&brush);
	pDC->Rectangle(rect);
	CView::OnLButtonUp(nFlags, point);
}
(3)自定义WM_MY_ MESSAGE消息,带50和100两个参数,由“?”键激活,屏幕显示相应信息
在这里插入图片描述
代码
// 在Message MAp处的定义
ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)

// 发送消息
void CMFCApplication1View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	// 63-------?
	if (nChar == 63)
		SendMessage(WM_MY_MESSAGE, 50, 100);
	else {
		CString str;
		str.Format(L"%c Key Entered", nChar);
		MessageBox(str);
	}
	CView::OnChar(nChar, nRepCnt, nFlags);
}
// 消息处理
LRESULT CMFCApplication1View::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
	// TODO: 在此处添加实现代码.
	CString str;
	str.Format(L"Message Param is %d and %d", wParam, lParam);
	MessageBox(str);
	return LRESULT();
}

2、编程实现小球程序,要求实现

(1)小球从左上角向右下方沿45度轨迹移动
(2)在客户区的4个边界处实现小球反弹
(2)增加两个二级菜单,实现小球移动的启动与停止

增加了小球的颜色随机变换功能

运行效果

在这里插入图片描述

核心代码
bool flage = 0;
// 颜色存储
vector<vector<int>>  rgb_store;
void CMFCApplication1View::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值


	if (nIDEvent == 1) {
		CDC *pDC = GetDC();
		CRect rect;
		GetClientRect(&rect);
		if (flage == 0)
		{
			vector<int> vector_rgb2;
			vector_rgb2.push_back(0);
			vector_rgb2.push_back(0);
			vector_rgb2.push_back(0);
			rgb_store.push_back(vector_rgb2);
		}

		vector<int> vector_rgb;

		int red = rand() % 255;
		int green = rand() % 255;
		int blue = rand() % 255;

		vector_rgb.push_back(red);
		vector_rgb.push_back(green);
		vector_rgb.push_back(blue);
		// 更新颜色存储器中的颜色
		if (flage == 0)
		{
			rgb_store.push_back(vector_rgb);
			flage = 1;
		}
		else {
			rgb_store[0] = rgb_store[1];
			rgb_store[1] = vector_rgb;
		}

		// 擦除上一次画的小球,和上一次的颜色相同
		int red2 = rgb_store[0][0];
		int green2 = rgb_store[0][1];
		int blue2 = rgb_store[0][2];
		CBrush brush2;
		brush2.CreateSolidBrush(RGB(red2, green2, blue2));
		pDC->SelectObject(&brush2);
		pDC->SetROP2(R2_XORPEN);
		pDC->Ellipse(x,y, x+ 15, y+ 15);
		if (y<0 || y>rect.bottom - 15)
			moveY = 0 - moveY;
		if (x<0 || x>rect.right - 15)
			moveX = 0 - moveX;
		x += moveX;
		y += moveY;
		// 画小球
		CBrush brush;
		brush.CreateSolidBrush(RGB(red, green, blue));
		pDC->SelectObject(&brush);
		pDC->Ellipse(x, y, x + 15, y + 15);
	}
	
	CView::OnTimer(nIDEvent);
}

void CMFCApplication1View::OnSmallballPause()
{
	// TODO: 在此添加命令处理程序代码
	KillTimer(1);
}


void CMFCApplication1View::OnSmallballBegin()
{
	// TODO: 在此添加命令处理程序代码
	SetTimer(1, 50, nullptr);
}
  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Joshua_yi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值