使用 Win32 API 更新许多 Windows 的 Z 顺序

发布于 2024-09-10 04:32:27 字数 778 浏览 2 评论 0原文

场景是,我有一个顶级窗口的窗口句柄列表,我想移动它们,以便它们按照我选择的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-09-17 04:32:28

有一组特殊的 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)

故笙诉离歌 2024-09-17 04:32:28

您可以使用 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);
}

You can use SetWindowPos to order your top-level windows.

// 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文