MFC鼠标事件、键盘事件、自定义消息与运动小球实现
程序员文章站
2022-06-11 21:05:14
...
整体效果展示
注:鼠标点击时的黄色小球为录屏软件自带的
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);
}
上一篇: 怎么选择打印机?打印机的选购指南