GLFW: size, position & monitorsGLFW 1.0

glfwSetWindowPos

Moves the window so the top-left corner of its CONTENT area lands at (xpos, ypos) on the desktop.

Signature

void glfwSetWindowPos(GLFWwindow* window, int xpos, int ypos);

How it works

Positions are in screen coordinates on the virtual desktop that spans every monitor: (0, 0) is the top-left of the primary monitor, and y grows downwards (unlike OpenGL).

The point is the corner of the content, not of the title bar. So placing the window at y = 0 puts its title bar above the screen; add the top size from glfwGetWindowFrameSize.

To avoid a visible jump, create the window hidden (hint GLFW_VISIBLE = GLFW_FALSE), move it, then call glfwShowWindow. GLFW 3.4 can also open it in place with the hints GLFW_POSITION_X / GLFW_POSITION_Y.

Parameters

windowGLFWwindow*

The window to move.

xposint

New x of the content area's left edge, in screen coordinates.

yposint

New y of the content area's top edge, in screen coordinates (y down).

Tips & pitfalls

  • Wayland does not let applications place their windows. There the call does nothing and reports GLFW_FEATURE_UNAVAILABLE.

  • It has no effect on a full-screen window.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GLFW_NOT_INITIALIZED
Called before glfwInit or after glfwTerminate.
GLFW_FEATURE_UNAVAILABLE
This platform cannot do it; Wayland, for example, does not let a program move its windows.
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1// center a hidden wĂ—h window on the primary monitor's work area
2int ax, ay, aw, ah, l, t, r, b;
3glfwGetMonitorWorkarea(glfwGetPrimaryMonitor(), &ax, &ay, &aw, &ah);
4glfwGetWindowFrameSize(window, &l, &t, &r, &b);
5glfwSetWindowPos(window,
6    ax + (aw - (w + l + r)) / 2 + l,
7    ay + (ah - (h + t + b)) / 2 + t);
8glfwShowWindow(window);
Official GLFW documentation