Create OpenGL window win32 API

1.2k Views Asked by At

I am trying to create an OpenGL window inside of already existing parent window using win32 api. However, when creating a child, the function CreateWindowEx returns NULL with an error 1407 ERROR_CANNOT_FIND_WND_CLASS. The window must be a child so there is a space left for other controls such as buttons and checkboxes...

Part of code that creates the parent window:

WNDCLASSEX wincl;
wincl.style = 0;
wincl.lpszMenuName = NULL;
wincl.lpszClassName = L"WindowClass";
wincl.lpfnWndProc = WndProc;
wincl.hInstance = hInstance;
wincl.hIconSm = LoadIcon(NULL, IDC_ICON);
wincl.hIcon = LoadIcon(NULL, IDC_ICON);
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wincl.cbWndExtra = 0;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.cbClsExtra = 0;

if(!RegisterClassEx(&wincl)){
    std::cout << "Window failed to register!" << std::endl;
    return false;
}

DWORD style = WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU | WS_SIZEBOX;

parentHwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                      L"WindowClass",
                      L"Title",
                      style,
                      CW_USEDEFAULT, // X
                      CW_USEDEFAULT, // Y
                      800, // Width
                      600, // Height
                      NULL,
                      NULL,
                      hInstance,
                      0);

if (parentHwnd == NULL){
    std::cout << "Window failed to create!" << std::endl;
    return false;
}

ShowWindow(parentHwnd, SW_SHOWNORMAL);
UpdateWindow(parentHwnd);

Inside the message loop, I am creating the child window:

LRESULT WndProc(HWND hwnd, UINT Msg, WPARAM wParam , LPARAM lParam){
    switch (Msg){
        case WM_CREATE:{
            // Create child OpenGL window
            childHwnd = CreateWindowEx(0,
                                   L"OpenGL",
                                   NULL,
                                   WS_CLIPCHILDREN | WS_VISIBLE,
                                   100, // X
                                   100, // Y
                                   400, // Width
                                   300, // Height
                                   hwnd, // Parent
                                   0, // ID
                                   NULL,
                                   NULL);
            // Prints 1407 ERROR_CANNOT_FIND_WND_CLASS
            std::cout << "Last error: " << GetLastError() << std::endl;
        }
        default:{
            return DefWindowProc(hwnd, Msg, wParam, lParam);
        }
    }
    return 0;
}

I have looked into several tutorials (NeHe, cs.rit.edu, and many other but I can't post more than 2 links) and all of them used L"OpenGL" as class name for the second argument in the function CreateWindowEx().

What am I doing wrong? Is "OpenGL" a valid class name such as "Edit" / "Static" / "Button" ? Can I replace it with something else? I tried "Static" but it did not rendered anything.

I know there are many libraries (GLFW, SDL, ...) that handle window creation, but I need to use pure win32 API.

UPDATE:

RegisterClass() solved the problem. Here is the code that works for me:

WNDCLASS wc      = {0}; 
wc.lpfnWndProc   = WndProc;
wc.hInstance     = hInstance; // Same hinstance used by parent
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"OpenGL";
wc.style = CS_OWNDC;

if(!RegisterClass(&wc)){
    std::cout << "Failed to register window!" << std::endl;
    return false;
}

childHwnd = CreateWindowEx(0, // Must be zero
                           L"OpenGL",
                           NULL,
                           WS_VISIBLE | WS_CHILD,
                           100, // X
                           100, // Y
                           400, // Width
                           300, // Height
                           parentHwnd, // Parent HWND
                           0, // ID
                           hInstance, // Same hinstance used by parent
                           NULL);
0

There are 0 best solutions below