How to Create a CHILD OpenGL Window in a Dialog

Environment: VC6 Win 2000

For a while, I’ve been using glut (which is great) to create all of my OpenGL projects. This was fine for a while until I had to do some “crazy” things with subwindows. I figured if I just used MFC instead, all my problems would be solved. The only down side was trying to get a child opengl window in my dialog. I’ve seen a lot of examples using MDI and SDI, but I just would have felt cheated if I couldn’t get it to work for a dialog. Through a little trial and error and a couple of passes through the MSDN, I offer you this quick and dirty example of how to do it.

The first thing you do is create a dialog based application. Next you can use the Class Wizard (by right clicking on the dialog form you see in your resource tab) to create a new class derived from a generic CWnd (I named mine COpenGL… quite original). After you’ve created your new class, use the class wizard to Set up the functions that handle the WM_CREATE and WM_PAINT messages. You can add other messages if you wish, but these are all we’ll need for now.

Inside the generated dialog class header file (in my example it’s called fastDlg.h), you’ll want to add a pointer to your new class.

class CFastDlg : public CDialog
// Construction
CFastDlg(CWnd* pParent = NULL); // standard constructor

COpenGL *m_pDisplay; //<– here he is!


Don’t forget to add the header file at the top. You’ll find the generated message handlers inside OpenGL.cpp. On creation of the window, I set its device context so it can handle opengl commands.

class CFastDlg : public CDialog
int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here

return 0;

The code for MySetPixelFormat(…) was found in the MSDN. Inside OnPaint, I make the device context current, do my opengl commands, and make my dievice context non-current. I’m sure there are several different ways to do this, and you should do as much error checking as possible. But like I said before, this is a quick and dirty example 😉

void COpenGL::OnPaint()
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
HDC hdc = ::GetDC(m_hWnd);
HGLRC hglrc;

// TODO: Add your message handler code here
glColor3f(1, 1, 1);

if (hglrc = wglCreateContext(hdc))
// try to make it the thread’s current rendering context
if(wglMakeCurrent(hdc, hglrc))
//render here
glColor3f(1, 0, 0);
glVertex3f(.5, 0, 0);
glColor3f(0, 1, 0);
glVertex3f(0, .5, 0);
glColor3f(0, 0, 1);
glVertex3f(-.5, 0, 0);

wglMakeCurrent(NULL, NULL) ;
::ReleaseDC (m_hWnd, hdc) ;

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

The last important thing that you’ll need to do is create the window in the OnInitDialog function.

BOOL CFastDlg::OnInitDialog()

// Set the icon for this dialog.
// The framework does this automatically when
// the application’s main window is not a dialog

SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

CRect rect(7, 7, 300, 300);

// TODO: Add extra initialization here
m_pDisplay->Create( NULL, //CWnd default
NULL, //has no name
this, //this is the parent
0); //this should really be a different
// number… check resource.h

return TRUE; // return TRUE unless you set
// the focus to a control


I really hope this helps. I know I couldn’t find a tutorial on creating a child window in a dialog, and I tried several other approaches before hitting this one. I’d also like to thank… there articles (and the people who submitted them) have helped out quite often.


Download demo project – 494 Kb

More by Author

Must Read