Is this happens all the time or its rare bug? Can you post drivers, version, OS, service pack, hardware, graphics card.
When you create invisible windows did you specify CS_OWNDC in window class?
Here is code that I use for that:
typedef struct tagPixelFormat
{
tagPixelFormat()
{
ColorDepth = 32;
AlphaDepth = 8;
ZDepth = 24;
StencilDepth = 0;
MultiSample = 0;
FullScreen = false;
VSync = false;
}
int ColorDepth, AlphaDepth, ZDepth, StencilDepth, MultiSample;
bool FullScreen;
bool VSync;
} tPixelFormat;
BOOL RegisterWindowClass () // Register A Window Class For This Application.
{ // TRUE If Successful
// Register A Window Class
WNDCLASSEX windowClass; // Window Class
ZeroMemory (&windowClass, sizeof (WNDCLASSEX)); // Make Sure Memory Is Cleared
windowClass.cbSize = sizeof (WNDCLASSEX); // Size Of The windowClass Structure
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;// | CS_DBLCLKS; // Redraws The Window For Any Movement / Resizing
windowClass.lpfnWndProc = (WNDPROC)(WindowProc); // WindowProc Handles Messages
windowClass.hInstance = g_hInstance; // Set The Instance
windowClass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE); // Class Background Brush Color
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
windowClass.lpszClassName = WND_CLASSNAME; // Sets The Applications Classname
if (RegisterClassEx (&windowClass) == 0) // Did Registering The Class Fail?
{
// NOTE: Failure, Should Never Happen
MessageBox (HWND_DESKTOP, "RegisterClassEx Failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return False (Failure)
}
return TRUE; // Return True (Success)
}
static LRESULT CALLBACK PFWinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, message, wParam, lParam);
}
static void initEntryPoints(HWND hwnd, const PIXELFORMATDESCRIPTOR &pfd)
{
HDC hdc = GetDC(hwnd);
int nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, nPixelFormat, &pfd);
HGLRC hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
InitOpenGLExtensions();
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwnd, hdc);
}
#define elementsOf(x) (sizeof(x) / sizeof(x[0]))
static int GetPixelFormat(IEngine::tPixelFormat& pf)
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof (PIXELFORMATDESCRIPTOR), 1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, pf.ColorDepth,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
pf.ZDepth, pf.StencilDepth,
0, PFD_MAIN_PLANE, 0, 0, 0, 0 };
WNDCLASS wincl;
wincl.hInstance = g_hInstance;
wincl.lpszClassName = "PFrmt";
wincl.lpfnWndProc = PFWinProc;
wincl.style = 0;
wincl.hIcon = NULL;
wincl.hCursor = NULL;
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = NULL;
RegisterClass(&wincl);
HWND hwnd = CreateWindow("PFrmt", "PFormat", WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 8, 8, HWND_DESKTOP, NULL, g_hInstance, NULL);
initEntryPoints(hwnd, pfd);
HDC hdc = GetDC(hwnd);
int iAttribs[] =
{
WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_RED_BITS_ARB, (pf.ColorDepth > 16)? 8 : 5,
WGL_GREEN_BITS_ARB, (pf.ColorDepth > 16)? 8 : 6,
WGL_BLUE_BITS_ARB, (pf.ColorDepth > 16)? 8 : 5,
WGL_ALPHA_BITS_ARB, (pf.ColorDepth > 16)? 8 : 0,
WGL_DEPTH_BITS_ARB, pf.ZDepth,
WGL_STENCIL_BITS_ARB, pf.StencilDepth,
0
};
int pixelFormats[256];
int bestFormat = 0;
int bestSamples = 0;
unsigned int nPFormats;
if ( EXTCAPS(WGL_ARB_pixel_format)
&& wglChoosePixelFormatARB(hdc, iAttribs, NULL, elementsOf(pixelFormats), pixelFormats, &nPFormats)
&& nPFormats > 0)
{
int minDiff = 0x7FFFFFFF;
int attrib = WGL_SAMPLES_ARB;
int samples;
// Find a multisample format as close as possible to the requested
for (unsigned int i = 0; i < nPFormats; i++)
{
wglGetPixelFormatAttribivARB(hdc, pixelFormats[i], 0, 1, &attrib, &samples);
int diff = abs(pf.MultiSample - samples);
if (diff < minDiff)
{
minDiff = diff;
bestFormat = i;
bestSamples = samples;
}
}
}
else
{
SendMessage(hwnd, WM_CLOSE, 0, 0);
DestroyWindow(hwnd);
return ChoosePixelFormat(hdc, &pfd);
}
pf.MultiSample = bestSamples;
SendMessage(hwnd, WM_CLOSE, 0, 0);
DestroyWindow(hwnd);
return pixelFormats[bestFormat];
}