Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating the Z-Order of Many Windows Using Win32 API

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.

like image 932
Greg Shackles Avatar asked Jul 19 '10 15:07

Greg Shackles


People also ask

How do you Z-order a window?

You can use the GetTopWindow function to search all child windows of a parent window and return a handle to the child window that is highest in z-order. The GetNextWindow function retrieves a handle to the next or previous window in z-order.

What is Z ordered list of windows?

Z-order is an ordering of overlapping two-dimensional objects, such as windows in a stacking window manager, shapes in a vector graphics editor, or objects in a 3D application. One of the features of a typical GUI is that windows may overlap, so that one window hides part or all of another.

What is layered window?

Layered WindowsUsing a layered window can significantly improve performance and visual effects for a window that has a complex shape, animates its shape, or wishes to use alpha blending effects. The system automatically composes and repaints layered windows and the windows of underlying applications.

Which function is USED to hide a window in Win32 API?

To hide the window, we first find it's OS handle, then we call some Win32 API functions. To find the handle of the window, we use Spy++. Spy++ comes with Visual Studio, and is bundled in the Express edition (the free version) from 2010 onwards.


1 Answers

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)

like image 50
Anders Avatar answered Oct 01 '22 23:10

Anders