Setting tray icon; detecting pause/suspend.

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Nextron
Posts: 1385
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Setting tray icon; detecting pause/suspend.

07 Dec 2016, 10:06

For my main script I want to change the tray icon to make it easier to distinguish from any other temporary/test scripts running. For obvious esthetic reasons, the suspend and/or paused icons should have matching icons. Changing the resources in AutoHotkey.exe isn't sustainable option as it requires repeating the process after updates and is less portable.

Scouring the forum I did find script samples to detect the message sent by clicking the tray or menu option, but those do not detect the Suspend or Pause commands. Considering this is AutoHotkey, I use a hotkey or some other trigger to invoke suspension and not the menu.

• So I can either repeatedly poll both states and act on changes :thumbdown:
• I can replace all code calling Suspend with code that does trigger detection (sample below) :think:

Is there some other method to be notified of the script's change in suspend/pause state?

Code: Select all

F1::Suspend ;does not trigger icon change
F2::Suspend, % Suspend() ;The Suspend function toggles the suspend state, the suspend command is required to keep the hotkey as suspend-permitted
F3::Pause ;does not trigger icon change
F4::Pause()


WM_Command_SetIcon(wParam) {
	;Icon from http://www.iconsdb.com/custom-color/os-windows8-icon.html, 16*16 data copied from hex editor
	static hIcon:=IconHexToHandle("0000010001001010000001002000680400001600000028000000100000002000000001002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080BB800280BB801A80BB803C80BB806080BB808280BB80A480BB80C880BB80E20000000080BB800480BB802480BB804680BB806880BB808A80BB801880BB808680BB80F480BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80D680BB80FA80BB80FF80BB80FF80BB80FF80BB80FF80BB802880BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB805680BB805880BB805880BB805880BB805880BB805880BB800E80BB803880BB805880BB805880BB805880BB805880BB805880BB805880BB805880BB805680BB806480BB806680BB806680BB806680BB806680BB806680BB801080BB804080BB806680BB806680BB806680BB806680BB806680BB806680BB806680BB806480BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80F680BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB802A80BB809E80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F680BB80BC80BB80E680BB80FF80BB80FF80BB80FF80BB80FF80BB802880BB809C80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F6000000000000000080BB800C80BB803080BB805480BB807880BB801680BB807880BB80E280BB80FC80BB80FF80BB80FF80BB80FF80BB80FF80BB80FF80BB80F600000000000000000000000000000000000000000000000000000000000000000000000080BB800A80BB802C80BB805080BB807480BB809880BB80BC80BB80D4FFF00000FA0000000200000002000000020000000200000002000000FFFF0000FFFF00000200000002000000020000000200000002000000FF000000FFF80000")
	,hIconPaused:=IconHexToHandle("000001000100101000000100200068040000160000002800000010000000200000000100200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001A1AA3021A1AA31A1A1AA33C1A1AA3601A1AA3821A1AA3A41A1AA3C81A1AA3E2000000001A1AA3041A1AA3241A1AA3461A1AA3681A1AA38A1A1AA3181A1AA3861A1AA3F41A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3D61A1AA3FA1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3281A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3561A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA30E1A1AA3381A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA3561A1AA3641A1AA3661A1AA3661A1AA3661A1AA3661A1AA3661A1AA3101A1AA3401A1AA3661A1AA3661A1AA3661A1AA3661A1AA3661A1AA3661A1AA3661A1AA3641A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3BC1A1AA3E61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3281A1AA39C1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F600000000000000001A1AA30C1A1AA3301A1AA3541A1AA3781A1AA3161A1AA3781A1AA3E21A1AA3FC1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F60000000000000000000000000000000000000000000000000000000000000000000000001A1AA30A1A1AA32C1A1AA3501A1AA3741A1AA3981A1AA3BC1A1AA3D4FFF00000FA0000000200000002000000020000000200000002000000FFFF0000FFFF00000200000002000000020000000200000002000000FF000000FFF80000")
	,hIconSuspended:=IconHexToHandle("0000010001001010000001002000680400001600000028000000100000002000000001002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000A5FF0200A5FF1A00A5FF3C00A5FF6000A5FF8200A5FFA400A5FFC800A5FFE20000000000A5FF0400A5FF2400A5FF4600A5FF6800A5FF8A00A5FF1800A5FF8600A5FFF400A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFD600A5FFFA00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2800A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FF5600A5FF5800A5FF5800A5FF5800A5FF5800A5FF5800A5FF0E00A5FF3800A5FF5800A5FF5800A5FF5800A5FF5800A5FF5800A5FF5800A5FF5800A5FF5600A5FF6400A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF1000A5FF4000A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF6400A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFBC00A5FFE600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2800A5FF9C00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF6000000000000000000A5FF0C00A5FF3000A5FF5400A5FF7800A5FF1600A5FF7800A5FFE200A5FFFC00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600000000000000000000000000000000000000000000000000000000000000000000000000A5FF0A00A5FF2C00A5FF5000A5FF7400A5FF9800A5FFBC00A5FFD4FFF00000FA0000000200000002000000020000000200000002000000FFFF0000FFFF00000200000002000000020000000200000002000000FF000000FFF80000")
	,hIconSuspendedPaused:=IconHexToHandle("000001000100101000000100200068040000160000002800000010000000200000000100200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001A1AA3021A1AA31A1A1AA33C1A1AA3601A1AA3821A1AA3A41A1AA3C81A1AA3E2000000001A1AA3041A1AA3241A1AA3461A1AA3681A1AA38A1A1AA3181A1AA3861A1AA3F41A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3D61A1AA3FA1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3281A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3F61A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA32A1A1AA39E1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3FF1A1AA3F61A1AA3561A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA30E1A1AA3381A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA3581A1AA35600A5FF6400A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF1000A5FF4000A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF6600A5FF6400A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFF600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2A00A5FF9E00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600A5FFBC00A5FFE600A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FF2800A5FF9C00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF6000000000000000000A5FF0C00A5FF3000A5FF5400A5FF7800A5FF1600A5FF7800A5FFE200A5FFFC00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFFF00A5FFF600000000000000000000000000000000000000000000000000000000000000000000000000A5FF0A00A5FF2C00A5FF5000A5FF7400A5FF9800A5FFBC00A5FFD4FFF00000FA0000000200000002000000020000000200000002000000FFFF0000FFFF00000200000002000000020000000200000002000000FF000000FFF80000")
	
	static init:=WM_Command_SetIcon("SetIcon"), i:=OnMessage(0x111,"WM_COMMAND_SetIcon")
	
	;				Suspend		Pause
	;Tray menu:		65305		65306
	;Window menu:	65404		65403
	
	If (wParam=65305 || wParam=65404) ;suspend
		s:=1
	Else If (wParam=65306 || wParam=65403) ;pause
		p:=1
	Else If (wParam!="SetIcon")
		return
	
	;https://autohotkey.com/board/topic/52211-change-tray-icons-of-ahk-file-pause-suspend-an-normal/#entry326996
	If (s && !A_IsSuspended || p && A_IsSuspended)
		t:="Suspended"
	If (p && !A_IsPaused || s && A_IsPaused)
		t.="Paused"
	If (s || p || wParam="SetIcon")
		Menu, Tray, Icon, % "hicon:*" hIcon%t%,,1	
}

