AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

MouseOver TrayIcon, triggering an event

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Tasman



Joined: 20 May 2006
Posts: 21

PostPosted: Mon Sep 04, 2006 2:43 pm    Post subject: MouseOver TrayIcon, triggering an event Reply with quote

This script detects if the mouse is hovering over the given tray icon (in the case below the scripts own icon).
The imagesearch is limited to the tray area and the size of the area sensitive to the mouseover event is set by using the systems icon size.
Read the comments in the script for more specific info.
So far tested on winXP and W2K

Code:
#persistent
Menu, Tray, Icon, pifmgr.dll, 2 ; set the icon for this script
; the icon to look for (in this case the one of this script)
iconstr = 2 pifmgr.dll ; the number of the icon [space] filename containing the icon
; get the icon sizes (system small) in order to determine the size of the area for the mouseover event
sysget, sm_cxsmicon, 49
sysget, sm_cysmicon, 50
; MsgBox, %sm_cxsmicon% %sm_cysmicon%
SetTimer, mouseovericon, 500 ; check every half second if the mouse is over the icon

mouseovericon:
   mouseovericon := iconfind(iconstr,sm_cxsmicon,sm_cysmicon)
   if mouseovericon = 0
      MsgBox Now! ; display now! when the mouse is over the icon
return

iconfind(iconstr,sm_cxsmicon,sm_cysmicon)
; checks to see if the mouse is over the given tray icon
; returns:
; 0 if mouse is over the icon,
; 1 if image not found
; 2 if imagesearch produced an error or
; 3 if icon was found but mouse was not over the image
{
   coordmode, pixel, screen ; set the coords for imagesearch to absolute
   coordmode, mouse, screen ; set mouse coords to absolute
   ; retrieve the tray position and size in order to limit (speedup) the imagesearch
   WinGetPos, trayX, trayY, trayWidth, trayHeight, ahk_class Shell_TrayWnd
   MouseGetPos, mouseX, mouseY ; get the mouse position
   ; see if the icon exists in the traywindow
   ImageSearch, imageX, imageY, trayX, trayY, trayX+trayWidth, trayY+trayHeight, *Icon%iconstr%
   coordmode, pixel, relative ; reset the coords for imagesearch to default
   coordmode, mouse, relative ; reset mouse coords to default
   if ErrorLevel > 0 ; image not found or an image search error occurred
      return %errorlevel%
   imageXlarge := imageX+sm_cxsmicon ; the outerlimit for X
   imageYlarge := ImageY+sm_cysmicon ; the outerlimit for Y
   if mouseX between %imageX% and %imageXlarge%
      if mouseY between %imageY% and %imageYlarge%
         return 0 ; if the mouse is over the designated icon
   return 3 ; 3 means mouse not over the icon
}
Back to top
View user's profile Send private message
Tasman



Joined: 20 May 2006
Posts: 21

PostPosted: Mon Sep 04, 2006 4:02 pm    Post subject: IconViewer Reply with quote

In order to get the icon number from a file you can use BeardBoy's iconviewer.
http://www.autohotkey.com/forum/viewtopic.php?t=5587&highlight=iconviewer
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Tue Sep 05, 2006 1:46 am    Post subject: Reply with quote

Automating tray icons is almost an FAQ, so I know people will appreciate this script as another means of doing it.
Back to top
View user's profile Send private message Send e-mail
Tasman



Joined: 20 May 2006
Posts: 21

PostPosted: Thu Sep 07, 2006 5:12 pm    Post subject: Other possibilities Reply with quote

Just to expand a little on how it could be used.

First of all by changing the line "iconstr = 2 pifmgr.dll" any other tray icon can be selected.

Also by making additions to the lower lines in the iconfind function with detection of left/right/middle/double mouse click, or holding down one of the keys etc. while hovering over an icon it is possible to add extra events (menus etc.) to any program with an icon in the tray.

For instance you could set it to detect a control key down while hovering over your firewall icon and show a menu with clickable links to other security programs, network properties and links to firewall testing sites.
Or open up a menu with links to your music/sound/CD/DVDburning programs when holding down the control key while hovering over the default windows volume icon.

Code:
if mouseX between %imageX% and %imageXlarge%
      if mouseY between %imageY% and %imageYlarge%
             if "add you click/key etc. detection here"
                return 0 ; if the mouse is over the designated icon and the event above has happened


Extra timers can be easily added in order to use the function for multiple icons
Back to top
View user's profile Send private message
poo_noo



Joined: 08 Dec 2006
Posts: 93
Location: Sydney Australia

