Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

MouseOver TrayIcon, triggering an event


  • Please log in to reply
18 replies to this topic
Tasman
  • Members
  • 21 posts
  • Last active: Sep 04 2006 08:33 PM
  • Joined: 20 May 2006
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

#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
}


Tasman
  • Members
  • 21 posts
  • Last active: Sep 04 2006 08:33 PM
  • Joined: 20 May 2006
In order to get the icon number from a file you can use BeardBoy's iconviewer.
http://www.autohotke...ight=iconviewer

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Automating tray icons is almost an FAQ, so I know people will appreciate this script as another means of doing it.

Tasman
  • Members
  • 21 posts
  • Last active: Sep 04 2006 08:33 PM
  • Joined: 20 May 2006
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.

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

poo_noo
  • Members
  • 251 posts
  • Last active: Jan 28 2015 08:33 PM
  • Joined: 08 Dec 2006
Is there a way to change the colour of the balloon text and /or the balloon background via AHK ?

Thanks in advance

PO

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
No, these are global settings in Windows, unless making a special custom window in place of the tooltip.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Paulo-nli
  • Guests
  • Last active:
  • Joined: --

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.autohotke...7ecb2928982e822).
In this script the tooltip is altered in many ways.

To just change the colors:

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


poo_noo
  • Members
  • 251 posts
  • Last active: Jan 28 2015 08:33 PM
  • Joined: 08 Dec 2006
Thanks ! Thats Fantastic !

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
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):
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):
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:
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.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

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:
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.)

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
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 :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

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


:D :D Thanks. This is very useful.

Erittaf
  • Members
  • 192 posts
  • Last active: Feb 28 2013 11:49 PM
  • Joined: 02 Nov 2007
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.

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
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.

zavjah
  • Members
  • 13 posts
  • Last active: Mar 13 2013 09:51 AM
  • Joined: 01 Jul 2008
Hi bmcclure,

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 :)


I am a newby to AHK but very interested in your adaptation of the script. Would you mind posting it in a full lenght?

Thanks in Advance.
zavjah