Sign in to follow this  
Followers 0
tuape

SysTray_UDF

107 posts in this topic

#1 ·  Posted (edited)

Inspired by this thread: Hide icon of a running program, Hide icon

An udf to get process behind any systray icon for easier closing and to give ability to remove any icon(s) from systray.

Functions:

;===============================================================================
;
; Function Name:    _SysTrayIconTitles()
; Description:    Get list of all window titles that have systray icon
; Parameter(s):  None
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns an array with all window titles
;                  On Failure - TO BE DONE
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconProcesses()
; Description:    Get list of all process' names that have systray icon
; Parameter(s):  None
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns an array with all process names
;                  On Failure - TO BE DONE
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconPids()
; Description:    Get list of all processes id's that have systray icon
; Parameter(s):  None
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns an array with all process id's
;                  On Failure - TO BE DONE
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconIndex($name, $mode=0)
; Description:    Get list of all processes id's that have systray icon
; Parameter(s):  $name = process name / window title text
;                   $mode 0 = get index by process name (default)
;                         1 = get index by window title
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns index of found icon
;                  On Failure - Returns -1 if icon for given process/wintitle 
;                                was not found.
;                              - Sets error to 1 and returns -1 in case of bad
;                                arguments
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconCount
; Description:    Utility function. Returns number of icons on systray
;                   Note: Hidden icons are also reported
; Parameter(s):  None
;                   
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns number of icons found
;                  On Failure - TO BE DONE
;                                
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconRemove($index=0
; Description:    Removes systray icon completely.
;                   
; Parameter(s):  index = icon index. Can be queried with _SysTrayIconIndex()
;                   Default = 0
;                   
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns 1 if icon successfully removed
;                  On Failure - Sets error to 1 and returns -1
;                                
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconVisible($flag, $index)
; Description:    Hides / unhides any icon on systray
;                   
; Parameter(s):  $flag = hide (1) or show (0) icon
;                   $index = icon index. Can be queried with _SysTrayIconIndex()
;                   
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns 1 if operation was successfull / 0 if 
;                   icon was already hidden/unhidden
;                  On Failure - If invalid parameters, returns -1 and sets error
;                   to 1
;                                
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconmove($curPos, $newPos)
; Description:    Moves systray icon
;                   
; Parameter(s):  $curPos = icon's current index (0 based)
;                   $newPos = icon's new position 
;                   ----> ($curPos+1 = one step to right, $curPos-1 = one step to left)
;                   
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns 1 if operation was successful
;                  On Failure - If invalid parameters, returns -1 and sets error
;                   to 1
;                                
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
;
; Function Name:    _SysTrayIconPos($iIndex=0)
; Description:    Gets x & y position of systray icon
; Parameter(s):  $iIndex = icon index (Note: starting from 0)
;                   
; Requirement(s):   AutoIt3 Beta
; Return Value(s):  On Success - Returns x [0] and y [1] position of icon
;                  On Failure - Returns -1 if icon is hidden (Autohide on XP etc.)
;                                Sets error to 1 if some internal error happens
;                                
; Author(s):        Tuape
;
;===============================================================================

;===============================================================================
 ;
 ; Function Name:   _SysTrayIconTooltip($iIndex=0)
 ; Description:   Utility function. Gets the tooltip text of 
 ;                  systray icon of given index
 ; Parameter(s):     $iIndex = icon index (Note: starting from 0)
 ;                  
 ; Requirement(s):   AutoIt3 Beta
 ; Return Value(s):  On Success - Returns tooltip text of icon
;                  On Failure - Returns -1 in error situations
;                                
 ; Author(s):       Tuape
 ;
 ;===============================================================================

As you see better error handling needs to be added.

I hope someone with more experience with Win32API, DllCalls & structures can take a look at this; just to make sure it's not leaking memory etc.

All comments welcome. Please also suggest functions you would like to add to this. I am already planning to add _SysTrayIconHide / unhide.

Some examples:

#include "SysTray_UDF.au3"
    
; -- Example 1 --
; Get window titles of all windows that have icon on systray
    $iTitles = _SysTrayIconTitles()
; Get process names of all processes that have icon on systray
    $iProcesses = _SysTrayIconProcesses()
    For $i=0 to Ubound($iTitles)-1
; write the info to consolewindow
        ConsoleWrite(@CR & "#" &$i & "Title: " & $iTitles[$i] & ", process: " & $iProcesses[$i])
    Next
    
    
; -- Example 2 --
    $st_process = "SETI@home.exe"; change this if needed
    _SysTrayIconRemove(_SysTrayIconIndex($st_process))
; Note that only the icon was removed; process still should be running
    
; -- Example 3 --
    $st_process = "winampa.exe"; change this if needed
    _SysTrayIconVisible(1, _SysTrayIconIndex($st_process))
; Note that the icon is hidden
    Sleep(10000)
    _SysTrayIconVisible(0, _SysTrayIconIndex($st_process))
    
; -- Example 4 --
    $pos = _SysTrayIconIndex($st_process)
    $ret = _SysTrayIconmove($pos, $pos+3); edit newpos if needed
    
; -- Example 5 --
; Left-click Outlook's icon on system tray
    
; Press hide inactive icon's button part is from Valik's refresh system tray script!
    $oldMatchMode = Opt("WinTitleMatchMode", 4)
    $oldChildMode = Opt("WinSearchChildren", 1)
    
    $class = "classname=Shell_TrayWnd"
    $hControl = ControlGetHandle($class, "", "Button2")
    
; get tray position and move there. Helps if Auto Hide tray option is used.
    $posTray = WinGetPos(_FindTrayToolbarWindow()) 
    MouseMove($posTray[0], $posTray[1])
    
; If XP and the Hide Inactive Icons mode is active
    If $hControl <> "" And ControlCommand($class, "", $hControl, "IsVisible","") Then
        ControlClick($class, "", $hControl)
        Sleep(250); Small delay to allow the icons to be drawn
    EndIf
    
    $index = _SysTrayIconIndex("Outlook.exe"); Change this to some other application if needed
    If $index <> -1 Then
        $pos = _SysTrayIconPos($index)
        
        If $pos = -1 Then Exit
        MouseMove($pos[0], $pos[1])
        Sleep(1000)
        MouseClick("left")
    EndIf
    ConsoleWrite(@CRLF & @CRLF & "Pos[0]: " & $pos[0] & "$pos[1]: " & $pos[1])
; Restore Opt settings
    Opt("WinTitleMatchMode", $oldMatchMode)
    Opt("WinSearchChildren", $oldChildMode)
      
 ; -- Example 6 - get all tooltip texts --
    For $p = 0 to _SystrayIconCount()
          MsgBox(0, "Tooltip", _SysTrayIconTooltip($p))
      Next
      
 ; -- Example 7 - get tooltip text when process name is known --
      MsgBox(0, "Tooltip", _SysTrayIconTooltip(_SysTrayIconIndex("Feedreader.exe")))

Note: This UDF has been tested with winXP only. Might work with win2000 but doesn't work in win98 AFAIK

Edit: Added _SysTrayIconVisible and an example how to use it.

Edit: Added _SysTrayIconmove and an example how to use it.

Edit: Added _SysTrayIconPos and an example how to use it for clicking specific icon.

Edit: New hWnd handling functions broke this (hWnd(), IsHwnd(). Updated the attachment, now needs 3.1.1.66

Edit: Added function to get tooltip texts + added an example how to use this

Edit: Changed all int_ptr to int* format

Edit: Corrected all memory leaks caused by incorrect parameter in VirtualFreeEx

Systray_test.au3

SysTray_UDF.au3

Edited by tuape

Share this post


Link to post
Share on other sites



what if i want one back :)

<{POST_SNAPBACK}>

You have to stop and restart the process owning that systray icon. There is a message TB_HIDEBUTTON

TB_HIDEBUTTON Message

Hides or shows the specified button in a toolbar.

But I haven't really looked at it yet. I think that can be used for _SysTrayIconHide

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

In the functions where you are using DllStructCreate() when you have errors and return -1, before the return call DllStructDelete()'s on the structs. Other then that and I personally check for errors after each DllStructCreate() but alot of people don't.

Example:

$procHandle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', $PROCESS_ALL_ACCESS, 'int', False, 'int', $pId)
If @error Then
    ConsoleWrite("Error: Could not read toolbar process memory, " & @error)
    DllStructDelete($TBBUTTON);add
    DllStructDelete($TBBUTTON2);add
    DllStructDelete($ExtraData);add
    return -1
EndIf
Edited by Ejoc

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

Should...

Const $PROCESS_ALL_ACCESS = 2035711

be...

Const $PROCESS_ALL_ACCESS = 16533; 0xFFF

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Should...

Const $PROCESS_ALL_ACCESS = 2035711

be...

Const $PROCESS_ALL_ACCESS = 16533; 0xFFF

<{POST_SNAPBACK}>

PROCESS_ALL_ACCESS ISNT 0XFFF. its 0x1f0fff (2035711).

just google "const PROCESS_ALL_ACCESS"

Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

All references I can find for PROCESS_ALL_ACCESS = 0x1F0FFF are dated prior to 2005, I have found one or two that say it is = to 0xFFF dated this year.

I got my original value (0xFFF) from APIViewer 2004 (Released 15/2/2004).

Edited by Burrup

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

In the functions where you are using DllStructCreate() when you have errors and return -1, before the return call DllStructDelete()'s on the structs.  Other then that and I personally check for errors after each DllStructCreate() but alot of people don't.

Example:

$procHandle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', $PROCESS_ALL_ACCESS, 'int', False, 'int', $pId)
If @error Then
    ConsoleWrite("Error: Could not read toolbar process memory, " & @error)
    DllStructDelete($TBBUTTON);add
    DllStructDelete($TBBUTTON2);add
    DllStructDelete($ExtraData);add
    return -1
EndIf

<{POST_SNAPBACK}>

Of course, otherwise DllStructDelete's would not be called at all. Thanks! I will edit the attachment in first post.

Share this post


Link to post
Share on other sites

Added _SysTrayIconmove which let's you change icon's position. See the first post.

Share this post


Link to post
Share on other sites

@Larry

Thanks for clarification, where did you get this value btw?

@tuape

Nice, I haven't had time to look at it yet but I will be sure to when I get home.


[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

*Bump*

Added function to get icon's position and example how to use it for clicking specific icon.

Share this post


Link to post
Share on other sites

*Bump*

Added function to get icon's position and example how to use it for clicking specific icon.

<{POST_SNAPBACK}>

Could somebody please try this out? I only have WinXP and I would like to know if it works in Win2000.

Question "how to click icon in system tray" has been asked frequently, but when there's a solution nobody is interested :whistle:

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Could somebody please try this out? I only have WinXP and I would like to know if it works in Win2000.

Question "how to click icon in system tray" has been asked frequently, but when there's a solution nobody is interested  :whistle:

<{POST_SNAPBACK}>

I am trying to incorporate this into a script but get the followin error

C:\Program Files\AutoIt3\Include\SysTray_UDF.au3 (225) : ==> Unknown function name.:

$TBBUTTON  = DllStructCreate($str)

$TBBUTTON  = ^ ERROR

>AutoIT3.exe ended.

>Exit code: 0    Time: 0.849

I am using the release version of AutoIT could this be the problem.

BTW

I am using XP as well

Edited by BigDod


Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

I am trying to incorporate this into a script but get the followin error

I am using the release version of AutoIT could this be the problem.

BTW

I am using XP as well

<{POST_SNAPBACK}>

You need the beta version of AutoIt. Download it from here

Share this post


Link to post
Share on other sites

You need the beta version of AutoIt. Download it from here

<{POST_SNAPBACK}>

Downloaded latest Beta and it worked a treat for my application.

Many Thanks :whistle::dance::dance:



Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

Downloaded latest Beta and it worked a treat for my application.

Many Thanks  :whistle::dance:  :dance:

<{POST_SNAPBACK}>

That's good to hear. Thanks for trying it out.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

@tuape, I have need of the tooltip of a tray icon instead of just the window title owning it. This single window has multiple icons, and I need the text in the tooltip of the icon. Do you have any ideas?

EDIT: I got this far, but I am unsure whether the function needs the hwnd of the tray or the hwnd of the icon.

$index = 0
$bufferptr = DllStructCreate ("ptr")
$lResult = DLLCall("user32.dll","int","SendMessage", "hwnd", _FindTrayToolbarWindow(), "int", $TB_GETBUTTONTEXT, "int", $index, "ptr", DllStructGetPtr($bufferptr))
$txt = DllStructGetData(DllStructCreate("char[" & $lResult[0] & "]", DllStructGetData($bufferptr, 1)), 1)
MsgBox(0,"",$txt)
Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites

@tuape, I have need of the tooltip of a tray icon instead of just the window title owning it. This single window has multiple icons, and I need the text in the tooltip of the icon. Do you have any ideas?

EDIT: I got this far, but I am unsure whether the function needs the hwnd of the tray or the hwnd of the icon.

$index = 0

$bufferptr = DllStructCreate ("ptr")

$lResult = DLLCall("user32.dll","int","SendMessage", "hwnd", _FindTrayToolbarWindow(), "int", $TB_GETBUTTONTEXT, "int", $index, "ptr", DllStructGetPtr($bufferptr))

$txt = DllStructGetData(DllStructCreate("char[" & $lResult[0] & "]", DllStructGetData($bufferptr, 1)), 1)

MsgBox(0,"",$txt)

the function in red should be a variable.......


[u]Do more with pre-existing apps![/u]ANYGUIv2.8

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0