I had a need to change the z-order of windows and their position in the "alt-tab" list. I spent quite a long time trying to figure out how to make it happen, so I thought I'd post this snippet back since I finally came up with a workable solution.
USE CASE: I bring music applications to the foreground while I'm working/etc via a hotkey. I then press the hotkey again in order to minimize the program. When I did that, however, my alt-tab order was then modified so that I couldn't continue to "alt-tab" between active windows I was working on, which is a distraction that hurt my workflow. I wanted a way to set a window to the back of the alt-tab list and also to change the z-order of a window (two different problems, as it turned out). I've used this with Windows 8.1.
I'm not sure if "WinWaitDelay" is applicable to WinSetState operations. I just have it there for good measure. The _WinAPI_FlashWindowEx bit is needed sometimes to prevent windows from automatically flashing the newly un-hidden window. Also, two ancillary functions are used in order to ensure that a different window is active than the one that is to be sent to the back, since this doesn't seem to work right unless that is the case.
This is another simple function I've found to be quite handy.
USE CASE: Since chrome v32 and later no longer exposes any standard windows API controls, a huge number of "controlsend" based automation functions I was using against webapps broke. I changed things to bring those windows to the foreground instead to send keys and then minimize them again (with the _SendToBottom function I just posted as well), but with the standard 'winactivate' it took too long. This function basically just reduces the winwaitdelay temporarily, but it also disables the automatic flashing of windows in the taskbar which occurs if you activate windows too rapidly, which was extremely annoying. It's not particularly useful if you just want to bring something to the foreground to actively work with, but if the goal is to activate something solely to send a key and then minimize again, it speeds things up quite a bit since it happens almost instantly. Hopefully someone else also finds this useful.