Drawing3d documented Class Library

FeaturesW32.wglCreateContext Method

The wglCreateContext function creates a new OpenGL rendering context, which is suitable for drawing on the device referenced by deviceContext. The rendering context has the same pixel format as the device context.

[Visual Basic]
<PreserveSig> _
<DllImport(EntryPoint:="wglCreateContext", CharSet:=CharSet.None, SetLastError:=True, ExactSpelling:=False, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, BestFitMapping:=False, ThrowOnUnmappableChar:=False, Value:="opengl32.dll")> _
<SuppressUnmanagedCodeSecurity> _
Public Shared Sub wglCreateContext( _
   ByVal deviceContext As IntPtr _
[DllImport(EntryPoint="wglCreateContext", CharSet=CharSet.None, SetLastError=True, ExactSpelling=False, PreserveSig=True, CallingConvention=CallingConvention.Winapi, BestFitMapping=False, ThrowOnUnmappableChar=False, Value="opengl32.dll")]
public static IntPtr wglCreateContext(
   IntPtr deviceContext



Handle to a device context for which the function creates a suitable OpenGL rendering context.

Return Value

If the function succeeds, the return value is a valid handle to an OpenGL rendering context.

If the function fails, the return value is Zero. To get extended error information, call GetLastWin32Error.


A rendering context is not the same as a device context. Set the pixel format of the device context before creating a rendering context. For more information on setting the device context's pixel format, see the SetPixelFormat function.

To use OpenGL, you create a rendering context, select it as a thread's current rendering context, and then call OpenGL functions. When you are finished with the rendering context, you dispose of it by calling the wglDeleteContext function.

The following code example shows wglCreateContext usage:

             IntPtr hdc;
             IntPtr hglrc;

             // create a device context

             // create a rendering context
             hglrc = Wgl.wglCreateContext(hdc);

             // make it the calling thread's current rendering context
             Wgl.wglMakeCurrent(hdc, hglrc);

             // call OpenGL APIs as desired...

             // when the rendering context is no longer needed...

             // make the rendering context not current
             Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);

             // delete the rendering context

See Also

FeaturesW32 Class | Drawing3d.OS.Windows Namespace | SetPixelFormat | wglDeleteContext | wglGetCurrentContext | wglMakeCurrent