SetPixelFormat problem

I have the code below, which sets my pixel format. But, the problem is that this code runs properly in my computer although it cannot set pixel format in some of other computers. Does SetPixelFormat function have any relationship with hardware or type of monitor or desktop settings etc…? Is there anything wrong which I cannot see?

/////////////////////////////////////////////////////////////////////////
PIXELFORMATDESCRIPTOR pfd = {
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
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};

int iPixelFormat;

if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
{
MessageBox(NULL, “ChoosePixelFormat Failed”, NULL, MB_OK);
return 0;
}

if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
{
MessageBox(NULL, “SetPixelFormat Failed”, NULL, MB_OK);
return 0;
}
/////////////////////////////////////////////////////////////////

Might be that 32bit depth buffer is not supported on other gfx card. Whats the card you using when it works and when not?
In general when it fails means that the values you provide for the pixel format are not supported. Better not hardcode those but give the ones you want and iterate through the supported formats to the closest one.

And make sure SetPixelFormat() is called only once in the same thread. :slight_smile:

It works with Intel® Q965/Q963 Express Chipset Family.
It does not work with Intel® 82865G Graphics Controller.

If you fail on SetPixelFormat(), fill in another values like 24 bits for depth buffer or 16 bits if it failes again and maybe few other options like 0/8bit stencil, … to many to list them.
You can’t rely on the constant values in your code because not all cards will support them as you may noticed already.

And make sure SetPixelFormat() is called only once in the same thread.

WRONG. It can be called as often as you like from the same thread. What you must not do is to call it more than once for the same window-DC.