SetWindowPos无法正确设置Z顺序。

huangapple go评论101阅读模式
英文:

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 后,我得到相同的结果。它们出现的顺序有时是正确的,但似乎非常随机。这是为什么呢?

编辑:显示问题的图片:

SetWindowPos无法正确设置Z顺序。

英文:

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 &gt;= 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, &quot;Failed to reposition window. Error code: %lu\n&quot;, error);
            continue;
        }

        if(i &lt; size - 1)
            continue;

        success = SetForegroundWindow(windows[i]);
        if(!success){
            DWORD error = GetLastError();
            if(GetLastError())
                fprintf(stderr, &quot;Failed to bring window to top. Error code: %lu\n&quot;, 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:

SetWindowPos无法正确设置Z顺序。

答案1

得分: 1

正如Jonathan Potter所说,窗口定位是异步的。

在调用SetWindowPos()之后加入Sleep()函数可以起作用。

英文:

As Jonathan Potter said, window positioning is asynchronous.

Putting a Sleep() in after the call to SetWindowPos() works.

huangapple
  • 本文由 发表于 2023年8月8日 21:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定