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中添加成员变量

 
  1. class MyOpenGL :

  2. public CWnd

  3. {

  4. public:

  5. MyOpenGL(void);

  6. ~MyOpenGL(void);

  7. //

  8. //成员变量

  9. int MySetPixelFormat(HDC hDC);

  10. void Rendercene();

  11. HDC hdc;

  12. HGLRC hglrc;

  13. //

  14. GLfloat step,s;

  15.  
  16. DECLARE_MESSAGE_MAP()

  17.  
  18. public:

  19. afx_msg void OnPaint();

  20. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

  21. };

6、MyOpenGL.cpp中假如以下代码:

6.1

 
  1. MyOpenGL::~MyOpenGL(void)

  2. {

  3. wglMakeCurrent(NULL,NULL);

  4. wglDeleteContext(hglrc);//删除渲染描述表

  5. ::ReleaseDC(m_hWnd,hdc);//释放设备描述表

  6. }

6.2在OnCreate中加入

 
  1. int MyOpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)

  2. {

  3. if (CWnd::OnCreate(lpCreateStruct) == -1)

  4. return -1;

  5.  
  6. // TODO: Add your specialized creation code here

  7. if(MySetPixelFormat(::GetDC(m_hWnd))==FALSE)

  8. return 0;

  9.  
  10. // 获得绘图描述表

  11. hdc = ::GetDC(m_hWnd);

  12. // 创建渲染描述表

  13. hglrc = wglCreateContext(hdc);

  14. // 使绘图描述表为当前调用现程的当前绘图描述表

  15. wglMakeCurrent(hdc, hglrc);

  16. return 0;

  17. }



6.3  在OnPaint()加入

 
  1. void MyOpenGL::OnPaint()

  2. {

  3. //CPaintDC dc(this); // device context for painting

  4. // TODO: Add your message handler code here

  5. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色缓存和深度缓存

  6. Rendercene();

  7. glPopMatrix();

  8. glFlush();

  9. SwapBuffers(hdc);

  10. // Do not call CWnd::OnPaint() for painting messages

  11. }


6.4 成员函数实现代码

 
  1. int MyOpenGL::MySetPixelFormat(HDC hDC)

  2. {

  3. PIXELFORMATDESCRIPTOR pixelDesc;

  4.  
  5. pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);

  6. pixelDesc.nVersion = 1;

  7.  
  8. pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |

  9. PFD_SUPPORT_OPENGL |

  10. PFD_DOUBLEBUFFER |

  11. PFD_TYPE_RGBA;

  12.  
  13. pixelDesc.iPixelType = PFD_TYPE_RGBA;

  14. pixelDesc.cColorBits = 32;

  15. pixelDesc.cRedBits = 0;

  16. pixelDesc.cRedShift = 0;

  17. pixelDesc.cGreenBits = 0;

  18. pixelDesc.cGreenShift = 0;

  19. pixelDesc.cBlueBits = 0;

  20. pixelDesc.cBlueShift = 0;

  21. pixelDesc.cAlphaBits = 0;

  22. pixelDesc.cAlphaShift = 0;

  23. pixelDesc.cAccumBits = 0;

  24. pixelDesc.cAccumRedBits = 0;

  25. pixelDesc.cAccumGreenBits = 0;

  26. pixelDesc.cAccumBlueBits = 0;

  27. pixelDesc.cAccumAlphaBits = 0;

  28. pixelDesc.cDepthBits = 0;

  29. pixelDesc.cStencilBits = 1;

  30. pixelDesc.cAuxBuffers = 0;

  31. pixelDesc.iLayerType = PFD_MAIN_PLANE;

  32. pixelDesc.bReserved = 0;

  33. pixelDesc.dwLayerMask = 0;

  34. pixelDesc.dwVisibleMask = 0;

  35. pixelDesc.dwDamageMask = 0;

  36.  
  37. int iPixelFormat;

  38.  
  39. // 为设备描述表得到最匹配的像素格式

  40. if((iPixelFormat = ChoosePixelFormat(hDC, &pixelDesc)) == 0)

  41. {

  42. MessageBox("ChoosePixelFormat Failed", NULL, MB_OK);

  43. return FALSE;

  44. }

  45.  
  46. // 设置最匹配的像素格式为当前的像素格式

  47. if(SetPixelFormat(hDC, iPixelFormat, &pixelDesc) == FALSE)

  48. {

  49. MessageBox("SetPixelFormat Failed", NULL, MB_OK);

  50. return FALSE;

  51. }

  52. return TRUE;

  53. }


 

 
  1. void MyOpenGL::Rendercene()

  2. {

  3. glBegin(GL_LINES);

  4. glVertex2f(-0.5f,-0.3f);

  5. glVertex2f(0.4f,0.6f);

  6. glEnd();

  7. }


 

7.在对话框的头文件里加入:#include MyOpenGL.h

添加成员变量 MyOpenGL*  m_GL;

然后在其OnCreate()函数中初始化:

 
  1. int CMFC_GL_TestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)

  2. {

  3. if (CDialog::OnCreate(lpCreateStruct) == -1)

  4. return -1;

  5.  
  6. // TODO: Add your specialized creation code here

  7. // 定义OpenGL绘图窗口的矩形大小

  8. //此段语句放在OnInitDialog(),或者OnCreate中

  9. CRect rect(0,0,300,300);

  10. m_GL=new MyOpenGL;//用New的方式创建

  11. m_GL->Create( NULL, //缺省的窗口

  12. NULL, //无窗口名称

  13. WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_VISIBLE,

  14. // 定义窗口风格

  15. rect, // 窗口的大小

  16. this, // 指定当前对话框为其父窗口指针

  17. 0);

  18. return 0;

  19. }


 

接下来就可以运行了。

效果图:

猜你喜欢

转载自blog.csdn.net/mid_Faker/article/details/107834552