Set window on top with undocummented SetWindowBand

Post your working scripts, libraries and tools for AHK v1.1 and older
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Set window on top with undocummented SetWindowBand

16 Aug 2021, 17:41

Here is link to this api, the work of very good reverse engineer ADeltaX.
https://blog.adeltax.com/window-z-order-in-windows-10/
To call SetWindowBand We need to inject dll into explorer.exe, set hook to SetWindowBand and show/hide start menu.
Here is script that injects autohotkey.dll and sets hook with minhook.dll
Need to download wrapper by tmplinshi
https://github.com/tmplinshi/MinHook-AHK
And run this code from example folder.
Pressing f11 will set the highest band to active window, equal to osk or magnifier window.

Code: Select all

SetWorkingDir %A_ScriptDir%
#NoEnv
#Persistent
#SingleInstance Force
if (A_Is64bitOS && A_PtrSize = 4)
{
   Run, %A_AhkPath%\..\AutoHotkeyU64.exe "%A_ScriptFullPath%", %A_ScriptDir%
   ExitApp
}

f11::
ourHwnd := WinExist("A")
ourBand := 2   ; ZBID_UIACCESS

code =
(LTrim
   SetWorkingDir, %A_ScriptDir%
   #Include %A_ScriptDir%\Lib\MinHook.ahk
   address_SetWindowBand := DllCall("GetProcAddress", Ptr, DllCall("GetModuleHandle", Str, "user32", "Ptr"), AStr, "SetWindowBand", "Ptr")
   hook1 := New MinHook("", address_SetWindowBand, "SetWindowBand_Hook")
   hook1.Enable()
   return

   SetWindowBand_Hook(hWnd, hwndInsertAfter, dwBand)
   {
      global hook1
      return DllCall(hook1.original, "ptr", %ourHwnd%, "ptr", 0, "uint", %ourBand%)
   }
)
codeUnhook =
(LTrim
   hook1 := ""
)

shellHwnd := DllCall("GetShellWindow", "ptr")
WinGet, pid, PID, ahk_id %shellHwnd%
dllFile := FileExist("AutoHotkeyMini.dll") ? A_ScriptDir "\AutoHotkeyMini.dll"
          : (A_PtrSize = 8)                  ? A_ScriptDir "\ahkDll\x64\AutoHotkeyMini.dll"
          : A_ScriptDir "\ahkDll\x32\AutoHotkeyMini.dll"
rThread := InjectAhkDll(pid, dllFile, "")
rThread.Exec(code)
AppVisibility := ComObjCreate(CLSID_AppVisibility := "{7E5FE3D9-985F-4908-91F9-EE19F9FD1514}", IID_IAppVisibility := "{2246EA2D-CAEA-4444-A3C4-6DE827E44313}")
loop 2
{
   if (A_Index = 1)
      visible := 1
   else
      visible := 0
   loop
   {
      send {LWin down}
      sleep 50
      send {LWin up}
      loop 200   ; wait no more than 2 seconds
      {
         if (DllCall(NumGet(NumGet(AppVisibility+0)+4*A_PtrSize), "Ptr", AppVisibility, "Int*", fVisible) = 0)
         {
            if (fVisible = visible)
               break 2
            sleep 10
         }
      }
   }
}
ObjRelease(AppVisibility)
rThread.Exec(codeUnhook)
rThread:=""
return
Last edited by malcev on 24 May 2023, 18:04, edited 3 times in total.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 239 guests