views:

270

answers:

2

Hi, I'm new in OpenGL.

I want to draw somethink using OpenGL in Windows Forms. If I use Win32 application with WinMain method, application working. In WinMain method, I fill HWND with CreateWindow() function and ı give WinMain parameters to CreateWindows.

But I want to get Handle from Windows form i cant get this. Everytime wglCreateContext(hdc) return NULL there is a example which is I take

public:
  COpenGL(System::Windows::Forms::Form ^ parentForm, GLsizei iWidth, GLsizei iHeight)
  {
   CreateParams^ cp = gcnew CreateParams;

   // Set the position on the form
   cp->X = 0;
   cp->Y = 0;
   cp->Height = iHeight;
   cp->Width = iWidth;

   // Specify the form as the parent.
   cp->Parent = parentForm->Handle;

   // Create as a child of the specified parent and make OpenGL compliant (no clipping)
   cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

   // Create the actual window
   this->CreateHandle(cp);

   m_hDC = GetDC((HWND)this->Handle.ToPointer());

   if(m_hDC)
   {
    MySetPixelFormat(m_hDC);
    ReSizeGLScene(iWidth, iHeight);
    InitGL();
   }

   rtri = 0.0f;
   rquad = 0.0f;
  }


GLint MySetPixelFormat(HDC hdc)
  {

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

   GLint  iPixelFormat; 

   // get the device context's best, available pixel format match 
   if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
   {
    MessageBox::Show("ChoosePixelFormat Failed");
    return 0;
   }


   // make that match the device context's current pixel format 
   if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
   {
    MessageBox::Show("SetPixelFormat Failed");
    return 0;
   }

   if((m_hglrc = wglCreateContext(hdc)) == NULL)
   {
    MessageBox::Show("wglCreateContext Failed");
    return 0;
   }

   if((wglMakeCurrent(hdc, m_hglrc)) == NULL)
   {
    MessageBox::Show("wglMakeCurrent Failed");
    return 0;
   }


   return 1;
  }

Have can I solve this problem?

A: 

you can check what glGetLastError value and mostly it is because you choose wrong or incompatible pixel format, you can try with other format, then your window class should be flagged CS_OWNDC and set DoubleBuffering to false.

uray
+1  A: 

Here, change in the constructor :

m_hDC = GetDC((HWND)this->Handle.ToPointer());
if(m_hDC)
{
wglMakeCurrent(m_hDC, NULL);
MySetPixelFormat(m_hDC);
ReSizeGLScene(iWidth, iHeight);
InitGL();
}

You must call wglMakeCurrent after m_hDC has been setup. I reply to the first article of the example. Here Creating an OpenGL view on a Windows Form

That solve my problem :)

swdev