Jump to content

Add more icons in the notification area


Go to solution Solved by power1power1,

Recommended Posts

  • Moderators

johnmcloud,

As so often, I went a different way:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <WinAPIShellEx.au3>
#include <GuiToolbar.au3>
#include <WinAPI.au3>
#include <GuiMenu.au3>

OnAutoItExitRegister("_OnAutoItExit")

; Get systray handle
Global $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')
; To store icon IDs
Global $aIcons[2]
; Selection of icons from shell32 DLL
Global $aIcon_Index[2] = [130, 166]
; Icon ID number
Global $iIcon_ID

; Create hidden menu GUI
$hGUI_Menu = GUICreate("Menu", 10, 10, -1, -1, $WS_POPUP, $WS_EX_TOOLWINDOW)

$cMenu_Label = GUICtrlCreateLabel("", 10, 10)
$cMenu_Dummy = GUICtrlCreateDummy()
$cMenu_Context = GUICtrlCreateContextMenu($cMenu_Dummy)
$hMenu_Context = GUICtrlGetHandle($cMenu_Context)

GUISetState(@SW_HIDE)

; Create placeholders for menu items
Global $cItem_1, $cItem_2, $cItem_About

; Create a non-hidden GUI
$hGUI = GUICreate("Test", 200, 200)

$cButton_Add = GUICtrlCreateButton("Add icons", 10, 10, 80, 30)

; Create all dummy controls in this GUI because they do not work if the GUI is hidden
$cHit_Dummy = GUICtrlCreateDummy()
$cItem_1_Dummy = GUICtrlCreateDummy()
$cItem_2_Dummy = GUICtrlCreateDummy()
$cItem_About_Dummy = GUICtrlCreateDummy()
$cMouse_Dummy = GUICtrlCreateDummy()

GUISetState()

; Create required icon structures
Global $tIcon_Rect = DllStructCreate("int Left;int Top;int Right;int Bottom")
Global $tNID = DllStructCreate($tagNOTIFYICONDATA)
DllStructSetData($tNID, 'Size', DllStructGetSize($tNID))
DllStructSetData($tNID, 'hWnd', $hGUI)

; Register message to receive menu selection
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

; Set up mouse hook
Global $hProc = DllCallbackRegister ("_Mouse_Handler", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hProc), $hmod)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton_Add
            For $i = 0 To 1
                $iRet = _Create_Icon($i + 1)
                $aIcons[$i] = $iRet
            Next
        Case $cItem_1_Dummy
            MsgBox($MB_SYSTEMMODAL, "Item 1", "Icon " & $iIcon_ID & " was right-clicked" & @CRLF & "Item 1 selected")
        Case $cItem_2_Dummy
            MsgBox($MB_SYSTEMMODAL, "Item 2", "Icon " & $iIcon_ID & " was right-clicked" & @CRLF & "Item 2 selected")
        Case $cItem_About_Dummy
            MsgBox($MB_SYSTEMMODAL, "Info", "Icon " & $iIcon_ID & " was right-clicked" & @CRLF & "About selected")
        Case $cMouse_Dummy
            _Check_Icons()
        Case $cHit_Dummy
            ; Which icon?
            $iIcon_ID = GUICtrlRead($cHit_Dummy)
            ; Create menu items for clicked icon
            $cItem_1 = GUICtrlCreateMenuItem("Icon " & $iIcon_ID & " - Item 1", $cMenu_Context)
            $cItem_2 = GUICtrlCreateMenuItem("Icon " & $iIcon_ID & " - Item 2", $cMenu_Context)
            $cItem_Sep = GUICtrlCreateMenuItem("", $cMenu_Context)
            $cItem_About = GUICtrlCreateMenuItem("Icon " & $iIcon_ID & " - About", $cMenu_Context)
            ; Get position of mouse
            $aMPos = MouseGetPos()
            ; Move menu GUI to there
            WinMove($hGUI_Menu, "", $aMPos[0] - 20, $aMPos[1] - 20)
            ; Show popup menu
            _GUICtrlMenu_TrackPopupMenu ($hMenu_Context, $hGUI_Menu)
            ; Destroy menu items
            GUICtrlDelete($cItem_1)
            GUICtrlDelete($cItem_2)
            GUICtrlDelete($cItem_Sep)
            GUICtrlDelete($cItem_About)
    EndSwitch

