在上一篇的基础上加入对键盘和鼠标的事件处理程序,以便用其来控制3D物体的旋转和移动。
1,首先在CCY457OpenGLView类中为WM_KEYDOWN, WM_LBUTTONDOWN, WM_LBUTTONUP 和 WM_MOUSEMOVE四个事件加入事件处理函数。
2,在CCY457OpenGLView.h中加入下列用于控制旋转和移动的变量:
CCY457OpenGLView::CCY457OpenGLView()
void COpenGLView::RenderScene ()
glTranslatef(m_xPos, m_yPos, -5.0f);
glRotatef(m_xAngle, 1.0f,0.0f,0.0f);
glRotatef(m_yAngle, 0.0f,1.0f,0.0f);
void COpenGLView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
// TODO: Add your message handler code here and/or call default
case VK_UP: m_yPos = m_yPos + 0.1f;
case VK_DOWN: m_yPos = m_yPos - 0.1f;
case VK_LEFT: m_xPos = m_xPos - 0.1f;
case VK_RIGHT: m_xPos = m_xPos + 0.1f;
default: MessageBox("Press the arrow keys only");
InvalidateRect(NULL,FALSE);
CView::OnKeyDown(nChar, nRepCnt, nFlags);
void COpenGLView::OnLButtonDown(UINT nFlags, CPoint point)
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
void COpenGLView::OnLButtonUp(UINT nFlags, CPoint point)
// TODO: Add your message handler code here and/or call default
m_MouseDownPoint=CPoint(0,0);
CView::OnLButtonUp(nFlags, point);
void COpenGLView::OnMouseMove(UINT nFlags, CPoint point)
// TODO: Add your message handler code here and/or call default
// Check if we have captured the mouse
//Increment the object rotation angles
m_xAngle+=(point.y-m_MouseDownPoint.y)/3.6;
m_yAngle+=(point.x-m_MouseDownPoint.x)/3.6;
InvalidateRect(NULL,FALSE);
CView::OnMouseMove(nFlags, point);
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/05/1327443.html,如需转载请自行联系原作者