上一篇文章中的视频展示了模型的旋转、平移和缩放的运行结果图。下面是具体的实现:
首先在*****View.h中定义一下成员变量:
protected:
//三维场景转换模式
enum CurrentAction3d
{
CurAction3d_Nothing,
CurAction3d_DynamicPanning, //平移
CurAction3d_DynamicZooming, //缩放
CurAction3d_DynamicRotation //旋转
};
Standard_Integer m_x_max; //记录鼠标平移坐标X
Standard_Integer m_y_max; //记录鼠标平移坐标Y
float m_scale; //记录滚轮缩放比例
CurrentAction3d m_current_mode; //三维场景转换模式(平移\缩放\旋转)
bool leftMouseBtn = false; //记录鼠标左键状态
bool midMouseBtn = false; //记录鼠标中键状态
CPoint mouseDownPT; //控制旋转角度
CPoint m_Pt;
接着在*****View的类向导中添加鼠标消息响应:
添加完成之后,会出现六个成员函数:
public:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMButtonUp(UINT nFlags, CPoint point);
至此,成员变量和成员函数添加完毕。
接下来进行变量初始化和函数的实现:
在*****View.h的构造函数COCCmfcMDIView::COCCmfcMDIView() noexcept 进行变量的初始化
COCCmfcMDIView::COCCmfcMDIView() noexcept
{
// TODO: 在此处添加构造代码
mouseDownPT.x = 0;
mouseDownPT.y = 0;
m_Pt.x = 0;
m_Pt.y = 0;
m_scale = 1.0;
}
下面进行鼠标响应事件的实现:
1. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);的实现
void COCCmfcMDIView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_current_mode = CurAction3d_DynamicRotation;
mouseDownPT = point;
myView->StartRotation(point.x, point.y);
leftMouseBtn = true;
CView::OnLButtonDown(nFlags, point);
}
2. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);的实现
void COCCmfcMDIView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_current_mode = CurAction3d_Nothing;
if (leftMouseBtn==true)
{
leftMouseBtn = false;
}
CView::OnLButtonUp(nFlags, point);
}
3. afx_msg void OnMButtonDown(UINT nFlags, CPoint point);的实现
//鼠标中键按下事件初始操作
void COCCmfcMDIView::OnMButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_current_mode = CurAction3d_DynamicPanning;
mouseDownPT = point;
m_x_max = point.x; //记录平移时起始X位置
m_y_max = point.y; //记录平移时起始Y位置
midMouseBtn = true;
CView::OnMButtonDown(nFlags, point);
}
4. afx_msg void OnMButtonUp(UINT nFlags, CPoint point);的实现
void COCCmfcMDIView::OnMButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_current_mode = CurAction3d_Nothing;
if (midMouseBtn==true)
{
midMouseBtn == false;
}
CView::OnMButtonUp(nFlags, point);
}
5. afx_msg void OnMouseMove(UINT nFlags, CPoint point);的实现
void COCCmfcMDIView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CView::OnMouseMove(nFlags, point);
switch (m_current_mode)
{
case COCCmfcMDIView::CurAction3d_DynamicPanning:
//执行平移
myView->Pan(point.x - m_x_max, m_y_max-point.y);
m_x_max = point.x;
m_y_max = point.y;
break;
case COCCmfcMDIView::CurAction3d_DynamicRotation:
//执行旋转
myView->Rotation(point.x, point.y);
break;
}
}
6. afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);的实现
BOOL COCCmfcMDIView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
static float upLimit = 10.0f;
static float downLimit = 0.04f;
static float fUp = 1.1f;
static float fDown = 0.9f;
m_Pt = pt;
myView->StartZoomAtPoint(pt.x, pt.y); //在鼠标光标的点处进行放大
if (zDelta > 0 && m_scale <= upLimit)
{
m_scale *= fUp;
myView->ZoomAtPoint(0, 0, 10, 0);
}
else if (zDelta<0 && m_scale>downLimit)
{
m_scale *= fDown;
myView->ZoomAtPoint(0, 0, -10, 0);
}
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
至此,完成了的模型的旋转、平移、缩放功能 !!!!!!!!!!!