For example the following would not actually change the icon:
Code: Select all
Menu,Tray,Icon,OriginalIcon.ico
...
resolution change
...
Menu,Tray,Icon,DifferentIcon.ico
Code: Select all
Menu,Tray,Icon,OriginalIcon.ico
...
resolution change
...
Menu,Tray,Icon,DifferentIcon.ico
This is exactly what AutoHotkey does. However, it seems the TaskbarCreated message is also sent by the system when the screen DPI changes, and in that case the icons have not been removed, so when AutoHotkey attempts to add the icon, it fails. Subsequent updates to the tray icon are not attempted, because it was assumed that if adding the icon failed, the icon is not present.When the taskbar is created, it registers a message with the TaskbarCreated string and then broadcasts this message to all top-level windows. When your taskbar application receives this message, it should assume that any taskbar icons it added have been removed and add them again.
Code: Select all
KillTrayIcon(A_ScriptHwnd) ; Remove the icon from the tray.
Menu Tray, NoIcon ; Ensure AutoHotkey thinks there's no icon.
Menu Tray, Icon ; Recreate the icon.
KillTrayIcon(scriptHwnd) {
static NIM_DELETE := 2, AHK_NOTIFYICON := 1028
VarSetCapacity(nic, size := 936+4*A_PtrSize)
NumPut(size, nic, 0, "uint")
NumPut(scriptHwnd, nic, A_PtrSize)
NumPut(AHK_NOTIFYICON, nic, A_PtrSize*2, "uint")
return DllCall("Shell32\Shell_NotifyIcon", "uint", NIM_DELETE, "ptr", &nic)
}
Code: Select all
OnMessage(DllCall("RegisterWindowMessage", "str", "TaskbarCreated"), "TaskbarCreated")
TaskbarCreated() {
if (A_IconFile = "") {
; Explicitly set an icon.
Menu Tray, Icon, % A_AhkPath
; Restore A_IconFile to its previous empty value (in case it affects script behaviour).
Menu Tray, Icon, *
}
else {
; Reapply icon.
Menu Tray, Icon, % A_IconFile, % A_IconNumber
}
return 0 ; Prevent default handling.
}
Code: Select all
OnMessage(DllCall("RegisterWindowMessage", "str", "TaskbarCreated"), "TaskbarCreated")
TaskbarCreated() {
if !HasTrayIcon()
return ; Allow default handling.
if (A_IconFile = "") {
; Explicitly set an icon.
Menu Tray, Icon, % A_AhkPath
; Restore A_IconFile to its previous empty value (in case it affects script behaviour).
Menu Tray, Icon, *
}
else {
; Reapply icon.
Menu Tray, Icon, % A_IconFile, % A_IconNumber
}
return 0 ; Prevent default handling.
}
HasTrayIcon(scriptHwnd := 0) {
static NIM_MODIFY := 1, AHK_NOTIFYICON := 1028
VarSetCapacity(nic, size := 936+4*A_PtrSize)
NumPut(size, nic, 0, "uint")
NumPut(scriptHwnd ? scriptHwnd : A_ScriptHwnd, nic, A_PtrSize)
NumPut(AHK_NOTIFYICON, nic, A_PtrSize*2, "uint")
return DllCall("Shell32\Shell_NotifyIcon", "uint", NIM_MODIFY, "ptr", &nic)
}