OpenGL and VS .NET Winforms

I’m sure everybody else already has this working, but this is the first time I’ve tried to use OpenGL with VS 2005. I initialized everything in the form load event.

HDC hDC;
		 HGLRC hRC;
		 HWND hWnd;
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
			 {
				 
				hWnd = (HWND)panel3->Handle.ToInt64();
				hDC = GetDC(hWnd);

				PIXELFORMATDESCRIPTOR pfd = {
    				sizeof(PIXELFORMATDESCRIPTOR),
					1,
					PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
					PFD_TYPE_RGBA,
					32,
					0,0,0,0,0,0,
					8,0,
					0,0,0,0,0,
					24,
					0,
					0,
					PFD_MAIN_PLANE,
					0,
					0,0,
				};

				int PixelFormat = ChoosePixelFormat(hDC, &pfd);
				SetPixelFormat(hDC, PixelFormat, &pfd);

				wglDeleteContext(hRC);
				hRC = wglCreateContext(hDC);

				if(hRC == NULL)
						return;

				if(wglMakeCurrent(hDC, hRC) == false)
						return;
			
				glEnable(GL_DEPTH_TEST);
				glDepthFunc(GL_LEQUAL);

				glEnable(GL_NORMALIZE);

				glViewport(0, 0, panel3->Width, panel3->Height);

				glClearColor(1, 0, 0, 0);
				glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

				SwapBuffers(hDC);
			 }

I then created a timer and inserted the same last 4 lines of the load event, simply clear the screen. But nothing is showing up? I then found out the call to wglCreateContext is failing.

I looked at the error code returned by GetLastError and its returning 2000, which indicates the pixel format is incorrect. But the format I specified works just fine in my non winforms projects.

In my code I have:

HWND hwnd = static_cast<HWND>( openGLPanel->Handle.ToPointer() );

and

static PIXELFORMATDESCRIPTOR pfd = 
	{
		sizeof( PIXELFORMATDESCRIPTOR ),
		1,
		PFD_DRAW_TO_WINDOW | 
		PFD_SUPPORT_OPENGL |
		PFD_DOUBLEBUFFER   |
		PFD_TYPE_RGBA,
		32,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};

Notice that PFD_TYPE_RGBA is part of the OR’ed statement, and not its own parameter. That pixel format code is from the OpenGL Game Programming book; seems to work.

I tried using your code and I still wglCreateContext still fails. Would you mind posting a sample winforms project that successfully initializes OpenGL? I have tried everything to get it to work on my machine.

Okay, I threw together a little app which just draws on the main window.

It would be very trivial to add a panel and draw to that (I address that in the code).

Let me know if this helps!

-JR

Here ya go!

OMG! That little project actually worked for me!

Now I just have to figure out how to integrate it into my app.

THANK YOU!