1.鼠标中键按下滚动缩放的实现
添加一个OnMouseWheel消息句柄:
BOOL CRenderView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
if(nFlags&MK_MBUTTON)
{
if(zDelta>0)
{
m_xScaling+=0.1f;
m_yScaling+=0.1f;
m_zScaling+=0.1f;
}
else
{
m_xScaling-=0.1f;
m_yScaling-=0.1f;
m_zScaling-=0.1f;
}
InvalidateRect(NULL,FALSE);
}
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
然后在OnPaint()里面加上glScalef(m_xScaling,m_yScaling,m_zScaling);
void CRenderView::OnPaint()
{
CPaintDC dc(this); // device context for painting
wglMakeCurrent(m_hDC,m_hRC);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//平移、旋转、缩放变换
//glTranslated(m_xTranslation,m_yTranslation,0.0f);平移
//glRotatef(m_xRotation,1.0f,0.0f,0.0f); 旋转
//glRotatef(m_yRotation,0.0f,1.0f,0.0f);
glScalef(m_xScaling,m_yScaling,m_zScaling); 缩放
RenderScene(); //绘图
glPopMatrix();
SwapBuffers(m_hDC); //将后台视频缓存拷贝到前台
wglMakeCurrent(m_hDC,NULL);
}
/
2.鼠标右键按下平移的实现:
添加OnRButtonDown消息响应
void CRenderView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_RightButtonDown=TRUE;
Pos1=point;
CView::OnRButtonDown(nFlags, point);
}
添加OnRButtonUp消息响应
void CRenderView::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_RightButtonDown=FALSE;
CView::OnRButtonUp(nFlags, point);
}
添加OnMouseMove消息响应:
void CRenderView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_RightButtonDown)
{
m_xTranslation=(float)(point.x - Pos1.x)*2.0f;
m_yTranslation=-(float)(point.y - Pos1.y)*2.0f;
InvalidateRect(NULL,FALSE);
}
CView::OnMouseMove(nFlags, point);
}
然后在Onpaint()里面添加:glTranslated(m_xTranslation,m_yTranslation,0.0f);即可
转载于:https://blog.51cto.com/shanzhizi/939092