[LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 20 Feb 2019, 09:41

First, I'd like to thank Sean, Cyruz, and FG for this superb library! It has been essential in the development of my latest program. I'm using the version with a Version Date of 2018-03-13 (my first attempt at this post included the library's URL, but that caused the post to be blocked by our new anti-spam methods).

My current effort is to make sure that my program is usable by the visually impaired. For this, they need to be able to right-click on an icon in the tray (without being able to see it, of course). I have set up several hotkeys (because there may be several instances of my program running at once, each with a different color icon in the tray) and a call to TrayIcon_Button with the "R" param is working perfectly. The only small problem, which isn't an issue for the visually impaired, but could be for the sighted who also use the feature, is that the context menu appears wherever the mouse cursor is when TrayIcon_Button is called. Is there any way to determine the X-Y coordinates of a tray icon so that I can do a MouseMove there before calling TrayIcon_Button? If not, is there a way to determine the X-Y coordinates of the upper left corner of the notification area (not of the whole taskbar)? Thanks very much, Joe

Edit: Btw, I had been wondering if "L" and "R" actually mean Left Click and Right Click, or if they really mean Primary Click and Secondary Click. In other words, if the left and right mouse buttons are swapped (often done by left-handed users), will the "R" param send a Right Click, which in this case would be a Primary Click, or a Left Click, which in this case would be a Secondary Click? I ran a test and found that the answer is the latter, i.e., the "R" param sends a Secondary Click, which is actually a Left Click when the left and right mouse buttons are swapped. In my opinion, this is great...exactly the behavior I want! My only suggestion is to document that in the comments at the function. I hope my experiment helps other users of this library.

User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 20 Feb 2019, 16:06

I just tried to edit this question with another "by the way" comment, but it got blocked by our new anti-spam methods. So, I'm trying to post it again via a new post instead of an edit of the first post (I think we're taking extra measures against edits because that's how our current spammer operates...via edits of his own initial posts).

My new BTW comment is to note that I tried ImageSearch to see if it could find the icon (which is in a 48x48 .ICO file that I created), but it always comes up with ErrorLevel=1 (not found). I set the X1,Y1,X2,Y2 params as 0,0,A_ScreenWidth,A_ScreenHeight (i.e., the whole screen) and tried numerous settings for *n *wn *hn — nothing worked. Regards, Joe

User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 03 Apr 2019, 09:08

Greetings to Sean, Cyruz, FG and anyone else who can help with this. It is important for the program that I mentioned in previous posts to have the tray icons in Shell_TrayWnd rather than NotifyIconOverflowWindow. I could put a section in the User Guide documenting the steps in Windows to show my program's icon (or show all icons), but it would be much better to have my program automatically put them in Shell_TrayWnd when "Always shows all icons..." is Off. Is there a way to do that? Thanks, Joe

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by jeeswg » 03 Apr 2019, 14:32

- @JoeWinograd: There's some code here for Windows 7 and possibly Windows 8. There were problems in Windows 10.
Is there a way to put an icon outside the hidden tray icons by default? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=58992
- Perhaps Microsoft doesn't want programs to be able to do this. And similarly they don't want programs to pin themselves to the taskbar. I.e. to prevent spammy software that places itself everywhere.
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
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 03 Apr 2019, 16:15

Hi jeeswg,
Thanks for the link...I'll give it a look, but I'd like my program to work in everything from XP to W10, and it must work in at least W7, W8.1, and W10. Cheers, Joe

User avatar
cyruz
Posts: 346
Joined: 30 Sep 2013, 13:31

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by cyruz » 03 Apr 2019, 22:02

Quick update and code merged in the OP.

@jeeswg that's interesting, if you or JoeWinograd come up with a solution for Windows 10 to "unhide" the icons it would be a nice addition.
ABCza on the old forum.
My GitHub.

julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by julesverne » 16 Jul 2019, 13:13

Was wondering if anyone was able to get this working on Win 10? Version 1709 or higher?

User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 16 Jul 2019, 14:32

Was wondering if anyone was able to get this working on Win 10? Version 1709 or higher?
Depends on what you mean by "this". If you mean the library, the answer is YES...it works perfectly on 1803, 1809, and 1903. If you mean being able to programmatically put the icons in Shell_TrayWnd when "Always shows all icons..." is Off, the answer is NO...haven't figured out how to do that...or whether it's even possible. Regards, Joe

julesverne
Posts: 42
Joined: 18 Apr 2017, 14:39
Contact:

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by julesverne » 16 Jul 2019, 22:52

Working great now! Thanks @JoeWinograd

michael_jie
Posts: 11
Joined: 26 Nov 2018, 04:44

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by michael_jie » 08 Oct 2019, 02:52

Thanks for lib. It's awesome.

But when i used TrayIcon_Button, the network connection and sound didn't work, the other worked perfect.
Run as admin didn't work too.

Here is my code:
I used RiseUp's version, and add event in listview. Used hWnd as id.
And changed TrayIcon_GetInfo() If ( !sExeName || sExeName == sProcess || sExeName == nPid || sExeName == hwnd) to let it can gets info by use hwnd

Code: Select all

#NoEnv				
SendMode Input	

Gui Add, ListView, Grid r30 w700 Sort gMyListView, Process|Tooltip|Visible|Handle


oIcons := TrayIcon_GetInfo()
Loop, % oIcons.MaxIndex()
{
    proc := oIcons[A_Index].Process
    ttip := oIcons[A_Index].tooltip
	tray := oIcons[A_Index].Tray
    hWnd := oIcons[A_Index].hWnd
    
	vis := (tray == "Shell_TrayWnd") ? "Yes" : "No"
	
    LV_Add(, proc, ttip, vis, hWnd)
}

LV_ModifyCol()					 
LV_ModifyCol(3, "AutoHdr")          

Gui Show, Center, System Tray Icons
Return

MyListView:
	if A_GuiEvent = DoubleClick
	{
	    LV_GetText(hWnd, A_EventInfo, 4)  
	    TrayIcon_Button(hWnd,"L")
	}
return


GuiEscape:
GuiClose:
    ExitApp
return

User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 22 May 2020, 14:57

Hi cyruz (or anyone else who would like to jump in),

Circling back to this after a year or so, I'm wondering if the recent W10 versions (1909 and 2004) have created a way to do this. What I'm hoping for is an enhanced TrayIcon_Move function that supports the sTray parameter for both the Old and the New index, something like this:

TrayIcon_Move(idxOld, idxNew, sTrayOld := "NotifyIconOverflowWindow", sTrayNew := "Shell_TrayWnd")

My hope is that this would result in the move of an icon from NotifyIconOverflowWindow to Shell_TrayWnd, or vice versa (at the specified index for each tray). Possible?

Thanks much, Joe

jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by jadams » 22 May 2020, 15:14

Thanks Joe for posting that... I need this as well!

jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by jadams » 04 Jun 2020, 12:27

JoeWinograd wrote:
22 May 2020, 14:57
Hi cyruz (or anyone else who would like to jump in),

Circling back to this after a year or so, I'm wondering if the recent W10 versions (1909 and 2004) have created a way to do this. What I'm hoping for is an enhanced TrayIcon_Move function that supports the sTray parameter for both the Old and the New index, something like this:

TrayIcon_Move(idxOld, idxNew, sTrayOld := "NotifyIconOverflowWindow", sTrayNew := "Shell_TrayWnd")

My hope is that this would result in the move of an icon from NotifyIconOverflowWindow to Shell_TrayWnd, or vice versa (at the specified index for each tray). Possible?

Thanks much, Joe
Anybody have an update on this?

jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by jadams » 03 Jul 2020, 10:20

Any news on this?

User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 03 Jul 2020, 10:59

jadams wrote:Any news on this?
Btw, there's another thread on this issue:

https://www.autohotkey.com/boards/viewtopic.php?f=76&t=58992

No complete solution there yet, but you should keep an eye on it. Regards, Joe

jiyongred
Posts: 4
Joined: 28 Dec 2015, 02:52
Contact:

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by jiyongred » 21 Sep 2020, 17:56

Thanks for the library. FWIW, TrayIcon_Move is not persisting between reboots for me (Windows 10 2004). It works great initially but after rebooting the icons are back to their pre-library call positions. I've got something clunky working simulating mouse downs and ups manually (and still relying on TrayIcon_GetInfo, so thanks!) but I'll be watching this thread in case TrayIcon_Move is ever updated.

User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by JoeWinograd » 21 Sep 2020, 19:31

jiyongred wrote:TrayIcon_Move is not persisting between reboots
TrayIcon_Move does exactly what its name says...moves the tray icons. The fact that they don't necessarily stay in the same position after a reboot has nothing to do with TrayIcon_Move...that's a Windows issue. If you move the icons manually via drag-and-drop, they also won't necessarily stay in the same positions after a reboot...Windows will put them where it wants.

It's up to you to provide the ability in your programs to place the icons where desired. For example, one of my programs that deals with tray icons has a Tools context menu choice with this submenu:

move tray icons.png
move tray icons.png (3.53 KiB) Viewed 7743 times

Another one of my programs offers those two choices plus a third choice to move the tray icons in any order (not just Beginning and End) based on a .ini configuration file.

Of course, all of those features in all of my programs use TrayIcon_Move to perform the moves. Regards, Joe

Dougal
Posts: 26
Joined: 19 Aug 2014, 16:51

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by Dougal » 09 Dec 2020, 02:10

Great tool that fixed long running issue with one of my scripts that was updated by external process that killed mine but didn't remove the tray icon. Users finished up with multiple icons in the system tray as it was updated each hour, and the icons only disappeared if moused-over or rebooted.

I slightly changed the test at line 86 to allow return only icons with no process or no process and no pid using "-" or "--" for sExeName respectively:

Code: Select all

If ( (sExeName = "-" && !sProcess) || (sExeName == "--" && !nPid) || (!sExeName) || (sExeName = sProcess) || (sExeName == nPid)
I initially tried no process, but the nVidia control panel has icon with pid but no registered process, no added 2nd check for no pid which seems reliable. Also changed the sExeName == sProcess comparison to case insensitive.

I added function TrayIcon_Clean to clean out any icons that have no pid specifically for my issue, maybe others will find it useful:

Code: Select all

; ----------------------------------------------------------------------------------------------------------------------
; Function .....: TrayIcon_Clean
; Description ..: Removes stale icons (no matching processid).
; Parameters ...: bVerbose - True to display output, false (default for no output).
; Notes ........: Uses modified TrayIcon_GetInfo that accepts sExeName - for no process, and -- for no processid
; ----------------------------------------------------------------------------------------------------------------------
TrayIcon_Clean(bVerbose := false)
{
	for i, oIcon in TrayIcon_GetInfo("--") {
		if (not oIcon.pid) {
			TrayIcon_Remove( oIcon.hwnd, oIcon.uid)
			if (bVerbose) {
				str .= "Idx: " oIcon.idx "`nhWnd: " oIcon.hwnd "`nUid: " oIcon.uid "`nIdCmd: " oIcon.idcmd "`n`n"
			}
		}
	}
	if (bVerbose) {
		if (str) {
			str := "Leftover icons removed`n`n" str
		} else {
			str := "No leftover icons to remove"
		}
		MsgBox, % str
	}
}
Thanks for your hard work, and to the others that have also contributed. :bravo:

Caporeira
Posts: 14
Joined: 23 Mar 2021, 08:05

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by Caporeira » 07 May 2021, 00:06

It is still workin for Windows 10 ?
I'm getting this error:

Code: Select all

>"C:\Program Files\AutoHotkey\AutoHotkey.exe" /CP65001 *
What i'm doing wrong ?

User avatar
spaceowl
Posts: 18
Joined: 16 May 2021, 06:46

Re: [LIB] TrayIcon - Sean's TrayIcon for Unicode and 64 bit

Post by spaceowl » 12 Jul 2021, 08:46

Hello, I am using this cool tray.ahk sript to click on icons with TrayIcon_Button, this works great! :dance:

However, I have problems with one programm, 4t-tray-minimizer.
It minimizes every window to the tray, every icon gehts the same exe name "4t-min.exe", so that I can't restore a certain app with TrayIcon_Button. Index doesn't help to.

Is it somehow possible to edit the tray.ahk script, so that it does use the exe name AND also part of the tooltrip text?
I am using this script (found here on the forum I think) to get infos and tooltips from tray icons:

Code: Select all

#NoEnv							; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input					; Recommended for new scripts due to its superior speed and reliability.

; This script assumes that TrayIcon.ahk is in one of your library locations:
;    %A_ScriptDir%\Lib\                                  ; Local library - requires v1.0.90+.
;    %A_MyDocuments%\AutoHotkey\Lib\                     ; User library.
;    path-to-the-currently-running-AutoHotkey.exe\Lib\   ; Standard library.
; If not in any of those, then add an #Include at the top pointing to the file.
#Include TrayIcon.ahk

; Create a ListView to display the list of info gathered
Gui Add, ListView, Grid r30 w700 Sort, Process|Tooltip|Visible|Handle

; Get all of the icons in the system tray using Sean's TrayIcon library
oIcons := TrayIcon_GetInfo()

; Loop through the info we obtained and add it to the ListView
Loop, % oIcons.MaxIndex()
{
    proc := oIcons[A_Index].Process
    ttip := oIcons[A_Index].tooltip
	tray := oIcons[A_Index].Tray
    hWnd := oIcons[A_Index].hWnd
    
	vis := (tray == "Shell_TrayWnd") ? "Yes" : "No"
	
    LV_Add(, proc, ttip, vis, hWnd)
}

LV_ModifyCol()
LV_ModifyCol(3, "AutoHdr")          ; Auto-size the 3rd column, taking into account the header's text

Gui Show, Center, System Tray Icons
Return

GuiEscape:
GuiClose:
    ExitApp

Post Reply

Return to “Scripts and Functions (v1)”