SetWindowPos无法正确设置Z顺序。

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

SetWindowPos not setting Z order properly

问题

我正在开发一个窗口管理器。以下是一些将窗口以对角线堆叠的代码:

  1. void stack_windows_diagonal(HWND* windows, int width, int height){
  2. if(windows == NULL) return;
  3. int size = 0;
  4. for(;windows[size]; size++);
  5. RECT scr_dim;
  6. scr_dim.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
  7. scr_dim.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
  8. scr_dim.right = GetSystemMetrics(SM_CXVIRTUALSCREEN);
  9. scr_dim.bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN);
  10. int xoffset = 40;
  11. int yoffset = 40;
  12. int startx = (scr_dim.right - width) / 2 - (((size - 1) * xoffset) / 2);
  13. int starty = (scr_dim.bottom - height) / 2 - (((size - 1) * yoffset) / 2);
  14. for(int i = size - 1; i >= 0; i--){
  15. bool success = SetWindowPos(windows[i], i == size - 1? HWND_TOP : windows[i - 1], startx + xoffset * i, starty + yoffset * i, width, height, 0);
  16. if(!success) {
  17. DWORD error = GetLastError();
  18. fprintf(stderr, "Failed to reposition window. Error code: %lu\n", error);
  19. continue;
  20. }
  21. if(i < size - 1)
  22. continue;
  23. success = SetForegroundWindow(windows[i]);
  24. if(!success){
  25. DWORD error = GetLastError();
  26. if(GetLastError())
  27. fprintf(stderr, "Failed to bring window to top. Error code: %lu\n", error);
  28. }
  29. }
  30. free(windows);
  31. }

windows 数组以 NULL 结尾。目前,此代码仅用于管理文件资源管理器窗口。它们出现在正确的 x 和 y 坐标上,但 Z 轴顺序通常是错误的。将所有窗口设置为 HWND_TOP 后,我得到相同的结果。它们出现的顺序有时是正确的,但似乎非常随机。这是为什么呢?

编辑:显示问题的图片:

SetWindowPos无法正确设置Z顺序。

英文:

I am working on a window manager. Here is some code to stack windows diagonally:

  1. void stack_windows_diagonal(HWND* windows, int width, int height){
  2. if(windows == NULL) return;
  3. int size = 0;
  4. for(;windows[size]; size++);
  5. RECT scr_dim;
  6. scr_dim.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
  7. scr_dim.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
  8. scr_dim.right = GetSystemMetrics(SM_CXVIRTUALSCREEN);
  9. scr_dim.bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN);
  10. int xoffset = 40;
  11. int yoffset = 40;
  12. int startx = (scr_dim.right - width) / 2 - (((size - 1) * xoffset) / 2);
  13. int starty = (scr_dim.bottom - height) / 2 - (((size - 1) * yoffset) / 2);
  14. for(int i = size - 1; i &gt;= 0; i--){
  15. bool success = SetWindowPos(windows[i], i == size - 1? HWND_TOP : windows[i - 1], startx + xoffset * i, starty + yoffset * i, width, height, 0);
  16. if(!success) {
  17. DWORD error = GetLastError();
  18. fprintf(stderr, &quot;Failed to reposition window. Error code: %lu\n&quot;, error);
  19. continue;
  20. }
  21. if(i &lt; size - 1)
  22. continue;
  23. success = SetForegroundWindow(windows[i]);
  24. if(!success){
  25. DWORD error = GetLastError();
  26. if(GetLastError())
  27. fprintf(stderr, &quot;Failed to bring window to top. Error code: %lu\n&quot;, error);
  28. }
  29. }
  30. free(windows);
  31. }

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:

确定