Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: OpenGL and VS .NET Winforms

  1. #1
    Guest

    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.

    Code :
    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.

  2. #2
    Guest

    Re: OpenGL and VS .NET Winforms

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2005
    Location
    San Francisco, CA
    Posts
    7

    Re: OpenGL and VS .NET Winforms

    In my code I have:

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

    Code :
    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.

  4. #4
    Guest

    Re: OpenGL and VS .NET Winforms

    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.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2005
    Location
    San Francisco, CA
    Posts
    7

    Re: OpenGL and VS .NET Winforms

    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!

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2007
    Location
    USA
    Posts
    1

    Re: OpenGL and VS .NET Winforms

    OMG! That little project actually worked for me!

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

    THANK YOU!
    --Cliff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •