How to Create a CHILD OpenGL Window in a Dialog | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 1, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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
public:
  ~CFastDlg(void);
  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
  MySetPixelFormat(::GetDC(m_hWnd));
  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
  glClearColor(0,0,0,0);
  glColor3f(1, 1, 1);
  if (hglrc = wglCreateContext(hdc))
  {
    // try to make it the thread’s current rendering context
    if(wglMakeCurrent(hdc, hglrc))
    {
      //render here
      glClear(GL_COLOR_BUFFER_BIT);
      glBegin(GL_TRIANGLES);
      glColor3f(1, 0, 0);
      glVertex3f(.5, 0, 0);
      glColor3f(0, 1, 0);
      glVertex3f(0, .5, 0);
      glColor3f(0, 0, 1);
      glVertex3f(-.5, 0, 0);
      glEnd();
      SwapBuffers(hdc);
    }
  }
  wglMakeCurrent(NULL, NULL) ;
  ::ReleaseDC (m_hWnd, hdc) ;
  wglDeleteContext(hglrc);
  // 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()
{
  CDialog::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
     WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_VISIBLE,
     rect,
     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 CodeGuru.com… there articles (and the people who submitted them) have helped out quite often.

Downloads

Download demo project – 494 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.