英文:
SetWindowPos not setting Z order properly
问题
我正在开发一个窗口管理器。以下是一些将窗口以对角线堆叠的代码:
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 后,我得到相同的结果。它们出现的顺序有时是正确的,但似乎非常随机。这是为什么呢?
编辑:显示问题的图片:
英文:
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:
答案1
得分: 1
正如Jonathan Potter所说,窗口定位是异步的。
在调用SetWindowPos()
之后加入Sleep()
函数可以起作用。
英文:
As Jonathan Potter said, window positioning is asynchronous.
Putting a Sleep()
in after the call to SetWindowPos()
works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论