PDA

View Full Version : Troubles with creating a window class C++



gandalfpopstar
03-10-2016, 02:23 AM
Hello, I`m new to OpenGl.
I used code from NeHe lessons and it worked well, but then I tried to move it to separate class. That`s the place where I get troubles.
After moving everything to class I found out that CreateWindowEx returns NULL and, thus, hWnd is pointing nowhere.
if( !( hWnd = CreateWindowEx( dwExStyle, _T("OpenGL"), title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
0, 0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top,
NULL, NULL, hInstance, this ) ) ) {
KillGLWindow();
MessageBox( NULL, L"Window Creation Error.", L"ERROR", MB_OK | MB_ICONEXCLAMATION );
return false;
}

As far as I know CreateWindowEx returns NULL usually when programm doesn`t handle WM_NCCREATE correctly. In changes I only created 2 new functions to solve trouble with the lpfnWndProc member of the WNDCLASS. I assigned &OpenGLInterface::InitialWndProc to lpfnWndProc.

LRESULT CALLBACK OpenGLInterface::InitialWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
if (Msg == WM_NCCREATE) {
LPCREATESTRUCT create_struct = reinterpret_cast<LPCREATESTRUCT>(lParam);
void *lpCreateParam = create_struct->lpCreateParams;
OpenGLInterface *this_window = reinterpret_cast<OpenGLInterface *>(lpCreateParam);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this_window));
SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&OpenGLInterface::StaticWndProc));
return this_window->WndProc(hWnd, Msg, wParam, lParam);
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}

LRESULT CALLBACK OpenGLInterface::StaticWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
LONG_PTR user_data = GetWindowLongPtr(hWnd, GWLP_USERDATA);
OpenGLInterface *this_window = reinterpret_cast<OpenGLInterface *>(user_data);
return this_window->WndProc(hWnd, Msg, wParam, lParam);
}

But something went wrong and now it creates a window that doesn`t have connection with keyboard and I even`t can`t draw a line on it.
Debug shows that before SetWindowLongPtr this_window point at OpenGLInterface class as it was expected. Anything else wasn`t changed, thus, I assume that the problem is somewhere here.
Thank you for any help

P. S. I also share links to github commits
https://github.com/a-einstein1879/MarketProject/commit/17b456ad3443b414f27629e728fb3a7b3d4aed39
and
https://github.com/a-einstein1879/MarketProject/commit/ab34dc296a12830817f9e0945f3d5f9c7ea4d272
In case anyone wants to see the whole code
I`m sorry for comments in russian, they were copied from russian translation of NeHe lessons