virtual int OnCreate( LPARAM hWnd, PIXELFORMATDESCRIPTOR* PixelFormat = NULL );


Specifies the HWND handle of window. The OnCreate method function creates an OpenGL render context for the device context of this window.


Pointer to a PIXELFORMATDESCRIPTOR structure that specifies the requested pixel format. This pixel format will be used by the OpenGL render context associated with the window. If the parameter is NULL, then a default PIXELFORMATDESCRIPTOR structure will be used. The default pixel format structure contains the following values:

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

Return Value

If the function succeeds, the return value is 0. Any other value indicates a function failure.


Call OnCreate method function before using any other COpenGL object’s function. The OnCreate function will provide all necessary initialization of COpenGL object for using the given window’s client area for OpenGL graphics output. The OpenGL rendering context will be created and associated with the specified window. The current thread’s rendering context will be set to the created rendering context.

The rendering context associated with the window will be destroyed by the OnDestroy method function or by the COpenGL object destructor automatically.

The window associated with the COpenGL object is also called the object window.

The OnCreate function sets the rendering context value used for clearing the depth buffer to 1.0. The GL_PACK_ALIGNMENT and GL_UNPACK_ALIGNMENT are set to value 1. Other rendering context default values remain set as specified by the OpenGL standard.

Note: The LPARAM type is used due to multiple possible declarations of HWND type in the Win32 API. Using LPARAM should avoid type mismatch problems for using all HWND declarations.


class COglSampleView : public CView
  // specify operations or attributes as desired …

  COpenGL	m_OpenGL;

  // specify operations or attributes as desired …

int COglSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;
  // initialize OpenGL context
  m_OpenGL.OnCreate( ( LPARAM )m_hWnd );
  // enable Z buffer depth test ( or call glEnable( GL_DEPTH_TEST ) );
  m_OpenGL.SetDepthTestMode( TRUE );

  // enable lighting ( or call glEnable( GL_LIGHTING ) )
  m_OpenGL.SetLightingMode( TRUE );
  // enable light 0 ( or call glEnable( GL_LIGHT0 ) )
  m_OpenGL.SetLightState( 0, TRUE );
  // indicate success
  return 0;

See Also

PreCreateWindow, OnDestroy, OnSize