WEnd

Func _WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)

    #forceref $iMsg, $ilParam
    ; If menu GUI
    If $hWnd = $hGUI_Menu Then
        ; Which item was clicked
        Switch $iwParam
            Case $cItem_1
                GUICtrlSendToDummy($cItem_1_Dummy)
            Case $cItem_2
                GUICtrlSendToDummy($cItem_2_Dummy)
            Case $cItem_About
                GUICtrlSendToDummy($cItem_About_Dummy)
        EndSwitch
    EndIf

EndFunc   ;==>WM_COMMAND

Func _Create_Icon($iID)

    $iIcon_Index = $aIcon_Index[$iID - 1]
    DllStructSetData($tNID, 'ID', $iID)
    DllStructSetData($tNID, 'Flags', $NIF_ICON)
    DllStructSetData($tNID, 'hIcon', _WinAPI_ShellExtractIcon(@SystemDir & '\shell32.dll', $iIcon_Index, 16, 16))
    _WinAPI_ShellNotifyIcon($NIM_ADD, $tNID)

    DllStructSetData($tNID, "Flags", $NIF_TIP)
    DllStructSetData($tNID, "Tip", "Add Icon " & $iID)
    _WinAPI_ShellNotifyIcon($NIM_MODIFY, $tNID)

    Return _GUICtrlToolbar_ButtonCount($hSysTray_Handle) - 1

EndFunc   ;==>_Create_Icon

Func _Check_Icons()

    ; Get count of buttons
    $iCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    ; Get position of tray
    $aTray_Pos = WinGetPos($hSysTray_Handle)
    ; get position of mouse
    $aMouse_Pos = MouseGetPos()
    ; Loop through buttons
    For $i = 0 To $iCount - 1
        ; Get button position in tray
        $aIcon_Pos = _GUICtrlToolbar_GetButtonRect($hSysTray_Handle, $i)
        ; Check absolute position of icon against mouse
        If ($aIcon_Pos[0] + $aTray_Pos[0] <= $aMouse_Pos[0]) And ($aIcon_Pos[2] + $aTray_Pos[0] >= $aMouse_Pos[0]) Then
            If ($aIcon_Pos[1] + $aTray_Pos[1] <= $aMouse_Pos[1]) And ($aIcon_Pos[3] + $aTray_Pos[1] >= $aMouse_Pos[1]) Then
                ; If this icon clicked
                For $j = 0 To 1
                    ; Check against the stored IDs for the add icons
                    If $aIcons[$j] = $i Then
                        ; If found then fire dummy
                        GUICtrlSendToDummy($cHit_Dummy, $j + 1)
                        ; No point in looking further
                        ExitLoop 2
                    EndIf
                Next
            EndIf
        EndIf
    Next

EndFunc

Func _Mouse_Handler($nCode, $wParam, $lParam)

    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    ; look for secondary up
    Switch BitAND($WParam, 0xFFFF)
        Case $WM_RBUTTONUP
            GUICtrlSendToDummy($cMouse_Dummy)
    EndSwitch

EndFunc

Func _OnAutoItExit()
    For $i = 0 To 1
        DllStructSetData($tNID, 'ID', $i + 1)
        _WinAPI_ShellNotifyIcon($NIM_DELETE, $tNID)
    Next
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hProc)
EndFunc   ;==>_OnAutoItExit
Now to see if I can tidy it up a bit! ;)

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

  • Moderators

