Jump to content

How to automatically "click" my script's tray icon?


Recommended Posts

My script has a tray icon, with tray menu items. I'd like a global hotkey to bring up this menu. Can I do this? Should it be done via somehow clicking the tray icon (I have tried but failed to figure out how to do this), or should I instead just forget this approach and instead have the hotkey popup a new window with a menu?

Thanks in advance...

Link to comment
Share on other sites

Sample script for showing the traymenu somewhere:

#include <Constants.au3>

Opt("TrayMenuMode", 1)
Opt("WinTitleMatchMode", 4)

Global Const $TPM_BOTTOMALIGN   = 0x0020

$aboutItem  = TrayCreateItem("About")
TrayCreateItem("")
$exitItem   = TrayCreateItem("Exit")

HotKeySet("^!t", "ShowTrayMenu") ; Ctrl + Alt + t

While 1
    $Msg = TrayGetMsg()
    Switch $Msg
        Case $exitItem
            ExitLoop
        Case $aboutItem
            Msgbox(0, "Info", "Just for fun...")
    EndSwitch
WEnd

Exit

Func ShowTrayMenu()
    Local $stPoint = DllStructCreate("int;int")
    DllCall("user32.dll", "int", "GetCursorPos", _
                                "ptr", DllStructGetPtr($stPoint))
    
    DllCall("user32.dll", "int", "TrackPopupMenuEx", _
                                "hwnd", TrayItemGetHandle(0), _
                                "int", $TPM_BOTTOMALIGN, _
                                "int", DllStructGetData($stPoint, 1), _
                                "int", DllStructGetData($stPoint, 2), _
                                "hwnd", WinGetHandle("classname=AutoIt v3"), _
                                "ptr", 0)
EndFunc
Link to comment
Share on other sites

2nd possibility:

#include <Constants.au3>

Opt("TrayMenuMode", 1)
Opt("WinTitleMatchMode", 4)

Global Const $WM_USER           = 0x0400
Global Const $MY_WM_NOTIFYICON  = $WM_USER + 1
Global Const $WM_LBUTTONDOWN    = 0x0201

$aboutItem  = TrayCreateItem("About")
TrayCreateItem("")
$exitItem   = TrayCreateItem("Exit")

HotKeySet("^!t", "ShowTrayMenu") ; Ctrl + Alt + t

While 1
    $Msg = TrayGetMsg()
    Switch $Msg
        Case $exitItem
            ExitLoop
        Case $aboutItem
            Msgbox(0, "Info", "Just for fun...")
    EndSwitch
WEnd

Exit

Func ShowTrayMenu()
    DllCall("user32.dll", "int", "SendMessage", _
                                "hwnd", WinGetHandle("classname=AutoIt v3"), _
                                "int", $MY_WM_NOTIFYICON, _
                                "int", 0, _
                                "int", $WM_LBUTTONDOWN)
EndFunc

Greets :D

Holger

Link to comment
Share on other sites

  • 1 month later...
  • 13 years later...
On 7/2/2006 at 9:00 AM, Holger said:

 

#include <Constants.au3>

Opt("TrayMenuMode", 1)
Opt("WinTitleMatchMode", 4)

Global Const $WM_USER           = 0x0400
Global Const $MY_WM_NOTIFYICON  = $WM_USER + 1
Global Const $WM_LBUTTONDOWN    = 0x0201

$aboutItem  = TrayCreateItem("About")
TrayCreateItem("")
$exitItem   = TrayCreateItem("Exit")

HotKeySet("^!t", "ShowTrayMenu") ; Ctrl + Alt + t

While 1
    $Msg = TrayGetMsg()
    Switch $Msg
        Case $exitItem
            ExitLoop
        Case $aboutItem
            Msgbox(0, "Info", "Just for fun...")
    EndSwitch
WEnd

Exit

Func ShowTrayMenu()
    DllCall("user32.dll", "int", "SendMessage", _
                                "hwnd", WinGetHandle("classname=AutoIt v3"), _
                                "int", $MY_WM_NOTIFYICON, _
                                "int", 0, _
                                "int", $WM_LBUTTONDOWN)
EndFunc

 

When I run this, and use the hotkey, it first shows Script Paused and Exit menu then when I click Exit, then the next time, it shows About and Exit menu.

Shouldn't it just show the About and Exit from the beginning?

Edited by leomoon
Link to comment
Share on other sites

Link to comment
Share on other sites

  • Developers
43 minutes ago, Nine said:

Maybe @Jos can tell us.

Are you using the full SciTE4AutoIt3 version?
I assume this is because SciTE4AutoIt3 will run AutoIt3Wrapper with AutoIt3, and it in turn will shell an extra watchdog version of Autoit3Wrapper to monitor whether one wants to kill the running script via SciTE and process are still running.

At first run you can see that one of the 3 AutoIt3 background processes gets killed although nothing visual happens, so assume the watchdog process gets killed.

Jos

edit: Try this version

#include <Constants.au3>
AutoItWinSetTitle("demotraymenu")

Opt("TrayMenuMode", 1)
Opt("WinTitleMatchMode", 4)

Global Const $WM_USER = 0x0400
Global Const $MY_WM_NOTIFYICON = $WM_USER + 1
Global Const $WM_LBUTTONDOWN = 0x0201

$aboutItem = TrayCreateItem("About")
TrayCreateItem("")
$exitItem = TrayCreateItem("Exit")

HotKeySet("^!t", "ShowTrayMenu") ; Ctrl + Alt + t

While 1
    $Msg = TrayGetMsg()
    Switch $Msg
        Case $exitItem
            ExitLoop
        Case $aboutItem
            MsgBox(0, "Info", "Just for fun...")
    EndSwitch
WEnd

Exit

Func ShowTrayMenu()
    DllCall("user32.dll", "int", "SendMessage", _
            "hwnd", WinGetHandle("demotraymenu"), _
            "int", $MY_WM_NOTIFYICON, _
            "int", 0, _
            "int", $WM_LBUTTONDOWN)

EndFunc   ;==>ShowTrayMenu

 

Edited by Jos
added fix for example

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Wow that was very hard to detect!

Thanks for the fix.

So I stumbled upon this script to see if I can get the handle of active window then show a menu (to change size and other properties of that active window). I tried adding WinGetHandle just before the DllCall in the ShowTrayMenu() but I only get 0x0000000000000000:

Func ShowTrayMenu()
    ConsoleWrite(WinGetHandle('')&@LF)
    DllCall("user32.dll", "int", "SendMessage", _
            "hwnd", WinGetHandle("demotraymenu"), _
            "int", $MY_WM_NOTIFYICON, _
            "int", 0, _
            "int", $WM_LBUTTONDOWN)
EndFunc

This script is kinda hard to understand! :P

Edited by leomoon
Link to comment
Share on other sites

You can now use instead of the dllcall :

_SendMessage(WinGetHandle("demotraymenu"), $MY_WM_NOTIFYICON, 0, $WM_LBUTTONDOWN)

Just add #include <SendMessage.au3> at the beginning of the script.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...