我正在开发一个窗口管理器。下面是一些对角堆叠窗口的代码:
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
以 NULL 终止。目前,这仅用于管理文件资源管理器窗口。它们出现在正确的 x y 坐标处,但 Z 顺序通常非常错误。将所有窗口设置为 HWND_TOP 得到相同的结果。它们出现的顺序有时是正确的,但看起来很随机。这是为什么?
编辑:显示问题的图像: