Creating a Rendering Context and Making It Current

The following code sample shows how to create an OpenGL rendering context in response to a WM_CREATE message. Notice that you set up the pixel format before creating the rendering context. Also notice that in this scenario the device context is not released locally; you release it when the window is closed, after making the rendering context not current. For more information, see Deleting a Rendering Context. Finally, notice that you can use local variables for the device context and rendering context handles, because with the wglGetCurrentContext and wglGetCurrentDC functions you can obtain handles to those contexts as needed.

// a window has been created, but is not yet visible 
case WM_CREATE: 
    { 
    // local variables 
    HDC      hdc ; 
    HGLRC    hglrc ; 
 
    // obtain a device context for the window 
    hdc = GetDC(hWnd); 
     
    // set an appropriate pixel format  
    myPixelFormatSetupFunction(hdc); 
 
    // if we can create a rendering context ...  
    if (hglrc = wglCreateContext( hdc ) ) { 
 
        // try to make it the thread's current rendering context 
        bHaveCurrentRC = wglMakeCurrent(hdc, hglrc) ; 
 
        } 
 
    // perform miscellaneous other WM_CREATE chores ... 
 
    }  
    break ;