Jump to content

AutoIt Window Events and System Tray Icons


pr1
 Share

Recommended Posts

Hello,

can you process AutoIt Window events, such as left-click, right-click, etc?

I am asking because I would like to create a System Tray Icon, which, when left-clicked, would display a dialog.

Many thanks.

pr1

Global $hWnd = WinGetHandle(AutoItWinGetTitle())

DllStructSetData($tNID, 'cbSize', DllStructGetSize($tNID))

DllStructSetData($tNID, 'hWnd', $hWnd)

Link to comment
Share on other sites

  • Moderators

pr1,

I wrote this in response to another topic - I hope it helps you too! :mellow:

#include <GUIConstantsEx.au3>
#include <Constants.au3>
#Include <GuiToolBar.au3>

Opt("TrayOnEventMode", 1) ; Use event trapping for tray menu
Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown.

TrayCreateItem("About")
TrayItemSetOnEvent(-1, "On_About")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "On_Exit")

TraySetState()

TraySetClick(8)

; Set left click to act as Play/Pause or New
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, "On_Dialog")

; Set tray icon tool tip
$sToolTipText = "My Dialog"
TraySetToolTip($sToolTipText)

Global $iIcon_X, $iIcon_Y

; Find the tray icon
_Locate_Icon()

; Create and hide the GUI
GUICreate("My Dialog", 200, 200, $iIcon_X - 200, $iIcon_Y - 240)
GUISetState(@SW_HIDE)

While 1

    Switch GUIGetMsg()
        ; Pressing either [_] or [X] rehides the GUI
        Case $GUI_EVENT_CLOSE, $GUI_EVENT_MINIMIZE
            GUISetState(@SW_HIDE)
    EndSwitch

WEnd


Func On_Dialog()

    ; Show the GUI
    GUISetState(@SW_SHOW)

EndFunc

Func _Locate_Icon   ()

    ; Find taskbar
    Local $aPos = WinGetPos("[Class:Shell_TrayWnd]", "")
    ; Start icon position calculation
    $iIcon_X = $aPos[0]
    $iIcon_Y = $aPos[1]
    ; Get systray handle and position
    Local $hSysTray_Handle = ControlGetHandle("[Class:Shell_TrayWnd]", "", "[Class:ToolbarWindow32;Instance:1]")
    $aPos = ControlGetPos("[Class:Shell_TrayWnd]", "", "[Class:ToolbarWindow32;Instance:1]")
    If @error Then Return
    ; Continue icon position calculation
    $iIcon_X += $aPos[0]
    $iIcon_Y += $aPos[1]
    ; Get systray item count
    Local $iSystray_ButCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    If $iSystray_ButCount = 0 Then Return
    ; Look for tooltip by finding track title
    For $iSystray_ButtonNumber = 0 To $iSystray_ButCount - 1
        Local $sText = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $iSystray_ButtonNumber)
        If StringInStr($sText, $sToolTipText) > 0 Then
            ; Get coords of icon
            $aPos = _GUICtrlToolbar_GetButtonRect($hSysTray_Handle, $iSystray_ButtonNumber)
            ; Finalise icon position calculation
            $iIcon_X += $aPos[0]
            $iIcon_Y += $aPos[1]
            ExitLoop
        EndIf
        If $iSystray_ButtonNumber = $iSystray_ButCount - 1 Then Return 1
    Next

    Return 0

EndFunc

Func On_About()
    MsgBox(0, "About", "Whatever")
EndFunc

Func On_Exit()
    Exit
EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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