No window,build hangs - embedding glfw window within winforms


#1

I am trying to embed glfw window generated using glfwCreateWindow() as child window within a winform.
No window appear.Can you help me in embedding glfw window as child window in winforms?

Please find the code below

if( !glfwInit() )
	{
		fprintf( stderr, "Failed to initialize GLFW\n" );
		getchar();
		return -1;
	}

	glfwWindowHint(GLFW_SAMPLES, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
	glfwWindowHint(GLFW_FOCUSED, GL_FALSE);
	glfwWindowHint(GLFW_DECORATED, GL_FALSE);

	// Open a window and create its OpenGL context
	window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
	if( window == NULL ){
		fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
		getchar();
		glfwTerminate();
		return -1;
	}    	

	HWND hWnd = glfwGetWin32Window(window);
	SetParent(hWnd, pFrame); // HWND pFrame is the parent window handle
	LONG nNewStyle = GetWindowLong(hWnd, GWL_STYLE) & ~WS_POPUP | WS_CHILDWINDOW;
	SetWindowLong(hWnd, GWL_STYLE, nNewStyle);
	ULONG_PTR cNewStyle = GetClassLongPtr(hWnd, GCL_STYLE) | CS_DBLCLKS;
	SetClassLongPtr(hWnd, GCL_STYLE, cNewStyle);

	glfwShowWindow(window);

	glfwMakeContextCurrent(window);

	// Initialize GLEW
	if (glewInit() != GLEW_OK) {
		fprintf(stderr, "Failed to initialize GLEW\n");
		getchar();
		glfwTerminate();
		return -1;
	}

	// Ensure we can capture the escape key being pressed below
	glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

#2

I am not sure that what you are trying to do is possible without altering the code of GLFW itself. There is an issue about creating a GLFW with another window’s hwnd on GLFW Github, and one comment shows how to alter GLFW to create the window as a child window.

Perhaps an alternative approach might work - for example if you just want to have a GUI with the GLFW window then you can use a graphics API based GUI - I use dear imgui but there are many others. Alternatively you could use a GUI system which has native support for OpenGL such as Qt.

Finally if you already have a window with WinForms you can use it’s hWnd to create an OpenGL context without using GLFW.


#3

Dear Dough,

Once again for helping me to save a lot of time. :slightly_smiling_face::slightly_smiling_face::slightly_smiling_face:

without altering the code of GLFW itself

I think this is great and you have already provided me links to have good understanding.I want to use glfw for my application.

you can use a graphics API based GUI

I will also go thorough the imgui library.Thanks for these coordinates.