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.
#include <WinAPI.au3>
$handle = WinGetHandle("A window which you would like to send to the back of all other windows")
_SendToBottom($handle)
Func _LastWindowTitle($z = 1)
If $z < 1 Then Return SetError(1, 0, 0) ; Bad parameter
Local $avList = WinList()
For $n = 1 to $avList[0][0]
If $avList[$n][0] <> "" And BitAND(WinGetState($avList[$n][1]), 2) Then
If $z Then
$z -= 1
Else
Local $arr = [$avList[$n][0],$avList[$n][1]]
Return $arr
EndIf
EndIf
Next
Return SetError(2, 0, 0) ; z-depth exceeded
EndFunc
Func _FastActivate($hWnd)
Opt("WinWaitDelay", 20)
WinActivate($hWnd)
WinWaitActive($hWnd,"",4)
_WinAPI_FlashWindowEx($hWnd,0,0,0)
Opt("WinWaitDelay", 250)
EndFunc
Func _SendToBottom($hWnd)
Local Const $HWND_BOTTOM = 1;
Opt("WinWaitDelay", 20)
Local $active = WinGetHandle("[ACTIVE]")
Local $lastWindow = _LastWindowTitle()
Local $lastHandle = $lastWindow[1]
If Not ($hWnd <> $active) Then
If (WinExists($lastHandle)) Then
_FastActivate($lastHandle)
EndIf
EndIf
DllCall("user32.dll", "long", "SetWindowPos", "uint", $hWnd, "uint", $HWND_BOTTOM , "int", 0, "int", 0, "int", 0, "int", 0 , "uint", 0x13)
WinSetState($hWnd,"",@SW_MINIMIZE)
WinSetState($hWnd,"",@SW_HIDE)
WinSetState($hWnd,"",@SW_SHOW)
;disable the taskbar flashing that sometimes occurs when hiding and showing windows...
_WinAPI_FlashWindowEx($hWnd,0,0,0)
Opt("WinWaitDelay", 250)
EndFunc