Window wndproc to class method

http://www.codeproject.com/Articles/27219/TetroGL-An-OpenGL-Game-Tutorial-in-C-for-Win32-Pla

 

 

 


void CMainWindow::RegisterWindowClass()
{
    WNDCLASS WindowClass;
    WindowClass.style         = 0;
    WindowClass.lpfnWndProc   = &CMainWindow::OnEvent;
    WindowClass.cbClsExtra    = 0;
    WindowClass.cbWndExtra    = 0;
    WindowClass.hInstance     = GetModuleHandle(NULL);
    WindowClass.hIcon         = NULL;
    WindowClass.hCursor       = 0;
    WindowClass.hbrBackground = 0;
    WindowClass.lpszMenuName  = NULL;
    WindowClass.lpszClassName = WINDOW_CLASSNAME;

    RegisterClass(&WindowClass);
}



CMainWindow::CMainWindow(int iWidth, int iHeight, bool bFullScreen) 
  :  m_hWindow(NULL), m_hDeviceContext(NULL), m_hGLContext(NULL), 
     m_bFullScreen(bFullScreen)
{
 RegisterWindowClass();

 RECT WindowRect;
 WindowRect.top = WindowRect.left = 0;
 WindowRect.right = iWidth;
 WindowRect.bottom = iHeight;

 // Window Extended Style
 DWORD dwExStyle = 0; 
 // Windows Style
 DWORD dwStyle = 0;  

 if (m_bFullScreen)
 {
  DEVMODE dmScreenSettings;
  memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); 
  dmScreenSettings.dmSize = sizeof(dmScreenSettings);  
  dmScreenSettings.dmPelsWidth = iWidth;   
  dmScreenSettings.dmPelsHeight = iHeight;  
  dmScreenSettings.dmBitsPerPel = 32;  
  dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;

  // Change the display settings to fullscreen. On error, throw 
  // an exception.
  if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)
    != DISP_CHANGE_SUCCESSFUL)
  {
   throw CException("Unable to swith to fullscreen mode");
  }

  dwExStyle = WS_EX_APPWINDOW; 
  dwStyle = WS_POPUP;  
  // In fullscreen mode, we hide the cursor.
  ShowCursor(FALSE);
 }
 else
 {
  dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  dwStyle = WS_OVERLAPPEDWINDOW; 
 }

 // Adjust the window to the true requested size
 AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);  
 // Now create the main window
    m_hWindow = CreateWindowEx(dwExStyle,TEXT(WINDOW_CLASSNAME), 
        TEXT("Tutorial1"), 
        WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
        0, 0, WindowRect.right-WindowRect.left, 
        WindowRect.bottom-WindowRect.top, 
        NULL, NULL, 
        GetModuleHandle(NULL), 
        this);
 if (m_hWindow==NULL)
  throw CException("Cannot create the main window");

 CreateContext();
 InitGL();
 ShowWindow(m_hWindow,SW_SHOW);
 // Call OnSize manually because in fullscreen mode it will be 
 // called only when the window is created (which is too early
 // because OpenGL is not initialized yet).
 OnSize(iWidth,iHeight);
}




LRESULT CMainWindow::OnEvent(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam)
{
 if (Message == WM_NCCREATE)
 {
        // Get the creation parameters.
  CREATESTRUCT* pCreateStruct = reinterpret_cast<CREATESTRUCT*>(lParam);

        // Set as the "user data" parameter of the window
        SetWindowLongPtr(Handle, GWLP_USERDATA, 
       reinterpret_cast<long>(pCreateStruct->lpCreateParams));
 }

    // Get the CMainWindow instance corresponding to the window handle
    CMainWindow* pWindow = reinterpret_cast<CMainWindow*>
  (GetWindowLongPtr(Handle, GWLP_USERDATA));
 if (pWindow)
  pWindow->ProcessEvent(Message,wParam,lParam);

    return DefWindowProc(Handle, Message, wParam, lParam);
}



void CMainWindow::ProcessEvent(UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch (Message)
    {
        // Quit when we close the main window
        case WM_CLOSE :
            PostQuitMessage(0);
   break;
  case WM_SIZE:
   OnSize(LOWORD(lParam),HIWORD(lParam));
   break;
        case WM_KEYDOWN :
            break;
        case WM_KEYUP :
            break;
    }
}





 

你可能感兴趣的:(Window wndproc to class method)