MFC中用OpenGL在Picture控件中显示图片如何获取控件内的鼠标轮转动消息

HEWDSD 2019-03-14 11:19:10
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);
}
...全文
663 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
HEWDSD 2019-04-17
  • 打赏
  • 举报
回复
void COpenGL::OnPaint()//绘制窗体
{
CPaintDC dc(this); //没有这条指令则无法使用定时器
//ValidateRect(NULL);//可以替代上一条指令
// 绘画设备上下文
// 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();
}

SwapBuffers(hdc); 这条语句是什么作用没有它为什么不能显示图形
还不能在void COpenGL::OnPaint()函数外面执行
schlafenhamster 2019-03-19
  • 打赏
  • 举报
回复
至于 OnMouseWheel 如果 父窗口 只有 COpenGL 那么 不用 SetFocus ,
否则 要 SetFocus,让 焦点 从 别的控件,转到 本窗口
schlafenhamster 2019-03-15
  • 打赏
  • 举报
回复
请上传 DrawBox()代码 !
schlafenhamster 2019-03-15
  • 打赏
  • 举报
回复
// CPaintDC dc(this); //没有这条指令则无法使用定时器
其实是因为没有这句 窗口 总是无效, 就 不停地 OnPaint ,没法 响应 Timer
你可以 用 下句 代替:
ValidateRect(NULL);// 使窗口 有效 !
// CPaintDC dc(this); //没有这条指令则无法使用定时器
schlafenhamster 2019-03-15
  • 打赏
  • 举报
回复
请 上 窗口 创建 代码
schlafenhamster 2019-03-14
  • 打赏
  • 举报
回复
down up 事件中 加入 SetFocus 试试。
HEWDSD 2019-03-14
  • 打赏
  • 举报
回复
是我的类用错了吗? 这是系统自动生成的 但是现在可以响应鼠标左右键 也可以响应中键和鼠标移动消息 就是不能响应鼠标轮转动消息
_船长_ 2019-03-14
  • 打赏
  • 举报
回复
鼠标滚轮这个消息,是发送给具有焦点的窗口消息队列中,这个消息比较恶心,建议使用钩子去处理这个消息
schlafenhamster 2019-03-14
  • 打赏
  • 举报
回复
在Picture控件中 即 class CMyStatic : public CStatic // 不是 CWnd

BOOL CMyStatic::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
afxDump << "OnMouseWheel\n";
return CStatic::OnMouseWheel(nFlags, zDelta, pt);
}

HEWDSD 2019-03-14
  • 打赏
  • 举报
回复
2楼的兄弟什么是钩子 4楼的兄弟 我给控件加了SetFocus焦点但是没有用

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