Drawing3d documented Class Library

FeaturesW32.SetPixelFormat Method

The SetPixelFormat function sets the pixel format of the specified device context to the format specified by the iPixelFormat index.

[Visual Basic]
<PreserveSig> _
<DllImport(EntryPoint:="SetPixelFormat", CharSet:=CharSet.None, SetLastError:=True, ExactSpelling:=False, PreserveSig:=True, CallingConvention:=CallingConvention.Winapi, BestFitMapping:=False, ThrowOnUnmappableChar:=False, Value:="gdi32.dll")> _
Public Shared Sub SetPixelFormat( _
   ByVal deviceContext As IntPtr, _
   ByVal pixelFormat As Integer, _
   ByRef pixelFormatDescriptor As PIXELFORMATDESCRIPTOR _
[DllImport(EntryPoint="SetPixelFormat", CharSet=CharSet.None, SetLastError=True, ExactSpelling=False, PreserveSig=True, CallingConvention=CallingConvention.Winapi, BestFitMapping=False, ThrowOnUnmappableChar=False, Value="gdi32.dll")]
public static bool SetPixelFormat(
   IntPtr deviceContext,
   int pixelFormat,
   ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor


Specifies the device context whose pixel format the function attempts to set.
Index that identifies the pixel format to set. The various pixel formats supported by a device context are identified by one-based indexes.
Pointer to a FeaturesW32.PIXELFORMATDESCRIPTOR structure that contains the logical pixel format specification. The system's metafile component uses this structure to record the logical pixel format specification. The structure has no other effect upon the behavior of the SetPixelFormat function.

Return Value

If the function succeeds, the return value is true.

If the function fails, the return value is false.


If hdc references a window, calling the SetPixelFormat function also changes the pixel format of the window. Setting the pixel format of a window more than once can lead to significant complications for the Window Manager and for multithread applications, so it is not allowed. An application can only set the pixel format of a window one time. Once a window's pixel format is set, it cannot be changed.

You should select a pixel format in the device context before calling the wglCreateContext function. The wglCreateContext function creates a rendering context for drawing on the device in the selected pixel format of the device context.

An OpenGL window has its own pixel format. Because of this, only device contexts retrieved for the client area of an OpenGL window are allowed to draw into the window. As a result, an OpenGL window should be created with the WS_CLIPCHILDREN and WS_CLIPSIBLINGS styles. Additionally, the window class attribute should not include the CS_PARENTDC style.

The following code example shows SetPixelFormat usage:

            HDC hdc;
            int pixelFormat;
            FeaturesW32.PIXELFORMATDESCRIPTOR pfd;

            // size of this pfd
            pfd.nSize = (ushort) sizeof(FeaturesW32.PIXELFORMATDESCRIPTOR);

            // version number
            pfd.nVersion = 1;

            // support window, support OpenGL, double buffered

            // RGBA type
            pfd.iPixelType = Gdi.PFD_TYPE_RGBA;

            // 24-bit color depth
            pfd.cColorBits = 24;

            // color bits and shift bits ignored
            pfd.cRedBits = 0;
            pfd.cRedShift = 0;
            pfd.cGreenBits = 0;
            pfd.cGreenShift = 0;
            pfd.cBlueBits = 0;
            pfd.cBlueShift = 0;
            pfd.cAlphaBits = 0;
            pfd.cAlphaShift = 0;

            // no accumulation buffer, accum bits ignored
            pfd.cAccumBits = 0;
            pfd.cAccumRedBits = 0;
            pfd.cAccumGreenBits = 0;
            pfd.cAccumBlueBits = 0;
            pfd.cAccumAlphaBits = 0;

            // no stencil buffer
            pfd.cStencilBits = 0;

            // no auxiliary buffer
            pfd.cAuxBuffers = 0;

            // main layer
            pfd.iLayerType = Gdi.PFD_MAIN_PLANE;

            // reserved
            pfd.bReserved = 0;

            // layer masks ignored
            pfd.dwLayerMask = 0;
            pfd.dwVisibleMask = 0;
            pfd.dwDamageMask = 0;

            pixelFormat = Gdi.ChoosePixelFormat(hdc, &pfd);
            // make that the pixel format of the device context
            Gdi.SetPixelFormat(hdc, pixelFormat, &pfd);

See Also

FeaturesW32 Class | Drawing3d.OS.Windows Namespace | ChoosePixelFormat