MFC中用OpenGL在Picture控件中显示图片如何获取控件内的鼠标轮转动消息
MFC中用OpenGL在Picture控件中显示图片如何获取控件内的鼠标轮转动消息
在Picture控件新建了一个类 可以响应鼠标左右键 也可以响应中键和鼠标移动消息 就是不能响应鼠标轮转动消息
#OpenGL.h
class COpenGL : public CWnd
{
DECLARE_DYNAMIC(COpenGL)
public:
COpenGL();
virtual ~COpenGL();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
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 OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);//已添加鼠标轮响应
afx_msg void OnMButtonUp(UINT nFlags, CPoint point);
afx_msg void OnSize(UINT nType, int cx, int cy);
public:
int MySetPixelFormat(HDC hdc);
void RefreshDisplay1();
public:
HDC hdc;
HGLRC hglrc;
GLfloat step,s;
};
OpenGL.cpp
IMPLEMENT_DYNAMIC(COpenGL, CWnd)
COpenGL::COpenGL()
{
m_xPos = 0.0f;
m_yPos = 0.0f;
m_zPos = 0.0f;
m_xAngle = 0.0f;
m_yAngle = 0.0f;
m_zAngle = 0.0f;
m_Scale = 1.0f;
m_Capture=false;
m_MouseDown=false;
m_LButtonDown=false;
}
COpenGL::~COpenGL()
{
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hglrc);//删除渲染描述表
::ReleaseDC(m_hWnd,hdc);//释放设备描述表
}
BEGIN_MESSAGE_MAP(COpenGL, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MBUTTONDOWN()
ON_WM_MBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_MOUSEWHEEL() //已添加鼠标轮响应
ON_WM_SIZE()
END_MESSAGE_MAP()
// COpenGL 消息处理程序
int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)//创建窗体
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
MySetPixelFormat(::GetDC(m_hWnd)); //创建绘图描述表,并关联渲染描述表
CPaintDC dc(this);
hdc=::GetDC(m_hWnd);
hglrc=wglCreateContext(hdc);//使绘图描述表为当前调用线程的当前绘图描述表
wglMakeCurrent(hdc,hglrc);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//清除颜色缓存
glClearDepth(10.0f);//清除深度缓存
glEnable(GL_DEPTH_TEST);//使能深度
return 0;
}
void COpenGL::OnPaint()//绘制窗体
{
CPaintDC dc(this); //没有这条指令则无法使用定时器
// 绘画设备上下文
// TODO: 在此处添加消息处理程序代码
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色缓存和深度缓存
glLoadIdentity();//加载身份
glTranslatef(m_xPos, m_yPos, m_zPos);//平移
glRotatef(m_xAngle, 1.0f, 0.0f, 0.0f);//按角度旋转
glRotatef(m_yAngle, 0.0f, 1.0f, 0.0f);
glScalef(m_Scale, m_Scale, m_Scale);//缩放
DrawBox();
SwapBuffers(hdc);
// 不为绘图消息调用 COpenGL::OnPaint();
}
void COpenGL::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (0 >= cx || 0 >= cy)
{
return;
}
m_cx=(float)cx;m_cy=(float)cy;
glViewport(0, 0, cx, cy);//视口参数X,Y指定了视见区域的左下角在窗口中的位置,一般情况下为(0,0),Width和Height指定了视见区域的宽度和高度
glMatrixMode(GL_PROJECTION);//矩阵模式
glLoadIdentity();//加载
//gluPerspective(60.0, (GLfloat) cx/(GLfloat) cy, 1.0, 10.0);
//gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
if (cx < cy)
{
//glOrtho(-100.0, 100.0, -100.0*(GLfloat)cy / (GLfloat)cx, 100.0*(GLfloat)cy / (GLfloat)cx, -100.0, 100.0);//容器//左/右/下/上/近/远
glOrtho(-(GLfloat)(cx/2), (GLfloat)(cx/2), -(GLfloat)(cy/2)*(GLfloat)cy / (GLfloat)cx, (GLfloat)(cy/2)*(GLfloat)cy / (GLfloat)cx, -(GLfloat)(cx/2), (GLfloat)(cx/2));
}
else
{
//glOrtho(-100.0*(GLfloat)cx / (GLfloat)cy, 100.0*(GLfloat)cx / (GLfloat)cy, -100.0, 100.0, -100.0, 100.0);
glOrtho(-(GLfloat)(cx/2)*(GLfloat)cx / (GLfloat)cy, (GLfloat)(cx/2)*(GLfloat)cx / (GLfloat)cy, -(GLfloat)(cy/2), (GLfloat)(cy/2), -(GLfloat)(cx/2), (GLfloat)(cx/2));
}
glMatrixMode(GL_MODELVIEW);//矩阵模式
glLoadIdentity();//加载
}
int COpenGL::MySetPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR), //pfd结构的大小
1, // 版本号
PFD_DRAW_TO_WINDOW | //支持在窗口绘图
PFD_SUPPORT_OPENGL | //支持OPENGL
PFD_DOUBLEBUFFER , //双缓冲
PFD_TYPE_RGBA, //RGBA颜色模式
24, //24位颜色深度
0,0,0,0,0,0, //忽略颜色位
0, //没有非透明度缓存
0, //忽略移位位
0, //无累加缓存
0,0,0,0, //忽略累加位
32, //32位深度缓存
0, //无模板缓存
0, //无辅助缓存
PFD_MAIN_PLANE, //主层
0, //保留
0,0,0 //忽略层,可见性和损毁掩模
};
int iPixelFormat;
if( (iPixelFormat = ChoosePixelFormat(hdc,&pfd))==0)
{
MessageBox("ChooseFixelForamt Failed 选择固定孔失败!",NULL,MB_OK);
return 0;
}
if(SetPixelFormat(hdc,iPixelFormat,&pfd)==false)
{
MessageBox("SetPixelFormat Failed 设置像素格式失败!",NULL,MB_OK);
return 0;
}
return 1;
}
void COpenGL::OnLButtonDown(UINT nFlags, CPoint point)//单击左键
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_LButtonDown=true;
m_LButtonDownPoint=point;
SetCapture();
CWnd::OnLButtonDown(nFlags, point);
}
void COpenGL::OnLButtonUp(UINT nFlags, CPoint point)//放开左键
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_LButtonDown=false;
ReleaseCapture();
CWnd::OnLButtonUp(nFlags, point);
}
void COpenGL::OnMButtonDown(UINT nFlags, CPoint point)//单击中键
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_MouseDown=true;
m_MouseDownPoint=point;
SetCapture();
CWnd::OnMButtonDown(nFlags, point);
}
void COpenGL::OnMButtonUp(UINT nFlags, CPoint point)//释放中键
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_MouseDown=false;
m_MouseDownPoint = CPoint(0, 0);
ReleaseCapture();
CWnd::OnMButtonUp(nFlags, point);
}
void COpenGL::OnRButtonDown(UINT nFlags, CPoint point)//单击右键
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_xPos = 0.0f;
m_yPos = 0.0f;
m_zPos = 0.0f;
m_xAngle = 0.0f;
m_yAngle = 0.0f;
m_zAngle = 0.0f;
m_Scale = 1.0f;
m_ax=0;m_ay=0;m_bx=0;m_by=0;
CWnd::OnRButtonDown(nFlags, point);
}
void COpenGL::OnMouseMove(UINT nFlags, CPoint point)//鼠标移动
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (GetCapture() == this)
{
//Increment the object rotation angles
if(m_MouseDown==true)
{
m_xAngle += (point.y - m_MouseDownPoint.y) / 3.6;
m_yAngle += (point.x - m_MouseDownPoint.x) / 3.6;
InvalidateRect(NULL, FALSE);
m_MouseDownPoint = point;
}
if(m_LButtonDown==true)
{
m_xPos += (point.x - m_LButtonDownPoint.x)*(m_cx/m_cy);
m_yPos += (m_LButtonDownPoint.y - point.y);
m_LButtonDownPoint=point;
InvalidateRect(NULL, FALSE);
m_LButtonDownPoint=point;
}
}
m_Capture=true;
m_Mouse=point;
CWnd::OnMouseMove(nFlags, point);
}
BOOL COpenGL::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)//滚轮消息无法响应不知道什么原因
{
return CWnd::OnMouseWheel(nFlags, zDelta, pt);
}