L
L
LoliDeveloper2021-10-31 13:55:35
API
LoliDeveloper, 2021-10-31 13:55:35

How to create an OpenGL window in a window received from CreateWindowEx (WINAPI)?

Here I am creating a window in Windows.

HWND hwnd = CreateWindowEx(
    0,                              // Optional window styles.
    CLASS_NAME,                     // Window class
    L"Learn to Program Windows",    // Window text
    WS_OVERLAPPEDWINDOW,            // Window style

    // Size and position
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

    NULL,       // Parent window    
    NULL,       // Menu
    hInstance,  // Instance handle
    NULL        // Additional application data
    );

if (hwnd == NULL)
{
    return 0;
}

And so the OpenGL window
/* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

How do I create an OpenGL window inside a windows window?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2021-10-31
@LoliDeveloper

Using glfw often leads to the fact that the usual OpenGL things cannot be done using glfw.
Within glfw, an OpenGL context is directly associated with an operating system window in type GLFWwindow[ ? ].
It will no longer be possible to separate them or use them separately. glfw does not provide such an interface.
Also, in the function glfwCreateWindow[ ? ] it is also impossible to pass the operating system window handle if you want to create a child window in your own.
It's such a trap to use glfw. Either the user uses glfw completely from the start and puts up with the limitations, or he refuses to use glfw and does everything by hand.
The most that can be done with glfw is to create several different windows, each of which will be associated with its unique OpenGL context. But you will have to work with such windows only and strictly in different threads, because An OpenGL context is bound only to a particular thread, on which the OpenGL functions need to be called for that context. In other words, such an approach can mean great difficulties in implementation.
If you look closely at the signature glfwCreateWindow, you will notice the last parameter, which allows you to specify another window whose resources should be shared with the new window being created. Using this option, you can create multiple windows with shared resource management, but you can still draw in these windows only on different threads.
To solve the problem of more flexible creation of windows and drawing in them using OpenGL, it is better to go from the WGL[ ? ] and direct work with Win32 API. In this case, you can even get by with one context, binding it either to one window or to another for the duration of rendering.
But if you just want to add interface elements to your current window, then it will be better and more convenient to take imgui or a similar library. To do this, glfw can not be abandoned.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question