johnmcloud,

And this is my final word on the subject: :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <WinAPIShellEx.au3>
#include <GuiToolbar.au3>
#include <WinAPI.au3>
#include <GuiMenu.au3>

OnAutoItExitRegister("_OnAutoItExit")

; Get systray handle
Global $hSysTray_Handle = ControlGetHandle('[Class:Shell_TrayWnd]', '', '[Class:ToolbarWindow32;Instance:1]')
; To store icon IDs
Global $aIcon_ID[2] = ["Add icon 1", "Add icon 2"]
; Selection of icons from shell32 DLL
Global $aIcon_Index[2] = [130, 166]
; Icon ID number
Global $iIcon_ID

; Create hidden menu GUI
$hGUI_Menu = GUICreate("Menu", 10, 10, -1, -1, $WS_POPUP, $WS_EX_TOOLWINDOW)

$cMenu_Label = GUICtrlCreateLabel("", 10, 10)
$cMenu_Dummy = GUICtrlCreateDummy()
$cMenu_Context = GUICtrlCreateContextMenu($cMenu_Dummy)
$hMenu_Context = GUICtrlGetHandle($cMenu_Context)

GUISetState(@SW_HIDE)

; Create placeholders for menu items
Global $cItem_1, $cItem_2, $cItem_About

; Create a non-hidden GUI
$hGUI = GUICreate("Test", 200, 200)

$cButton_Add = GUICtrlCreateButton("Add icons", 10, 10, 80, 30)

; Create all dummy controls in this GUI because they do not work if the GUI is hidden
$cHit_Dummy = GUICtrlCreateDummy()
$cItem_1_Dummy = GUICtrlCreateDummy()
$cItem_2_Dummy = GUICtrlCreateDummy()
$cItem_About_Dummy = GUICtrlCreateDummy()

GUISetState()

; Create required icon structures
Global $tIcon_Rect = DllStructCreate("int Left;int Top;int Right;int Bottom")
Global $tNID = DllStructCreate($tagNOTIFYICONDATA)
DllStructSetData($tNID, 'Size', DllStructGetSize($tNID))
DllStructSetData($tNID, 'hWnd', $hGUI)

; Register message to receive menu selection
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

; Set up mouse hook
Global $hProc = DllCallbackRegister ("_Mouse_Handler", "long", "int;wparam;lparam")
Global $hmod = _WinAPI_GetModuleHandle(0)
Global $hHook = _WinAPI_SetWindowsHookEx($WH_MOUSE_LL, DllCallbackGetPtr($hProc), $hmod)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton_Add
            For $i = 0 To 1
                ; Create icon and store ID
                $aIcon_ID[$i] = _Create_Icon($i + 1)
            Next
        Case $cItem_1_Dummy
            MsgBox($MB_SYSTEMMODAL, "Item 1", "Icon " & $iIcon_ID & " was right-clicked" & @CRLF & "Item 1 selected")
        Case $cItem_2_Dummy
            MsgBox($MB_SYSTEMMODAL, "Item 2", "Icon " & $iIcon_ID & " was right-clicked" & @CRLF & "Item 2 selected")
        Case $cItem_About_Dummy
            MsgBox($MB_SYSTEMMODAL, "Info", "Icon " & $iIcon_ID & " was right-clicked" & @CRLF & "About selected")
        Case $cHit_Dummy
            _Check_Icons()
    EndSwitch

WEnd

Func _WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)

    #forceref $iMsg, $ilParam
    ; If menu GUI
    If $hWnd = $hGUI_Menu Then
        ; Which item was clicked
        Switch $iwParam
            Case $cItem_1
                GUICtrlSendToDummy($cItem_1_Dummy)
            Case $cItem_2
                GUICtrlSendToDummy($cItem_2_Dummy)
            Case $cItem_About
                GUICtrlSendToDummy($cItem_About_Dummy)
        EndSwitch
    EndIf

