OpenGL in MFC static (frame) control

How can i render opengl to an MFC Frame Control ?
I believe That OpenGL can be render to any win32 control,Since
I have done this in the past but with win32 api not MFC.
When I do the same thing with MFC it won’t work.

//----------------code------------------------

void MainDialog::EnableOpenGL(int colorbits,HWND openGLHwnd,int width,int height)
{
int iFormat;
PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
colorbits,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};

   	        hDC = GetDC (hWnd);
            iFormat = ChoosePixelFormat (hDC, &pfd);
            SetPixelFormat (hDC, iFormat, &pfd);
            hRC = wglCreateContext(hDC );
            wglMakeCurrent(hDC,hRC);


	if (height==0)										
            {
	      height=1;									
            }

            glViewport(0,0,width,height);
            glMatrixMode(GL_PROJECTION);						
        glLoadIdentity();								
        glOrtho(0,width,0,height,-1,1);

}

int MainDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;

HWND openGLFrameHandle = ::GetDlgItem(this->m_hWnd,FRAME_OPENGL); //---(1)
EnableOpenGL(32,openGLFrameHandle,600,400); //---(2)

//EnableOpenGL(32,this->m_hWnd,800,600);  --(3)




SetTimer(1,15,NULL);


return 0;

}

void MainDialog::OnTimer(UINT nIDEvent)
{
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SwapBuffers();

CDialog::OnTimer(nIDEvent);

}

//-------------code--------------------------------------

1.This application doesn’t do anything,it just suppose to clear FRAME_OPENGL static(frame)
Control to black color

2.EnableOpenGL is just a function for initialize opengl

3.hDc and hRc are member variable of this class

4.if you comment (1) and (2) and use Maindialog handle (3) instead of FRAME_OPENGL’s handle,
every thing just work fine

Perhaps MFC is setting the pixel format first? I know that the pixel format can only be called once.

I don’t know much about MFC, sorry.

try it that way:

HWND openGLFrameHandle = ::GetDlgItem(this->m_hWnd,FRAME_OPENGL);
hDC = GetDC (openGLFrameHandle);
iFormat = ChoosePixelFormat (hDC, &pfd);
SetPixelFormat (hDC, iFormat, &pfd);
hRC = wglCreateContext(hDC );
wglMakeCurrent(hDC,hRC);
.
.
.

i don’t know much about mfc either, but it seems a bit suspicious to me that you pass HWND openGLHwnd in void MainDialog::EnableOpenGL, but you use a different handle later in the function: hDC = GetDC (hWnd).

Try with this->GetSafeHwnd()

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.