IconHexToHandle(Hex){ ;https://autohotkey.com/board/topic/31044-crazy-scripting-include-an-icon-in-your-script/
	VarSetCapacity( IconData,( nSize:=StrLen(Hex)//2) )
	Loop %nSize% ; MCode by Laszlo Hars: http://www.autohotkey.com/forum/viewtopic.php?t=21172
		NumPut( "0x" . SubStr(Hex,2*A_Index-1,2), IconData, A_Index-1, "Char" )
	Return hIcon:= DllCall( "CreateIconFromResourceEx", UInt,&IconData+22, UInt, NumGet(IconData,14), Int,1, UInt,0x30000, Int,16, Int,16, UInt,0 )
}

Suspend(Mode:="Toggle"){
	If (Mode="Toggle" || Mode="Off" && A_IsSuspended || Mode="On" && !A_IsSuspended)
		DllCall("PostMessage", UInt, A_ScriptHwnd, UInt,0x111, UInt,65404, UInt, 0)
	Return "Permit"
}
Pause(Mode:="Toggle", OperateOnUnderlyingThread:=0){
	If (Mode="Toggle" || Mode="Off" && A_IsPaused || Mode="On" && !A_IsPaused)
		DllCall("PostMessage", UInt, A_ScriptHwnd, UInt,0x111, UInt,65403, UInt, 0)
	If OperateOnUnderlyingThread
		MsgBox Not sure how to handle OperateOnUnderlyingThread.
}
Last edited by Nextron on 14 Dec 2016, 17:02, edited 1 time in total.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Setting tray icon; detecting pause/suspend.

07 Dec 2016, 11:45

Edit 2 or 3 or 4...: I actually opened one of the threads in Nextron's comments. https://autohotkey.com/board/topic/5221 ... ntry326996 Seems like that's simple enough? But when you try to add in hotkeys, the Suspend command causes "inverse" icons as described below, past the other Edit label.


I'm curious as well. Because in my little attempt and little browsing of documentation, I don't see where you can change the Suspended and Paused icons themselves. You can change the icon of a Suspended script, but you don't see that change until you un-suspend it.

You may be able to set a TrayTip that says if a script is Suspended or Paused (not sure how you'd do Paused) and use the Freeze parameter for setting Menu, Tray, Icon,,, 1.

Actually, through Serendipity, I found an answer. For Suspend at least.

Code: Select all

Menu, Tray, Icon,TestScript3.ico,, 1 ; The 1 freezes the icon so the normal Suspend and Pause icons don't appear
OnMessage("0x111","Jello") ; used for right-click menu on tray icon, or external scripts posting messages
^o::
Suspend ; does not trigger the OnMessage, that's why we end up using GoSub
GoSub CheckSuspendedState
return


Jello(wParam){
If (wParam=65305)
GoSub CheckSuspendedState
}

CheckSuspendedState:
If (A_IsSuspended)
Menu, Tray, Icon, TestScript3.ico ; this is an "S" icon
else
Menu, Tray, Icon, TestScript2.ico ; this is a custom green icon
return
I'm not sure how you'd want to manage Pausing. I'm playing around with it though.

But this does suggest that you may need to craft your own commands/subroutines for managing a Suspend/Pause to then execute a GoSub to get the state, if you use those commands in the script.

Edit: This was fun and challenging. The Suspend and Pause commands behave differently, it appears. The Pause command triggers the Jello function (Suspend does not), which really threw me off until I figured that out (but in further testing I'm not entirely sure I have the full picture). There are two three bugs still to sort out.

1) If you right click on the script's tray icon to pause it, but then try to use ^p (Pause command) to unpause it, the tray icon will not update - so the Jello function is not executing. It seems the Jello function has to be executed once more - even it's just right clicking the tray icon (which does post an OnMessage value of 0x111 with a large wParam value per my testing), and then ^p will start to work again. The script will resynchronize itself for the pause state, though in combination with the below, it may take a while.

2) If you do a Pause and Suspend, or vice versa, you won't have an icon that reflects both states. It shouldn't be too tricky to just create a fourth one that does and do some more If checks on the A_IsSuspended and A_IsPaused variables, I'd imagine. But confusion can arise when you unpause or unsuspend a "dual-state" script and the icon changes to be the normal one.

3) There's a problem where if you right click and select suspend, the wrong (opposite) icon is shown. I'm not sure why that is. I fixed that by just creating a second label that does the opposite of the other. (And yeah, those CheckSuspendedState/Inverse labels could totally be ternary'd.)

Code: Select all

Menu, Tray, Icon,TestScript2.ico,, 1
OnMessage("0x111","Jello")
return
^o::
Suspend
GoSub CheckSuspendedState ; the function Jello will NOT execute
return
^p::Pause ; the function Jello will execute, even with the script pausing
; the Pause command seems to NOT run when using ^p after using right click to set the script to Paused, though Jello function fires
; But if you right click on the tray icon, which posts a message triggering the Jello function, ^p can then work. But before Jello executes again, ^p does NOT work indefinitely.

Jello(wParam){
If (wParam=65305)
GoSub CheckSuspendedStateInverse ; for some reason, suspending through the right click tray menu sets the icon to the opposite of what the current Suspend state
If (wParam=65306)
	Menu, Tray, Icon, % (A_IsPaused)?"TestScript2.ico":"TestScript.ico" ; reflects PAST state. 2 is my NORMAL icon, no-num is my PAUSE icon
	; A_IsPaused reflects the PAST state. I guess the variable doesn't update once the function has been launched, even adding a Sleep to give it time to process the native Pause command doesn't work. 
}

CheckSuspendedState:
If (A_IsSuspended)
Menu, Tray, Icon, TestScript3.ico ; this is an "S" icon
else
Menu, Tray, Icon, TestScript2.ico ; this is a custom green icon
return

CheckSuspendedStateInverse:
If !(A_IsSuspended)
Menu, Tray, Icon, TestScript3.ico ; this is an "S" icon
else
Menu, Tray, Icon, TestScript2.ico ; this is a custom green icon
return
Icons for anyone who wants them; I don't have the icontohex function or whatever Nextron's original code used. Yes they're pretty shoddy, especially the "S" icon. These images are uploaded to imgur as .bmp, but if you change the extension to .ico during/after saving, they should work alright.

http://imgur.com/a/A383I
User avatar
Nextron
Posts: 1385
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Setting tray icon; detecting pause/suspend.

13 Dec 2016, 18:59

Thank you Exaskryz for spending so much time on this. Your Jello function monitors the same message as my WM_Command_SetIcon. I think the inversing is due to the function being triggered by the pause/suspend menu but executing before AHK actually toggling the state so A_IsPaused/A_IsSuspended aren't yet updated to the state they are becoming. I tried to account for that in my code as well.

I going through some parts of AHK's source to see if it helps me come up with other ideas to tackle this.
Helgef
Posts: 4664
Joined: 17 Jul 2016, 01:02
Contact:

Re: Setting tray icon; detecting pause/suspend.

14 Dec 2016, 03:00

I would do something like this, ofc adding the other menu items if wanted, or leaving the standard ones if desired. Doesn't handle the case where you pause and suspend at the same time, easy to add though.

Code: Select all

#SingleInstance, force
Hotkey, ^o, s							; Toggle suspension
Hotkey, ^p, p, T2						; Toggle pause
Menu, Tray, NoStandard
Menu, Tray, Icon,Shell32.dll,138, 1
Menu, Tray, Add, Pause, p
Menu, Tray, Add, Suspend, s
Menu, Tray, Add, Exit, exit

return

s:
	Suspend, Permit
	Menu, Tray, Icon,Shell32.dll, % A_IsSuspended?138:132, 1
	Suspend, % A_IsSuspended?"Off":"On"
return
	
p:
	Suspend, Permit
	Menu, Tray, Icon,Shell32.dll, % A_IsPaused?138:28, 1
	Pause, % A_IsPaused?"Off":"On"
return

exit:
	ExitApp
return
Good luck
lexikos
Posts: 8318
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Setting tray icon; detecting pause/suspend.

14 Dec 2016, 03:29

Nextron wrote:Is there some other method to be notified of the script's change in suspend/pause state?
There isn't any method to be notified of the script's change in suspend/pause state, let alone some other one.

As you've noted, the 0x111 message is received before the change takes place; therefore it clearly isn't notifying you of the change.

The best solution is to "replace all code calling Suspend with code that does trigger detection".

When you activate a menu item, the menu sends the 0x111 message to the menu's window. The default processing for the message (i.e. the action assigned to the menu item) will not occur until you return from the message-monitoring function, and will not occur at all if you return a value.
Exaskryz wrote:The Pause command triggers the Jello function
No, it doesn't.
User avatar
Nextron
Posts: 1385
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Setting tray icon; detecting pause/suspend.

14 Dec 2016, 11:11

lexikos wrote:The best solution is to "replace all code calling Suspend with code that does trigger detection".
Right, too bad. Thanks for the confirmation. :thumbup:
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Setting tray icon; detecting pause/suspend.

14 Dec 2016, 15:41

lexikos wrote:
Exaskryz wrote:The Pause command triggers the Jello function
No, it doesn't.

Something different happens that the Suspend command does not trigger the Jello function. Could you elaborate on what does happen?

My observations:
Using a single line hotkey for Suspend does not trigger Jello.
Using a single line hotkey for Pause does trigger Jello.
Using the tray's context menu for Suspend does trigger Jello.
Using the tray's context menu for Pause does trigger Jello.

Do those observations match your expectations or your own observations?
lexikos
Posts: 8318
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Setting tray icon; detecting pause/suspend.

16 Dec 2016, 03:24

Your observation is incorrect. Pause does not trigger Jello.

Neither Pause nor Suspend cause message 0x111 to be sent.

Perhaps you were being confused as a result of pausing threads. If a hotkey's thread is paused, pressing the hotkey will not launch a new thread unless #MaxThreadsPerHotkey allows it. OnMessage has the same default limit, set by the MaxThreads parameter.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Setting tray icon; detecting pause/suspend.

16 Dec 2016, 12:03

("Start" of the post is after the ------")

My new observations/conclusion:


I'm dumb. I had an external script that totally interfered with all the testing I had done. I have a script that I use Ctrl+r to reload a script if my currently active window is Notepad++ and that is the currently active file. I forgot that I also added the Suspend, Pause, etc. commmands. It was this external script that was posting messages to the script I was testing all of this on. So that's how I got different behaviors. I use ^p and ^o in the code below to change the icons, while I use ^p and ^u (u for suspend instead of ^s for save) in my other script that I had running concurrently. That helps clear up a bit of the mess.

But that does tell us that if we used an external script to control the pausing and suspension of this script, we could manipulate the icons through OnMessage...

-------

Let me resupply my code (with some changes just for testing, so it's a spot ugly):

Code: Select all

;https://autohotkey.com/boards/viewtopic.php?f=5&t=25370&p=119836#p119836
Menu, Tray, Icon,TestScript2.ico,, 1
OnMessage("0x111","Jello")
return
^o::
Suspend
GoSub CheckSuspendedState ; the function Jello will NOT execute
return
^p::Pause ; the function Jello will execute, even with the script pausing
return

Jello(wParam){
Tooltip triggered
If (wParam=65305)
{
Tooltip hey
GoSub CheckSuspendedStateInverse
}
If (wParam=65306)
	Menu, Tray, Icon, % (A_IsPaused)?"TestScript2.ico":"TestScript.ico" ; reflects PAST state. 2 is my NORMAL icon, no-num is my PAUSE icon
	; A_IsPaused reflects the PAST state. I guess the variable doesn't update once the function has been launched, even adding a Sleep to give it time to process the native Pause command doesn't work. 
}
CheckSuspendedState:
If (A_IsSuspended)
Menu, Tray, Icon, TestScript3.ico ; this is an "S" icon
else
Menu, Tray, Icon, TestScript2.ico ; this is a custom green icon
return
CheckSuspendedStateInverse:
If !(A_IsSuspended)
Menu, Tray, Icon, TestScript3.ico ; this is an "S" icon
else
Menu, Tray, Icon, TestScript2.ico ; this is a custom green icon
return
^l::listlines
The trouble where I don't think I'm having confusion is I can reload my script after each test to clear up any confusion. If the first thing I do is hit ^p::Pause, the "triggered" tooltip appears. And that is only inside the Jello function, so it appears to be triggering it. Is there another explanation for the function firing when it shouldn't be, or if something else happens as a result of Pause being used, that that other thing in turn sends a 0x111 message? It's still sending a wParam value of 65306 as my icon is changing.

Far as I know, my observations are still consistent on here. This is with other scripts running, if that makes a difference.

And it does. It totally does.

Ha, ha ha, haaaaaaa.... See top of post.
User avatar
Nextron
Posts: 1385
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Setting tray icon; detecting pause/suspend.

16 Dec 2016, 18:03

Glad you hear you figured out the discrepancy.
Exaskryz wrote:that does tell us that if we used an external script to control the pausing and suspension of this script, we could manipulate the icons through OnMessage...
'External' as in process or as in function/label? Using the code in the top post I've got everything working like I want. The only sacrifice I had to make was to replace all Suspend and Pause commands and to remember to do that for new code as well:
Suspend %param% -> Suspend % Suspend(param)
Pause %param% -> Pause(param))

The custom icon set is really growing on me.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Setting tray icon; detecting pause/suspend.

26 Feb 2017, 11:09

This script tries to recreate/modify the tray menu icon/items. It expects to find 'AutoHotkey Basic.exe' in 'C:\Program Files\AutoHotkey'. It seems to work fine, there could be some slight issues however, since changing the menu can be quite fiddly.

Please make any comments including regarding:
;vIsPaused := A_IsPaused ;with this line it didn't

Code: Select all

;==================================================

;ID_TRAY_OPEN := 65300
;ID_FILE_RELOADSCRIPT := 65400 ;ID_TRAY_RELOADSCRIPT := 65303
;ID_FILE_EDITSCRIPT := 65401 ;ID_TRAY_EDITSCRIPT := 65304
;ID_FILE_WINDOWSPY := 65402 ;ID_TRAY_WINDOWSPY := 65302
;ID_FILE_PAUSE := 65403 ;ID_TRAY_PAUSE := 65306
;ID_FILE_SUSPEND := 65404 ;ID_TRAY_SUSPEND := 65305
;ID_FILE_EXIT := 65405 ;ID_TRAY_EXIT := 65307
;ID_VIEW_LINES := 65406
;ID_VIEW_VARIABLES := 65407
;ID_VIEW_HOTKEYS := 65408
;ID_VIEW_KEYHISTORY := 65409
;ID_VIEW_REFRESH := 65410
;ID_HELP_USERMANUAL := 65411 ;ID_TRAY_HELP := 65301
;ID_HELP_WEBSITE := 65412

;1:&Open-ID=65300
;2:&Help-ID=65301
;3:{Empty String}-ID=0
;4:&Window Spy-ID=65302
;5:&Reload This Script-ID=65303
;6:&Edit This Script-ID=65304
;7:{Empty String}-ID=0
;8:&Suspend Hotkeys-ID=65305
;9:&Pause Script-ID=65306
;10:E&xit-ID=65307

;==================================================

#Persistent
DetectHiddenWindows, On
;vPath := "C:\Windows\System32\shell32.dll"
vPath := "C:\Program Files\AutoHotkey\AutoHotkey Basic.exe"
hModule := DllCall("LoadLibrary", Str,vPath)
hIcon00 := DllCall("LoadIcon", Ptr,hModule, Ptr,159, Ptr) ;[H+green]
hIcon01 := DllCall("LoadIcon", Ptr,hModule, Ptr,206, Ptr) ;S on [S+green]
hIcon10 := DllCall("LoadIcon", Ptr,hModule, Ptr,207, Ptr) ;P on [H+red]
hIcon11 := DllCall("LoadIcon", Ptr,hModule, Ptr,208, Ptr) ;PS on [S+red]
Menu, Tray, Icon, HICON:%hIcon00%
Menu, Tray, Icon, , , 1 ;freeze icon
OnMessage(0x111, "WM_COMMAND")

;==================================================

Menu, Tray, NoStandard ;remove standard menu items

Menu, Tray, Add, &Open, Open
Menu, Tray, Add, Open Containing Folder, OpenContainingFolder
Menu, Tray, Add, &Help, Help
Menu, Tray, Add ;------------------------------
Menu, Tray, Add, &Window Spy, WindowSpy
Menu, Tray, Add, &Reload This Script, Reload
Menu, Tray, Add, &Edit This Script, Edit
Menu, Tray, Add ;------------------------------
Menu, Tray, Add, &Suspend Hotkeys, Suspend
Menu, Tray, Add, &Pause Script, Pause
Menu, Tray, Add, E&xit, Exit

Menu, Tray, Default, &Open
Return

;==================================================

Open:
Help:
WindowSpy:
Reload:
Edit:
Suspend:
Pause:
Exit:

vIsPaused := A_IsPaused
vIsSuspended := A_IsSuspended

(A_ThisLabel = "Open") ? (vID := 65300) : ""
(A_ThisLabel = "Help") ? (vID := 65301) : ""
(A_ThisLabel = "WindowSpy") ? (vID := 65302) : ""
(A_ThisLabel = "Reload") ? (vID := 65303) : ""
(A_ThisLabel = "Edit") ? (vID := 65304) : ""
(A_ThisLabel = "Suspend") ? (vID := 65305, vIsSuspended := !vIsSuspended) : ""
(A_ThisLabel = "Pause") ? (vID := 65306, vIsPaused := !vIsPaused) : ""
(A_ThisLabel = "Exit") ? (vID := 65307) : ""

Menu, Tray, % vIsPaused ? "Check" : "Uncheck", &Pause Script
Menu, Tray, % vIsSuspended ? "Check" : "Uncheck", &Suspend Hotkeys

PostMessage, 0x111, %vID%, , , ahk_id %A_ScriptHwnd% ;WM_COMMAND
Return

;==================================================

OpenContainingFolder:
Run, %ComSpec% /c explorer.exe /select`, "%A_ScriptFullPath%", , Hide
Return

;==================================================

WM_COMMAND(wParam, lParam)
{
global hIcon00, hIcon01, hIcon10, hIcon11
static vIsPaused, vIsSuspended
Critical
vID := wParam & 0xFFFF

if vID in 65305,65404,65306,65403
{
	if (vIsPaused = "") ;with these lines it worked
		vIsPaused := 0 ;with these lines it worked
	;vIsPaused := A_IsPaused ;with this line it didn't
	vIsSuspended := A_IsSuspended
	if vID in 65306,65403 ;pause
		vIsPaused := !vIsPaused
	else
		vIsSuspended := !vIsSuspended
	hIcon := hIcon%vIsPaused%%vIsSuspended%
	Menu, Tray, Icon, HICON:%hIcon%
}
}

;==================================================

;functions by Nextron:
;Setting tray icon; detecting pause/suspend. - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=25370
;replace Suspend/Pause commands in scripts as follows
;in order to use custom icons:
;e.g. Suspend %param% -> Suspend % Suspend(param)
;e.g. Pause %param% -> Pause(param))
Suspend(Mode:="Toggle"){
	If (Mode="Toggle" || Mode="Off" && A_IsSuspended || Mode="On" && !A_IsSuspended)
		DllCall("PostMessage", UInt, A_ScriptHwnd, UInt,0x111, UInt,65404, UInt, 0)
	Return "Permit"
}
Pause(Mode:="Toggle", OperateOnUnderlyingThread:=0){
	If (Mode="Toggle" || Mode="Off" && A_IsPaused || Mode="On" && !A_IsPaused)
		DllCall("PostMessage", UInt, A_ScriptHwnd, UInt,0x111, UInt,65403, UInt, 0)
	If OperateOnUnderlyingThread
		MsgBox Not sure how to handle OperateOnUnderlyingThread.
}

;==================================================
Links:
Modify AHK default tray menu - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/4930 ... tray-menu/
How To Change The Tray Icon - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/2451 ... tray-icon/
[this page]
Setting tray icon; detecting pause/suspend. - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25370

[to view icons in files and get icon numbers]
IconsExtract - Extract icon/cursor stored in EXE, DLL, OCX, CPL files
http://www.nirsoft.net/utils/iconsext.html
[download AutoHotkey Basic]
AutoHotkey Downloads
https://autohotkey.com/download/

Get Info from Context Menu - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/1975 ... text-menu/
list of AutoHotkey WM_COMMAND IDs (e.g. Reload/Edit/Suspend/ListVars on another script) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=27824
Pause
https://autohotkey.com/docs/commands/Pause.htm
Suspend
https://autohotkey.com/docs/commands/Suspend.htm

[EDIT:]
To make the tray/main window Edit menu items open with a different program:

Code: Select all

;add this to the WM_COMMAND OnMessage function
if vID in 65304,65401
{
	Run, "wordpad.exe" "%A_ScriptFullPath%"
	Return 0
}

;replace instances of 'Edit' with 'Edit()' in the script
Edit()
{
	Run, "wordpad.exe" "%A_ScriptFullPath%"
	Return
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
pervect66
Posts: 20
Joined: 09 Jun 2021, 06:53

Re: Setting tray icon; detecting pause/suspend.

03 Feb 2022, 04:24

Simplest way:

Code: Select all

;@Ahk2Exe-AddResource stop.ico, 206  ; Replaces 'S on green'
This replaces the "S on green" default suspend icon in the compiled executable by stop.ico

No further coding required, stop.ico will be in the compiled exe as a resource, and will be automatically used when suspended.

The other icons can be replaced as well:

Code: Select all

;@Ahk2Exe-AddResource stop.ico, 160  ; Replaces 'H on blue'
;@Ahk2Exe-AddResource stop.ico, 207  ; Replaces 'H on red'
;@Ahk2Exe-AddResource stop.ico, 208  ; Replaces 'S on red'

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Smile_, tatagi, teadrinker and 57 guests