Contrary to my previous post, it seems HWND_MESSAGE only allows it to "recover quicker" - the function still interferes with the active window.
That being the case, I thought I'd make another attempt at preventing it from stealing focus. My attempts at catching window messages, using LockSetForegroundWindow, etc. all failed, so I went all out, and temporarily replaced ShowWindow/SetForegroundWindow:
Loop {
tick := A_TickCount
ToolTip % ListGlobalVars()
}
ListGlobalVars()
{
static hwndEdit, pSFW, pSW, bkpSFW, bkpSW
if !hwndEdit
{
dhw := A_DetectHiddenWindows
DetectHiddenWindows, On
Process, Exist
ControlGet, hwndEdit, Hwnd,, Edit1, ahk_class AutoHotkey ahk_pid %ErrorLevel%
DetectHiddenWindows, %dhw%
astr := A_IsUnicode ? "astr":"str"
ptr := A_PtrSize=8 ? "ptr":"uint"
hmod := DllCall("GetModuleHandle", "str", "user32.dll", ptr)
pSFW := DllCall("GetProcAddress", ptr, hmod, astr, "SetForegroundWindow", ptr)
pSW := DllCall("GetProcAddress", ptr, hmod, astr, "ShowWindow", ptr)
DllCall("VirtualProtect", ptr, pSFW, ptr, 8, "uint", 0x40, "uint*", 0)
DllCall("VirtualProtect", ptr, pSW, ptr, 8, "uint", 0x40, "uint*", 0)
bkpSFW := NumGet(pSFW+0, 0, "int64")
bkpSW := NumGet(pSW+0, 0, "int64")
}
if (A_PtrSize=8) {
NumPut(0x0000C300000001B8, pSFW+0, 0, "int64") ; return TRUE
NumPut(0x0000C300000001B8, pSW+0, 0, "int64") ; return TRUE
} else {
NumPut(0x0004C200000001B8, pSFW+0, 0, "int64") ; return TRUE
NumPut(0x0008C200000001B8, pSW+0, 0, "int64") ; return TRUE
}
ListVars
NumPut(bkpSFW, pSFW+0, 0, "int64")
NumPut(bkpSW, pSW+0, 0, "int64")
ControlGetText, text,, ahk_id %hwndEdit%
RegExMatch(text, "sm)(?<=^Global Variables \(alphabetical\)`r`n-{50}`r`n).*", text)
return text
}
ListGlobalVars() neither shows nor activates the AutoHotkey main window. It is also considerably faster: on my system, 2ms vs 128ms (40ms with SetWinDelay, -1.)
It also doesn't hide the main window if it was already visible, though it does of course overwrite its contents.
---
Edit @ 2010-10-21 - Unicode builds of AutoHotkey_L require Astr vs str (optional for the ANSI build):
pSFW := DllCall("GetProcAddress", "uint", hmod, "Astr", "SetForegroundWindow")
pSW := DllCall("GetProcAddress", "uint", hmod, "Astr", "ShowWindow")
Edit @ 2010-11-12 - Updated code and tested with AutoHotkey v1.0.48.05, AutoHotkey_L ANSI, Unicode and Unicode x64.
Edit @ 2015-09-28 - Fixed x64 bugs.