使用 Win32 API 更新许多 Windows 的 Z 顺序
场景是,我有一个顶级窗口的窗口句柄列表,我想移动它们,以便它们按照我选择的 z 顺序排列。我首先迭代列表(我希望最后一个窗口位于顶部),对每个列表调用 SetForegroundWindow
。这似乎在某些时候有效,但并非总是有效,当我在每次通话之间稍微暂停时,情况会有所改善。
有更好的方法吗?
编辑:
看起来 BeginDeferWindowPos
/DeferWindowPos
/EndDeferWindowPos
路线是要走的路。然而,我似乎无法让它同时与多个窗口一起工作。当我将窗口列表限制为单个窗口时,它可以正常工作。当列表有多个窗口时,它似乎只获取其中之一。这是我正在做的伪代码:
HWND[] windows;
HWND lastWindowHandle = 0;
HDWP positionStructure = BeginDeferWindowPos(windows.length);
for (int i = 0; i < windows.length; i++)
{
positionStructure = DeferWindowPos(positionStructure, windows[i],
lastWindowHandle, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
EndDeferWindowPos(positionStructure);
我确信我在这里遗漏了一些小/明显的东西,但我只是没有看到它。
The scenario is that I have a list of window handles to top level windows and I want to shift them around so they are arranged in the z-order of my choosing. I started off by iterating the list (with the window I want to end up on top last), calling SetForegroundWindow
on each one. This seemed to work some of the time but not always, improving a little when I paused slightly in between each call.
Is there a better way to do this?
Edit:
It looks like the BeginDeferWindowPos
/DeferWindowPos
/EndDeferWindowPos
route is the way to go. However, I can't seem to get it to work with more than one window at a time. When I limit the window list to a single window, it works correctly. When the list has multiple windows, it only seems to get one of them. Here is pseudo code of what I'm doing:
HWND[] windows;
HWND lastWindowHandle = 0;
HDWP positionStructure = BeginDeferWindowPos(windows.length);
for (int i = 0; i < windows.length; i++)
{
positionStructure = DeferWindowPos(positionStructure, windows[i],
lastWindowHandle, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
EndDeferWindowPos(positionStructure);
I'm sure it's something small/obvious I'm missing here but I'm just not seeing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一组特殊的 api 用于设置多个窗口的窗口位置: BeginDeferWindowPos + DeferWindowPos + EndDeferWindowPos (循环中的 SetWindowPos 当然也可以工作,但可能会有更多闪烁)
There is a special set of api's for setting window positions for multiple windows: BeginDeferWindowPos + DeferWindowPos + EndDeferWindowPos (SetWindowPos in a loop will also work of course, but it might have more flicker)
您可以使用
SetWindowPos
订购您的顶级窗户。You can use
SetWindowPos
to order your top-level windows.