EndFunc   ;==>WM_COMMAND

Func _Create_Icon($iID)

    $iIcon_Index = $aIcon_Index[$iID - 1]
    DllStructSetData($tNID, 'ID', $iID)
    DllStructSetData($tNID, 'Flags', $NIF_ICON)
    DllStructSetData($tNID, 'hIcon', _WinAPI_ShellExtractIcon(@SystemDir & '\shell32.dll', $iIcon_Index, 16, 16))
    _WinAPI_ShellNotifyIcon($NIM_ADD, $tNID)

    $sToolTip = $aIcon_ID[$iID - 1]
    DllStructSetData($tNID, "Flags", $NIF_TIP)
    DllStructSetData($tNID, "Tip", $sToolTip)
    _WinAPI_ShellNotifyIcon($NIM_MODIFY, $tNID)

    ; Get icon ID based on tooltip
    For $i = 1 To 100
        If $sToolTip = _GUICtrlToolbar_GetButtonText($hSysTray_Handle, $i) Then
            ; Return for further use
            Return $i
        EndIf
    Next

EndFunc   ;==>_Create_Icon

Func _Check_Icons()

    ; Get count of buttons
    $iCount = _GUICtrlToolbar_ButtonCount($hSysTray_Handle)
    ConsoleWrite($iCount & @CRLF)

    ; Get position of tray
    $aTray_Pos = WinGetPos($hSysTray_Handle)
    ; Get position of mouse
    $aMouse_Pos = MouseGetPos()
    ; Is mouse over taskbar?
    If $aMouse_Pos[1] >= $aTray_Pos[1] Then
        ; Loop through add icons buttons
        For $iIcon_ID = 1 To 2
            ; Get button position in tray
            $aIcon_Pos = _GUICtrlToolbar_GetButtonRect($hSysTray_Handle, $aIcon_ID[$iIcon_ID - 1])
            ; Check absolute position of icon against mouse
            If ($aIcon_Pos[0] + $aTray_Pos[0] <= $aMouse_Pos[0]) And ($aIcon_Pos[2] + $aTray_Pos[0] >= $aMouse_Pos[0]) Then
                ; Create menu items for clicked icon
                $cItem_1 = GUICtrlCreateMenuItem("Icon " & $iIcon_ID & " - Item 1", $cMenu_Context)
                $cItem_2 = GUICtrlCreateMenuItem("Icon " & $iIcon_ID & " - Item 2", $cMenu_Context)
                $cItem_Sep = GUICtrlCreateMenuItem("", $cMenu_Context)
                $cItem_About = GUICtrlCreateMenuItem("Icon " & $iIcon_ID & " - About", $cMenu_Context)
                ; Get position of mouse
                $aMPos = MouseGetPos()
                ; Move menu GUI to there
                WinMove($hGUI_Menu, "", $aMPos[0] - 20, $aMPos[1] - 20)
                ; Show popup menu
                _GUICtrlMenu_TrackPopupMenu ($hMenu_Context, $hGUI_Menu)
                ; Destroy menu items
                GUICtrlDelete($cItem_1)
                GUICtrlDelete($cItem_2)
                GUICtrlDelete($cItem_Sep)
                GUICtrlDelete($cItem_About)
                ExitLoop
            EndIf
        Next
    EndIf

EndFunc

Func _Mouse_Handler($nCode, $wParam, $lParam)

    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    ; look for secondary up
    Switch BitAND($WParam, 0xFFFF)
        Case $WM_RBUTTONUP
            GUICtrlSendToDummy($cHit_Dummy)
    EndSwitch

EndFunc

Func _OnAutoItExit()
    For $i = 0 To 1
        DllStructSetData($tNID, 'ID', $i + 1)
        _WinAPI_ShellNotifyIcon($NIM_DELETE, $tNID)
    Next
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hProc)
EndFunc   ;==>_OnAutoItExit
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...