MFC OpenGL and Dialogs

This is unfortunately going to be a vague question. I have an opengl program running inside a CFrameWnd. It runs fine, but when I try and bring up any dialog boxes(either ones I’ve created or something like CColorDialog) they don’t appear and it seems to freeze the program because it is trying to show the dialog box. If I hit Alt+Tab to switch to a different program and then back, the dialogs are visible and I can use them fine. The stange part, is so far, the CFileDialog works fine, but all other dialogs are screwy.

Does anyone have any clue what is going on? Any suggestions about where I should be looking in my code, or what I should be looking for would be appreciated. Thanks in advance.

-Drew

Hi !

Not a clue, but check your code, i’m pretty sure you have scrwed up pretty bad somewhere in it.

Mikael

Hi again !

I forgot to ask, I guess you don’t have problems with other OpenGL apps ?

Mikael

This is my first try mixing OpenGL and MFC. Up until this part I have just used GLUT. I thought that the question was a little vague, do you have any idea where I should be looking?

At the bottom of this are my OnCreate(), OnPaint(), PreCreateWindow(), and SetPixelformat(). If you can think of anything else that it might be, let me know, and I will post that.

-Drew

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{

m_hgldc = ::GetDC(m_hWnd);	// Get a device context for OpenGL

if(!SetPixelformat(m_hgldc)) // set pixel format
{
::MessageBox(::GetFocus(),"SetPixelformat Failed!","Error",MB_OK);
return -1;
}

m_hglRC = wglCreateContext(m_hgldc);	// Create an OpenGL rendering context 
int i= wglMakeCurrent(m_hgldc,m_hglRC);	// Make rendering context current

InitGL();	// Initialize OpenGL

hIcon = GetIcon( IDI_ICON1 );
CWnd::SetIcon( hIcon, TRUE );

return 0;

}

void CMainFrame::OnPaint( void )
{
CPaintDC dc( this );

if( m_bPlay && m_pCurrent != NULL )
{
		TimerWait( FRAMERATE );
}

DrawGLScene();

SwapBuffers(m_hgldc);

Invalidate(FALSE);

}

BOOL CMainFrame::PreCreateWindow( CREATESTRUCT& cs )
{
if(!CFrameWnd::PreCreateWindow(cs))
return FALSE;

cs.style &= ~FWS_ADDTOTITLE;    // keep MFC from adding to title

// Set the initial size of the window
cs.cx = m_nWindowWidth;
cs.cy = m_nWindowHeight + WIN_COMPS;
cs.x = 100;
cs.y = 100;

return CFrameWnd::PreCreateWindow(cs);//TRUE;

}

BOOL CMainFrame::SetPixelformat(HDC hdc)
{

PIXELFORMATDESCRIPTOR *ppfd; 
int pixelformat; 

PIXELFORMATDESCRIPTOR pfd = { 
sizeof(PIXELFORMATDESCRIPTOR),  //  size of this pfd 
1,                     // version number 
PFD_DRAW_TO_WINDOW |   // support window 
PFD_SUPPORT_OPENGL |   // support OpenGL 
PFD_GENERIC_FORMAT |
PFD_DOUBLEBUFFER,      // double buffered 
PFD_TYPE_RGBA,         // RGBA type 
32,                    // 32-bit color depth 
0, 0, 0, 0, 0, 0,      // color bits ignored 
8,                     // no alpha buffer 
0,                     // shift bit ignored 
8,                     // no accumulation buffer 
0, 0, 0, 0,            // accum bits ignored 
64,                    // 64-bit z-buffer	 
8,                     // stencil buffer 
8,                     // auxiliary buffer 
PFD_MAIN_PLANE,        // main layer 
0,                     // reserved 
0, 0, 0                // layer masks ignored 
}; 


ppfd = &pfd;


if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 ) 
{ 
    ::MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK); 
    return FALSE; 
} 

if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE) 
{ 
    ::MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK); 
    return FALSE; 
} 

return TRUE; 

}

Rather than trying to debug your code, I suggest you take a look at the following website for all your MFC & OpenGL questions.
www.mfcogl.com

Originally posted by jleffelm:
[b]This is unfortunately going to be a vague question. I have an opengl program running inside a CFrameWnd. It runs fine, but when I try and bring up any dialog boxes(either ones I’ve created or something like CColorDialog) they don’t appear and it seems to freeze the program because it is trying to show the dialog box. If I hit Alt+Tab to switch to a different program and then back, the dialogs are visible and I can use them fine. The stange part, is so far, the CFileDialog works fine, but all other dialogs are screwy.

Does anyone have any clue what is going on? Any suggestions about where I should be looking in my code, or what I should be looking for would be appreciated. Thanks in advance.

-Drew[/b]

Only advice is be careful when calling OpenGL commands from anywhere.

Make sure your rendering context is always valid before calling
a ‘gl’ command.

If you’re calling some gl command outside the normal rendering
loop, make sure OpenGL is initialized with a valid RC.

If you have more than one rendering context, make it is
the current RC and that it is valid.