wglCreateContext()每次在OpenGL和Visual C ++中返回NULL

Mur*_*rat 3 opengl visual-c++

我是OpenGL的新手。

我想在Windows窗体中使用OpenGL进行一些思考。如果我将Win32应用程序与WinMain方法一起使用,则该应用程序将正常工作。在WinMain方法中,我HWNDCreateWindow()函数和?填充。将WinMain参数提供给CreateWindows

但我想从Windows窗体中获取Handle,我无法获取此信息。每次 wglCreateContext(hdc)NULL 一个例子是我拿

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;
  }
Run Code Online (Sandbox Code Playgroud)

我能解决这个问题吗?

swd*_*dev 5

在这里,更改构造函数:

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

设置m_hDC之后,必须调用wglMakeCurrent。我回复该示例的第一篇文章。在此处在Windows窗体上创建OpenGL视图

那解决了我的问题:)