How to set window startup position in Win32?

i am wondering if there is any way to change the window startup position in Win32 projects

i tried messing around in AppDelegate::applicationDidFinishLaunching() method but with no good results

my first try was using glfwSetWindowPos() but it didnt work as nothing changes when calling it

bool AppDelegate::applicationDidFinishLaunching() {

......

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)

		auto rect = cocos2d::Rect(Vec2(), designResolutionSize);
		glview = GLViewImpl::createWithRect("MyGame", rect);

		GLViewImpl* glviewImp = (GLViewImpl*)glview;
		GLFWwindow* win = glviewImp->getWindow();

		glfwSetWindowPos(win, x, y);
		
		//glfwSetWindowCenter(win);
.....
}

i also tried using a method that i found online and unfortunately it also didnt work

int glfwSetWindowCenter(GLFWwindow * window)
{
	int sx = 0, sy = 0;
	int px = 0, py = 0;
	int mx = 0, my = 0;
	int monitor_count = 0;
	int best_area = 0;
	int final_x = 0, final_y = 0;
	int j;

	GLFWmonitor ** m = NULL;

	if (!window)
		return 0;

	glfwGetWindowSize(window, &sx, &sy);
	glfwGetWindowPos(window, &px, &py);

	m = glfwGetMonitors(&monitor_count);

	if (!m)
		return 0;

	for (j = 0; j<monitor_count; ++j)
	{
		const GLFWvidmode * mode = NULL;
		int minX;
		int minY;
		int maxX;
		int maxY;
		int area;

		glfwGetMonitorPos(m[j], &mx, &my);
		mode = glfwGetVideoMode(m[j]);

		if (!mode)
			continue;

		minX = mx>px ? mx : px;
		minY = my>py ? my : py;

		maxX = mx + mode->width <px + sx ? mx + mode->width : px + sx;
		maxY = my + mode->height<py + sy ? my + mode->height : py + sy;

		area = (maxX - minX>0 ? maxX - minX : 0)*(maxY - minY>0 ? maxY - minY : 0);
		if (area>best_area)
		{
			final_x = mx + (mode->width - sx) / 2;
			final_y = my + (mode->height - sy) / 2;
			best_area = area;
		}
	}

	if (best_area)
		glfwSetWindowPos(window, final_x, final_y);
	else
	{
		GLFWmonitor * primary = glfwGetPrimaryMonitor();
		if (primary)
		{
			const GLFWvidmode * desktop = glfwGetVideoMode(primary);

			if (desktop)
				glfwSetWindowPos(window, (desktop->width - sx) / 2, (desktop->height - sy) / 2);
			else
				return 0;
		}
		else
			return 0;
	}
	return 1;
}

when i debug the method glfwSetWindowCenter() i found that it stops on the function call to glfw Get Monitor() and it returns null? so as it seems it doesn’t recognize my monitor some how?

Goto definition of
glview = GLViewImpl::createWithRect(…
in GLViewImpl::initWithRect
after glfwCreateWindow

add this line
glfwSetWindowPos(_mainWindow, 100, 100); // x & y desired position[quote=“Joseph39, post:1, topic:37947”]
when i debug the method glfwSetWindowCenter() i found that it stops on the function call to glfw Get Monitor() and it returns null? so as it seems it doesn’t recognize my monitor some how?
[/quote]
It will work in main thread only.

3 Likes

problem solved i added a call to glfwSetWindowCenter(_mainWindow) just after glfwCreateWindow() located in GLViewImpl::initWithRect and the window is centered just like i wanted it !!!