My window menu gets corrupted as soon as GLFW window is created

Hi All,

I’m currently working on Windows Application with GLFW on 4 different viewports (4 separated child windows).
I’ve attached window menu when to create main window as below,

bool InitInstance(HINSTANCE hInstance) {
    .
    .
    .
    // Load main menu
    mainMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDC_POINT3D));

    // Create window
    mainHwnd = CreateWindow((LPCSTR)application.className,
        (LPCSTR)Title,
        Style,
        X,
        Y,
        Width,
        Height,
        HWND_DESKTOP,
        mainMenu,
        hInstance,
        NULL
    );
    .
    .
    .
    return 0;
}

and then created GLFW window like below

int CreateGLWindows(GL_Window& glWindow) {
    // GLFW: Initialize and configure
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    #ifdef __APPLE__
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    #endif

    glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
    glfwWindowHint(GLFW_DECORATED, GL_FALSE);
    glfwWindowHint(GLFW_FOCUSED, GL_TRUE);

    // GLFW window creation
    glWindow.glfwHandle = glfwCreateWindow(glWindow.Width, glWindow.Height, glWindow.className, NULL, NULL);
    glWindow.hWnd = glfwGetWin32Window(glWindow.glfwHandle);

    if (glWindow.glfwHandle == NULL) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    // Refine window style
    long style = GetWindowLong(glWindow.hWnd, GWL_STYLE);
    style &= ~(WS_POPUP | WS_CAPTION); // remove popup style
    style |= WS_CHILD | CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    SetWindowLong(glWindow.hWnd, GWL_STYLE, style);

    // GLAD: Load all OpenGL function pointers
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }
    .
    .
    .
    SetParent(glWindow.hWnd, mainHwnd);

    glfwSetWindowPos(glWindow.glfwHandle, glWindow.x, glWindow.y);
    glfwShowWindow(glWindow.glfwHandle);

    return 0;
}

Can you please share me out how to fix the issue below? or do I need to make my custom menu?

Thanks in advance,

Don’t try to manipulate windows created by GLFW (or any other toolkit) via Win32 API calls. The toolkit is going to assume that a window’s state will remain unchanged unless modified by the toolkit itself.

If you want to use the Win32 API to manipulate windows, use it to create them, and just don’t use GLFW at all (other than perhaps as a reference for how to create and manage contexts via the wgl* functions).

OpenGL toolkits have two purposes: one is to provide a cross-platform framework (which is pointless if you’re going to use platform-specific calls), the other is to eliminate “boilerplate” code. The latter is more relevant if you’re writing textbook examples or exploratory test programs. If you’re writing a “real” application, it’s only going to reduce the size of the code by trivial amount.

If you’re using GLFW because you don’t know how to manage contexts, it shouldn’t take more than a day to learn if you use an existing toolkit as a reference. Also, see the wiki.

1 Like

CS_* styles are also not valid for use here; these are class styles, not window styles, so the correct place to set them is in the WNDCLASS struct used by RegisterClass. This is all native Windows API stuff, of course - using GLFW it’s not exposed to you.

1 Like