title: wglCreateContext throws INVALID_OPERATION exception
body: <p>suppose that I have <a href="http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c10975/Setting-Up-OpenGL-in-an-MFC-Control.htm" rel="nofollow noreferrer">COpenGLControl class downloaded here from codeguru</a> assuming that <a href="https://stackoverflow.com/questions/18258668/order-of-events-when-a-window-starts-in-visual-c-mfc">the first event handler runned when creating the OpenGL window is OnCreate</a>, I have tried to catch up the errors of this class. here's the code used to create the window in my dialog's .h and .cpp files:  </p>
  
  <p><strong>MyOpenGLTestDlg.h</strong>  </p>
  
  <pre><code>COpenGLControl m_oglWindow;  
  </code></pre>
  
  <p><strong>MyOpenGLTestDlg.cpp</strong>  </p>
  
  <pre><code>CRect rect;
  
  // Get size and position of the picture control
  GetDlgItem(ID_OPENGL)-&gt;GetWindowRect(rect);
  
  // Convert screen coordinates to client coordinates
  ScreenToClient(rect);  
  </code></pre>
  
  <p>up to know I think the function <code>OnCreate</code> has been invoked. In fact, I think the line of code <code>COpenGLControl m_oglWindow;</code> causes this function to be invoked! but I'm not sure so it will be appreciated if you guide me alittle about this?<br>
  anyway, I have not made a lot of change to the class:  </p>
  
  <p><strong>OpenGLControl.h</strong>  </p>
  
  <pre><code>#pragma once
  #include "afxwin.h"
  #include "WinBase.h"
  
  #include &lt;gl/gl.h&gt;
  #include &lt;gl/glu.h&gt;
  
  class COpenGLControl : public CWnd
  {
  public:
      /******************/
      /* Public Members */
      /******************/
      UINT_PTR m_unpTimer;
      // View information variables
      float    m_fLastX;
      float    m_fLastY;
      float    m_fPosX;
      float    m_fPosY;
      float    m_fZoom;
      float    m_fRotX;
      float    m_fRotY;
      bool     m_bIsMaximized;
  
  private:
      /*******************/
      /* Private Members */
      /*******************/
      // Window information
      CWnd  *hWnd;     //window handle
      HDC   hdc;       //device context handle    
      HGLRC hrc;       //handle to GL Rendering Context
      int   m_nPixelFormat;
      CRect m_rect;
      CRect m_oldWindow;
      CRect m_originalRect;
  
  public:
      COpenGLControl(void);
      virtual ~COpenGLControl(void);
  
      void oglCreate(CRect rect, CWnd *parent);
      void oglInitialize(void);
      void oglDrawScene(void);
  
      // Added message classes:
      afx_msg void OnPaint();
      afx_msg void OnSize(UINT nType, int cx, int cy);
      afx_msg void OnDraw(CDC *pDC);
      afx_msg int  OnCreate(LPCREATESTRUCT lpCreateStruct);
      afx_msg void OnTimer(UINT nIDEvent);
      afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  
      DECLARE_MESSAGE_MAP()
  };  
  </code></pre>
  
  <p><strong>OpenGLControl.cpp</strong>  </p>
  
  <pre><code>int COpenGLControl::OnCreate(LPCREATESTRUCT lpCreateStruct)
  {
      if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;
  
      oglInitialize();
  
      return 0;
  }  
  void COpenGLControl::oglInitialize(void)
  {
      // Initial Setup:
      //
      static PIXELFORMATDESCRIPTOR pfd =
      {
          sizeof(PIXELFORMATDESCRIPTOR),
          1,
          PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
          PFD_TYPE_RGBA,
          32, // bit depth
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          24, // z-buffer depth
          8,0,PFD_MAIN_PLANE, 0, 0, 0, 0,
      };
  
      // Get device context only once.
      hdc = GetDC()-&gt;m_hDC;
      // Pixel format.
      m_nPixelFormat = ChoosePixelFormat(hdc, &amp;pfd);
      SetPixelFormat(hdc, m_nPixelFormat, &amp;pfd);
      // Create the OpenGL Rendering Context.
      hrc = wglCreateContext(hdc);
      GLenum error13 = glGetError();
      wglMakeCurrent(hdc, hrc);
  
     // Basic Setup:
     //
     // Set color to use when clearing the background.
     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
     glClearDepth(1.0f);
     // Turn on backface culling
     glFrontFace(GL_CCW);
     glCullFace(GL_BACK);
     // Turn on depth testing
     glEnable(GL_DEPTH_TEST);
     glDepthFunc(GL_LEQUAL);
     // Send draw request
     OnDraw(NULL);
  }  
  </code></pre>
  
  <p>As you see I have written the code <code>GLenum error13 = glGetError();</code> right after <code>hrc = wglCreateContext(hdc);</code> to catch any probable error that it throws and yes the value for error13 is <code>1282</code> which means <code>INVALID_OPERATION</code> so I think the <code>handle for OpenGL rendering context</code> is not created properly!<br>
  besides if you check the values for <code>hdc</code> and <code>hrc</code>, you encounter these:  </p>
  
  <p><em>hdc -> unused = ??? (Error: expression can not be evaluated)<br>
  hrc -> unused = 0</em>  </p>
  
  <p><strong>Could you help me find out why it is the case? and what's the problem?</strong></p>