How to center a Window at Screen?

phoenix94 wrote on Wednesday, July 10, 2013:

Hello,

I tried to create a Window with GLFW an then Center it on the Screen with glfwSetWindowPos().
But unfortunately I’m not able to get the current size of my Desktop Screen, to center my window.
Is there a functino for it, or another way to do it?

Thanks for help. :slight_smile:

elmindreda wrote on Wednesday, July 10, 2013:

You can retrieve the current video mode for your chosen monitor and then figure out where to put the window for it to be centred.

Video mode sizes use screen coordinates, same as window positions and sizes.

phoenix94 wrote on Wednesday, July 10, 2013:

Do you mean the glfwGetVideoMode() function? It doesn’t seem to work.

GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());

causes the following error:

error C2440: ‘initialization’: ‘const GLFWvidmode *’ could not be converted into ‘GLFWvidmode *’

By the way, if it is necessary: I use Windows 7 and C++ Express 2010 :slight_smile:

Edit: OK, I was a little bit stupid. -.-

When I use reinterpret_cast all works well. Thank you for your great help. :smiley:

markspeir wrote on Wednesday, July 10, 2013:

I had the same issue.

Change: GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
to: const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());

phoenix94 wrote on Thursday, July 11, 2013:

Thank you all for your Help. It works fine now.

So I think this is a closed topic now? :smiley:

I think something like this might work. Remember to Pre-define the window.

	// Defining a monitor
	GLFWmonitor* monitor = glfwGetPrimaryMonitor();
	const GLFWvidmode* mode = glfwGetVideoMode(monitor);


	// Putting it in the centre
	glfwSetWindowPos(window, mode->width/7, mode->height/7);

You may need to play around with the values but Im sure you’ll get it!

Since GLFW 3.3 there is also glfwGetMonitorWorkarea, which could be helpful when centering.