如何设置在 autohotkey 中打开新窗口时触发的操作?例如,我可以设置任何 Windows 资源管理器窗口,使其在打开时立即标记为“始终在最前面”。或者,我可以将任何新的 Skype 窗口设置为始终在第二台显示器上打开,或始终为半透明,等等...
我认为 autohotkey 可以实现这一点,但我不知道如何实现。我需要它在新窗口出现时立即执行一些任意操作。
答案1
AutoHotkey 是专为热键创建的衍生产品......
你可能想尝试一下原版自动识别相反,它有很多自动化功能,包括窗口事件!
更具体地说,你可以在这里找到脚本跟踪窗口并对新窗口采取行动。
#include <Array.au3>
; Initialize tracking arrays
Global $avWinListPrevious[1][2] = [[0, ""]], $avWinListCurrent
; Monitor unique window handles
While 1
$avWinListCurrent = WinList("[REGEXPTITLE:.+[ \- ]GIMP]", "GNU Image Manipulation Program")
For $n = $avWinListCurrent[0][0] To 1 Step -1
; Check has title and visible
If ($avWinListCurrent[$n][0] <> "") And BitAND(WinGetState($avWinListCurrent[$n][1]), 2) Then
; Check for already seen
$fFound = False
For $i = 1 To $avWinListPrevious[0][0]
If $avWinListCurrent[$n][1] = $avWinListPrevious[$i][1] Then
$fFound = True
ExitLoop
EndIf
Next
; New window found
If Not $fFound Then
WinMove("[REGEXPTITLE:.+[ \- ]GIMP]", "GNU Image Manipulation Program", 169, 0, 893, 771 )
EndIf
Else
_ArrayDelete($avWinListCurrent, $n)
EndIf
Next
$avWinListCurrent[0][0] = UBound($avWinListCurrent) - 1
$avWinListPrevious = $avWinListCurrent
Sleep(500)
WEnd
答案2
像这样的事情会很好用: http://www.autohotkey.com/forum/topic63673.html
如果链接因任何原因发生变化或中断:
;========================================================================
;
; Template: WinTrigger (former OnOpen/OnClose)
; Description: Act upon (de)activation/(un)existance of programs/windows
; Online Ref.: http://www.autohotkey.com/forum/viewtopic.php?t=63673
;
; Last Update: 15/Mar/2010 17:30
;
; Created by: MasterFocus
; http://www.autohotkey.net/~MasterFocus/AHK/
;
; Thanks to: Lexikos, for improving it significantly
; http://www.autohotkey.com/forum/topic43826.html#267338
;
;========================================================================
;
; This template contains two examples by default. You may remove them.
;
; * HOW TO ADD A PROGRAM to be checked upon (de)activation/(un)existance:
;
; 1. Add a variable named ProgWinTitle# (Configuration Section)
; containing the desired title/ahk_class/ahk_id/ahk_group
;
; 2. Add a variable named WinTrigger# (Configuration Section)
; containing the desired trigger ("Exist" or "Active")
;
; 3. Add labels named LabelTriggerOn# and/or LabelTriggerOff#
; (Custom Labels Section) containing the desired actions
;
; 4. You may also change CheckPeriod value if desired
;
;========================================================================
#Persistent
; ------ ------ CONFIGURATION SECTION ------ ------
; Program Titles
ProgWinTitle1 = ahk_class Notepad
WinTrigger1 = Exist
ProgWinTitle2 = Calculator
WinTrigger2 = Active
; SetTimer Period
CheckPeriod = 200
; ------ END OF CONFIGURATION SECTION ------ ------
SetTimer, LabelCheckTrigger, %CheckPeriod%
Return
; ------ ------ ------
LabelCheckTrigger:
While ( ProgWinTitle%A_Index% != "" && WinTrigger := WinTrigger%A_Index% )
if ( !ProgRunning%A_Index% != !Win%WinTrigger%( ProgWinTitle := ProgWinTitle%A_Index% ) )
GoSubSafe( "LabelTriggerO" ( (ProgRunning%A_Index% := !ProgRunning%A_Index%) ? "n" : "ff" ) A_Index )
Return
; ------ ------ ------
GoSubSafe(mySub)
{
if IsLabel(mySub)
GoSub %mySub%
}
; ------ ------ CUSTOM LABEL SECTION ------ ------
LabelTriggerOn1:
LabelTriggerOff1:
LabelTriggerOn2:
MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger:`t" WinTrigger
Return
; ------ END OF CUSTOM LABEL SECTION ------ ------
请注意,我没有编写此代码。
答案3
这是用 AutoHotkey 做的
#Persistent
Gui +LastFound
hWnd := WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd)
MsgNum := DllCall("RegisterWindowMessage", Str,"SHELLHOOK")
OnMessage(MsgNum, "ShellMessage")
Return
ShellMessage(wParam,lParam) {
Local k
If (wParam = 1) { ; HSHELL_WINDOWCREATED := 1
NewID := lParam
SetTimer, Action, -1
} else {
;SetTimer, Action, -1 ; this is to make it execute even on old windows. Probably more CPU intensive.
}
}
Action:
; here put the action that you want to run when a new window is detected
Return