使用 Win32 API 更新许多 Windows 的 Z 顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3282328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating the Z-Order of Many Windows Using Win32 API
提问by Greg Shackles
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.
场景是我有一个顶级窗口的窗口句柄列表,我想移动它们,以便它们按照我选择的 z 顺序排列。我首先迭代列表(我希望最后一个窗口结束),调用SetForegroundWindow
每个列表。这似乎在某些时候有效,但并非总是如此,当我在每次通话之间稍微暂停时,情况会有所改善。
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:
它看起来像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);
I'm sure it's something small/obvious I'm missing here but I'm just not seeing it.
我确定这是我在这里遗漏的很小/很明显的东西,但我只是没有看到它。
回答by Anders
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)
有一组特殊的 api 用于为多个窗口设置窗口位置:BeginDeferWindowPos+ DeferWindowPos + EndDeferWindowPos(循环中的 SetWindowPos 当然也可以工作,但它可能会有更多闪烁)
回答by In silico
You can use SetWindowPos
to order your top-level windows.
您可以使用它SetWindowPos
来订购顶级窗口。
// Hypothetical function to get an array of handles to top-level windows
// sorted with the window that's supposed to be topmost at the end of array.
HWND* windows = GetTopLevelWindowsInOrder();
int numWindows = GetTopLevelWindowCount();
for(int i = 0; i < numWindows; ++i)
{
::SetWindowPos(windows[i], HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}