MFC的对话框中使用OpenGL绘图
我们在编写软件是总是会有自己的界面,当然利用C++就不得不和MFC打交道了,那么可视化界面通常就要用MFC的Dialog;OpenGL通常画图时会自己生成一个窗口,就如同OpenCV一样,但现在我想OpenGL把图画在对话框指定的位置上,列如,我想在以下对话框的左侧部分显示我要画的图,该怎么做呢?
1、首先当然要设置好OpenGL的编程环境;
2、新建一个基于对话框的工程,我选择VS studio平台;
3、选着Project->add->class,添加一个新类,取名为MyOpenGL,选着基类为CWnd
4利用VS的类向导给MyOpenGL添加OnCreate()和OnPaint()函数;
5、在MyOpenGL.h中添加成员变量
-
class MyOpenGL :
-
public CWnd
-
{
-
public:
-
MyOpenGL(void);
-
~MyOpenGL(void);
-
//////////////////////////////////////////////////////////////////////////
-
//成员变量
-
int MySetPixelFormat(HDC hDC);
-
void Rendercene();
-
HDC hdc;
-
HGLRC hglrc;
-
//////////////////////////////////////////////////////////////////////////
-
GLfloat step,s;
-
DECLARE_MESSAGE_MAP()
-
public:
-
afx_msg void OnPaint();
-
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
-
};
6、MyOpenGL.cpp中假如以下代码:
6.1
-
MyOpenGL::~MyOpenGL(void)
-
{
-
wglMakeCurrent(NULL,NULL);
-
wglDeleteContext(hglrc);//删除渲染描述表
-
::ReleaseDC(m_hWnd,hdc);//释放设备描述表
-
}
6.2在OnCreate中加入
-
int MyOpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
-
{
-
if (CWnd::OnCreate(lpCreateStruct) == -1)
-
return -1;
-
// TODO: Add your specialized creation code here
-
if(MySetPixelFormat(::GetDC(m_hWnd))==FALSE)
-
return 0;
-
// 获得绘图描述表
-
hdc = ::GetDC(m_hWnd);
-
// 创建渲染描述表
-
hglrc = wglCreateContext(hdc);
-
// 使绘图描述表为当前调用现程的当前绘图描述表
-
wglMakeCurrent(hdc, hglrc);
-
return 0;
-
}
6.3 在OnPaint()加入
-
void MyOpenGL::OnPaint()
-
{
-
//CPaintDC dc(this); // device context for painting
-
// TODO: Add your message handler code here
-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色缓存和深度缓存
-
Rendercene();
-
glPopMatrix();
-
glFlush();
-
SwapBuffers(hdc);
-
// Do not call CWnd::OnPaint() for painting messages
-
}
6.4 成员函数实现代码
-
int MyOpenGL::MySetPixelFormat(HDC hDC)
-
{
-
PIXELFORMATDESCRIPTOR pixelDesc;
-
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
-
pixelDesc.nVersion = 1;
-
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
-
PFD_SUPPORT_OPENGL |
-
PFD_DOUBLEBUFFER |
-
PFD_TYPE_RGBA;
-
pixelDesc.iPixelType = PFD_TYPE_RGBA;
-
pixelDesc.cColorBits = 32;
-
pixelDesc.cRedBits = 0;
-
pixelDesc.cRedShift = 0;
-
pixelDesc.cGreenBits = 0;
-
pixelDesc.cGreenShift = 0;
-
pixelDesc.cBlueBits = 0;
-
pixelDesc.cBlueShift = 0;
-
pixelDesc.cAlphaBits = 0;
-
pixelDesc.cAlphaShift = 0;
-
pixelDesc.cAccumBits = 0;
-
pixelDesc.cAccumRedBits = 0;
-
pixelDesc.cAccumGreenBits = 0;
-
pixelDesc.cAccumBlueBits = 0;
-
pixelDesc.cAccumAlphaBits = 0;
-
pixelDesc.cDepthBits = 0;
-
pixelDesc.cStencilBits = 1;
-
pixelDesc.cAuxBuffers = 0;
-
pixelDesc.iLayerType = PFD_MAIN_PLANE;
-
pixelDesc.bReserved = 0;
-
pixelDesc.dwLayerMask = 0;
-
pixelDesc.dwVisibleMask = 0;
-
pixelDesc.dwDamageMask = 0;
-
int iPixelFormat;
-
// 为设备描述表得到最匹配的像素格式
-
if((iPixelFormat = ChoosePixelFormat(hDC, &pixelDesc)) == 0)
-
{
-
MessageBox("ChoosePixelFormat Failed", NULL, MB_OK);
-
return FALSE;
-
}
-
// 设置最匹配的像素格式为当前的像素格式
-
if(SetPixelFormat(hDC, iPixelFormat, &pixelDesc) == FALSE)
-
{
-
MessageBox("SetPixelFormat Failed", NULL, MB_OK);
-
return FALSE;
-
}
-
return TRUE;
-
}
-
void MyOpenGL::Rendercene()
-
{
-
glBegin(GL_LINES);
-
glVertex2f(-0.5f,-0.3f);
-
glVertex2f(0.4f,0.6f);
-
glEnd();
-
}
7.在对话框的头文件里加入:#include MyOpenGL.h
添加成员变量 MyOpenGL* m_GL;
然后在其OnCreate()函数中初始化:
-
int CMFC_GL_TestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
-
{
-
if (CDialog::OnCreate(lpCreateStruct) == -1)
-
return -1;
-
// TODO: Add your specialized creation code here
-
// 定义OpenGL绘图窗口的矩形大小
-
//此段语句放在OnInitDialog(),或者OnCreate中
-
CRect rect(0,0,300,300);
-
m_GL=new MyOpenGL;//用New的方式创建
-
m_GL->Create( NULL, //缺省的窗口
-
NULL, //无窗口名称
-
WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_VISIBLE,
-
// 定义窗口风格
-
rect, // 窗口的大小
-
this, // 指定当前对话框为其父窗口指针
-
0);
-
return 0;
-
}
接下来就可以运行了。
效果图: