Actually, Gui,Show has two no-activate options.
NoActivate: Unminimizes or unmaximizes the window, if necessary. The window is also shown without activating it.
NA: Shows the window without activating it.
My only question, how do I make closing the cmd prompt NOT kill my script?
The close event can be intercepted by using SetConsoleCtrlHandler(), but it seems while that allows the script to clean up properly, the process is always terminated after some system-defined timeout.
The following script demonstrates two things:[*:2q7rsgxs]Using a mouse hotkey and WM_NCHITTEST to prevent the script from exiting when the user presses the close button.
[*:2q7rsgxs]Using SetConsoleCtrlHandler() to clean up properly (i.e. remove the tray icon) when the console is closed some other way (i.e. sysmenu->Close.)
#NoEnv
DllCall("AllocConsole")
if (hwnd := DllCall("GetConsoleWindow"))
GroupAdd, MyConsole, ahk_id %hwnd%
DllCall("SetConsoleCtrlHandler", "uint", RegisterCallback("HandlerRoutine"), "int", 1)
return
HandlerRoutine(dwCtrlType)
{
if dwCtrlType = 2 ; CTRL_CLOSE_EVENT
ExitApp ; clean up properly
return false
}
#IfWinActive ahk_group MyConsole
LButton::Click Down
LButton Up::
CoordMode, Mouse, Screen
MouseGetPos, x, y
SendMessage, 0x84,, (x & 0xFFFF) | (y & 0xFFFF) << 16 ; WM_NCHITTEST
if (ErrorLevel = 20) ; close button
DllCall("FreeConsole")
Click Up
return
Unfortunately (for me), WM_NCHITTEST doesn't work accurately with Windows Vista's Aero interface: It returns HTMAXBUTTON, HTTOPRIGHT, HTTOP or HTCLOSEBUTTON depending on where in the close button the mouse cursor is.