# How to center a Window at Screen?

**URL:** <https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593>\
**Category:** support\
**Created:** [July 10, 2013, 9:27pm UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593 "2013-07-10T21:27:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 10, 2013, 9:27pm UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/1 "2013-07-10T21:27:07Z")

</div>

**[phoenix94](https://sourceforge.net/u/phoenix94/)** wrote on [Wednesday, July 10, 2013](https://sourceforge.net/p/glfw/discussion/247562/thread/fd42cf21/#2500):

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. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 10, 2013, 9:44pm UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/2 "2013-07-10T21:44:43Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Wednesday, July 10, 2013](https://sourceforge.net/p/glfw/discussion/247562/thread/fd42cf21/#945c):

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 10, 2013, 9:55pm UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/3 "2013-07-10T21:55:33Z")

</div>

**[phoenix94](https://sourceforge.net/u/phoenix94/)** wrote on [Wednesday, July 10, 2013](https://sourceforge.net/p/glfw/discussion/247562/thread/fd42cf21/#945c/97b5):

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 🙂

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

When I use reinterpret\_cast all works well. Thank you for your great help. 😃

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 10, 2013, 10:28pm UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/4 "2013-07-10T22:28:09Z")

</div>

**[markspeir](https://sourceforge.net/u/markspeir/)** wrote on [Wednesday, July 10, 2013](https://sourceforge.net/p/glfw/discussion/247562/thread/fd42cf21/#945c/97b5/a07b):

I had the same issue.

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [July 11, 2013, 6:05am UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/5 "2013-07-11T06:05:59Z")

</div>

**[phoenix94](https://sourceforge.net/u/phoenix94/)** wrote on [Thursday, July 11, 2013](https://sourceforge.net/p/glfw/discussion/247562/thread/fd42cf21/#273c):

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

So I think this is a closed topic now? 😃

---

<div class="post-metadata">

**Author:** ![PROgrammer6152](https://avatars.discourse-cdn.com/v4/letter/p/f6c823/32.png) [@PROgrammer6152](https://discourse.glfw.org/u/PROgrammer6152)\
**Post date:** [May 30, 2023, 12:27am UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/6 "2023-05-30T00:27:20Z")

</div>

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

```auto
	// 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!

---

<div class="post-metadata">

**Author:** ![elmindreda](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/elmindreda/32/26_2.png) [@elmindreda](https://discourse.glfw.org/u/elmindreda)\
**Post date:** [May 31, 2023, 7:39pm UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/7 "2023-05-31T19:39:23Z")

</div>

Since GLFW 3.3 there is also [glfwGetMonitorWorkarea](https://www.glfw.org/docs/3.3/group__monitor.html#ga7387a3bdb64bfe8ebf2b9e54f5b6c9d0), which could be helpful when centering.

---

<div class="post-metadata">

**Author:** ![mikesinclair66](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/mikesinclair66/32/989_2.png) [@mikesinclair66](https://discourse.glfw.org/u/mikesinclair66)\
**Post date:** [December 25, 2024, 8:23am UTC](https://discourse.glfw.org/t/how-to-center-a-window-at-screen/593/8 "2024-12-25T08:23:49Z")

</div>

This works perfect! Was able to do mode-\>width/2, mode-\>height/2 to locate it at the screen’s center.