PostPosted: Sat May 12, 2007 12:34 pm    Post subject: Reply with quote

Is there a way to change the colour of the balloon text and /or the balloon background via AHK ?

Thanks in advance

PO
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sun May 13, 2007 8:44 am    Post subject: Reply with quote

No, these are global settings in Windows, unless making a special custom window in place of the tooltip.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Paulo-nli
Guest





PostPosted: Sun May 13, 2007 10:32 am    Post subject: Reply with quote

poo_noo wrote:
Is there a way to change the colour of the balloon text and /or the balloon background via AHK ?


Sean showed us some nice tricks with tooltips|traytip windows.
Read his "Color Zoomer/Picker" script.
(http://www.autohotkey.com/forum/topic18167.html&sid=de3b72662209273e57ecb2928982e822).
In this script the tooltip is altered in many ways.

To just change the colors:

Code:

TrayTip , Title, Text, 10, 1
WinGet, hWnd, ID, ahk_class tooltips_class32
SendMessage, 1043, 0xFF00FF, 0,, ahk_id %hWnd% ;1043=TTM_SETTIPBKCOLOR
SendMessage, 1044, 0xFFFFFF, 0,, ahk_id %hWnd% ;1044=TTM_SETTIPTEXTCOLOR
Sleep, 5000
TrayTip
ToolTip, blabla
WinGet, hWnd2, ID, ahk_class tooltips_class32
SendMessage, 1043, 0x0000FF, 0,, ahk_id %hWnd2%
SendMessage, 1044, 0xFF0000, 0,, ahk_id %hWnd2%
Sleep, 5000
ToolTip
Back to top
poo_noo



Joined: 08 Dec 2006
Posts: 93
Location: Sydney Australia

PostPosted: Mon May 14, 2007 7:37 am    Post subject: Reply with quote

Thanks ! Thats Fantastic !
Back to top
View user's profile Send private message Visit poster's website
bmcclure



Joined: 24 Nov 2007
Posts: 446

PostPosted: Sat Nov 24, 2007 9:40 pm    Post subject: Reply with quote

Great work Tasman! This truly works great.

I have done some slight modifications and I'm now using it to display a custom tray menu made with MMenu on a right/left click of my program's tray icon.

I like to check if functions evaluate to True or False, so I changed the return codes in IconFind(). I also modified it so I can just specify a .ico file (though it ideally needs to work with both types):
Code:
IconFind(IconStr,Sm_CxSmIcon,Sm_CySmIcon)
; checks to see if the mouse is over the given tray icon
; returns:
; 0 if mouse is NOT over the icon or the command failed
; 1 if cursor is over the icon
; ErrorLevel is set if the command fails for any reason
{
   coordmode, pixel, screen ; set the coords for imagesearch to absolute
   coordmode, mouse, screen ; set mouse coords to absolute
   ; retrieve the tray position and size in order to limit (speedup) the imagesearch
   WinGetPos, trayX, trayY, trayWidth, trayHeight, ahk_class Shell_TrayWnd
   MouseGetPos, mouseX, mouseY ; get the mouse position
   ; see if the icon exists in the traywindow
   ImageSearch, imageX, imageY, trayX, trayY, trayX+trayWidth, trayY+trayHeight, %IconStr%
   coordmode, pixel, relative ; reset the coords for imagesearch to default
   coordmode, mouse, relative ; reset mouse coords to default
   if ErrorLevel > 0 ; image not found or an image search error occurred
      return 0
   imageXlarge := imageX+Sm_CxSmIcon ; the outerlimit for X
   imageYlarge := ImageY+Sm_CySmIcon ; the outerlimit for Y
   if mouseX between %imageX% and %imageXlarge%
      if mouseY between %imageY% and %imageYlarge%
         return 1 ; if the mouse is over the designated icon
   ErrorLevel = 0
   return 0 ; mouse not over the icon
}



I put this in directly after my custom menu is created (but not shown):
Code:
IconToFind = icon.ico ; Set the filename of the desired icon

; get the icon sizes (system small) in order to determine the size of the area for the mouseover event
SysGet, Sm_CxSmIcon, 49
SysGet, Sm_CySmIcon, 50
   
; Start monitoring if user is hovering over icon
HotKey, LButton, LeftClickTray, Off ; Enabled if pointer over the tray icon
Hotkey, RButton, RightClickTray, Off ; Enabled if pointer over tray icon
SetTimer, MouseOverIcon, 250


And the associated Subs:
Code:
MouseOverIcon:
   If IconFind(IconToFind,Sm_CxSmIcon,Sm_CySmIcon) {
      ; Mouse is over icon, check for clicks
      HotKey, LButton, On
      Hotkey, RButton, On
      SetTimer, MouseOffIcon, 250
   }
Return

MouseOffIcon:
   SetTimer, MouseOverIcon, Off
   If Not IconFind(IconToFind,Sm_CxSmIcon,Sm_CySmIcon) {
      ; Mouse has moved off icon
      Hotkey,  LButton, Off
      Hotkey,  RButton, Off
      SetTimer, MouseOffIcon, Off
      SetTimer,  MouseOverIcon, On
   }
Return

LeftClickTray:
RightClickTray:
   CoordMode, Mouse, Screen
   MouseGetPos, curMouseX, curMouseY
   MMenu_Show(TrayMenu, curMouseX, curMouseY, "TrayMenuClick", "ITrayMenuIsShowing UTrayMenuIsClosing")
Return

TrayMenuIsShowing:
   SetTimer, MouseOffIcon, Off
   SetTimer, MouseOverIcon, Off
   Hotkey,  LButton, Off
   Hotkey,  RButton, Off
Return

TrayMenuIsClosing:
   SetTimer, MouseOffIcon, On
   SetTimer, MouseOverIcon, On
Return


And it works great! There's a 1/4 second delay after mousing over the icon when it doesn't work, but I hardly notice it and I don't want to poll the timer much more often than that.

Oh, and I've removed all default tray menu items so that there is no tray menu. Otherwise before the timer enables the LButton and RButton hotkeys the old menu would still work.

Anyway, just wanted to let you know that I'm making good use of your script and it's excellent! Thanks.
_________________
-Ben

SteamLab
SteamLab Wiki

[Broken] - My industrial music [on GarageBand]
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2558
Location: Australia, Qld

PostPosted: Wed Nov 28, 2007 10:51 am    Post subject: Reply with quote

bmcclure wrote:
I have done some slight modifications and I'm now using it to display a custom tray menu made with MMenu on a right/left click of my program's tray icon.
There's a much simpler way to hook mouse events on the script's tray icon:
Code:
Menu, M, Add, Note that this is, DoNothing
Menu, M, Add, NOT the tray menu., DoNothing
OnMessage(0x404, "AHK_NOTIFYICON")

AHK_NOTIFYICON(wParam, lParam)
{
    if (lParam = 0x205) ; WM_RBUTTONUP
    {
        SetTimer, ShowRbuttonMenu, -1
        return 0
    }
}

ShowRbuttonMenu:
    Menu, M, Show
DoNothing:
return
This should also work for other mouse buttons (see "Windows Messages" in the help file for the relevant WM_*BUTTONUP/DOWN codes.) iirc, you can detect mouse movement over the icon, but detecting when the mouse leaves the icon is not as easy.

(The timer ensures that AHK_NOTIFYICON() will catch the message even if the user right-clicks the icon while the menu is still showing.)
Back to top
View user's profile Send private message
bmcclure



Joined: 24 Nov 2007
Posts: 446

PostPosted: Sun Dec 02, 2007 7:53 pm    Post subject: Reply with quote

Amazing. I cannot thank you enough. The tray menu works SO much better because of the OnMessage event instead of my primitive timer method.

Not to mention it took about 5 minutes to implement into my already-working script. Thanks again Smile
_________________
-Ben

SteamLab
SteamLab Wiki

[Broken] - My industrial music [on GarageBand]
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Sun Dec 02, 2007 9:33 pm    Post subject: Reply with quote

lexikos wrote:
a much simpler way to hook mouse events on the script's tray icon


Very Happy Very Happy Thanks. This is very useful.
Back to top
View user's profile Send private message
Erittaf



Joined: 03 Nov 2007
Posts: 182

PostPosted: Fri Feb 22, 2008 1:00 am    Post subject: Reply with quote

Is there a good way to detect if the mouse has left the tray Icon? what about if it is over a particular window? I know the ladder is prolly easy as the window spy does this for us (haven't looked around much yet), however the first question has me stumped.
Back to top
View user's profile Send private message Send e-mail
bmcclure



Joined: 24 Nov 2007
Posts: 446

PostPosted: Fri Feb 22, 2008 5:43 pm    Post subject: Reply with quote

If worse comes to worse you can always go an image search at the location under the mouse and see if it matches the tray icon's image. Maybe not ideal, but that is one option.
_________________
-Ben

SteamLab
SteamLab Wiki

[Broken] - My industrial music [on GarageBand]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group