-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathA_DefaultGui.ahk
61 lines (48 loc) · 2.18 KB
/
A_DefaultGui.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
; Link:
; Author:
; Date:
; for: AHK_L
/*
*/
A_DefaultGui(wParam := "") {
; majkinetor : A_DefaultGui() -> www.autohotkey.com/board/topic/24532-function-a-defaultgui/
; Lexikos : GetDefaultGui() -> www.autohotkey.com/board/topic/84414-ahk-l-get-the-current-default-gui-name/
Static Msg := 0 ; has to be 0, otherwise If (wParam = Msg) is true
Static DefaultGui
static first_call := true
If (wParam = Msg) { ; called by OnMessage
DefaultGui := A_Gui
Return 0
}
if(first_call) {
first_call := false
if(OnMessage(msg) != "") ; safety check that the script doesn't already
return, "", ErrorLevel := 1 ; monitor the msg through some other function
try DllCall("User32.dll\RegisterWindowMessage", "Str", "GetDefaultGui", "UInt")
catch
return, "", ErrorLevel := 2 ; and that RegisterWindowMessage worked
}
LastFoundWindow := WinExist() ; save the Last Found Window, if any
Gui, +LastFoundExist ; check if the default Gui exists,
If !WinExist() ; if not, it has to be created
;Gui, New, +LastFound +hwndHDESTROY
; sets the new default gui window, we don't want that
;
Gui, +LastFound +hwndHDESTROY
OnMessage(Msg, A_ThisFunc) ; get the current default Gui
try DllCall("User32.dll\SendMessage", "Ptr", WinExist(), "UInt", Msg, "Ptr", Msg, "Ptr", 0)
catch
return, "", ErrorLevel := 3
OnMessage(Msg, "")
If (HDESTROY) ; if the dummy Gui has been created, destroy it
Gui, %HDESTROY%: Destroy
If (LastFoundWindow) { ; if the Last Found Window had been set, restore it ...
A_DetectHiddenWindowsOld := A_DetectHiddenWindows
DetectHiddenWindows, On ; precaution: window to be restored can be hidden
WinExist("ahk_id" LastFoundWindow)
DetectHiddenWindows, % A_DetectHiddenWindowsOld
}
Else ; ... otherwise clear it
Gui, %A_ThisFunc%_NonexistentGui: +LastFoundExist
Return DefaultGui, ErrorLevel := 0
}