I am working on a window manager. Here is some code to stack windows diagonally:
void stack_windows_diagonal(HWND* windows, int width, int height){
if(windows == NULL) return;
int size = 0;
for(;windows[size]; size++);
RECT scr_dim;
scr_dim.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
scr_dim.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
scr_dim.right = GetSystemMetrics(SM_CXVIRTUALSCREEN);
scr_dim.bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int xoffset = 40;
int yoffset = 40;
int startx = (scr_dim.right - width) / 2 - (((size - 1) * xoffset) / 2);
int starty = (scr_dim.bottom - height) / 2 - (((size - 1) * yoffset) / 2);
for(int i = size - 1; i >= 0; i--){
bool success = SetWindowPos(windows[i], i == size - 1? HWND_TOP : windows[i - 1], startx + xoffset * i, starty + yoffset * i, width, height, 0);
if(!success) {
DWORD error = GetLastError();
fprintf(stderr, "Failed to reposition window. Error code: %lu\n", error);
continue;
}
if(i < size - 1)
continue;
success = SetForegroundWindow(windows[i]);
if(!success){
DWORD error = GetLastError();
if(GetLastError())
fprintf(stderr, "Failed to bring window to top. Error code: %lu\n", error);
}
}
free(windows);
}
windows
is terminated by NULL. Currently, this is only used for managing file explorer windows. The appear at the right x y coordinates, but the Z order is often very wrong. I get the same result setting all windows to HWND_TOP. The order they appear in is sometimes correct, but it seems very random. Why is this?
EDIT: Image showing the issue:
As Jonathan Potter said, window positioning is asynchronous.
Putting a
Sleep()
in after the call toSetWindowPos()
works.