Moderators Melba23 Posted November 14, 2013 Moderators Share Posted November 14, 2013 johnmcloud,As so often, I went a different way:expandcollapse popup#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 ;==>_OnAutoItExitNow to see if I can tidy it up a bit! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 15, 2013 Moderators Share Posted November 15, 2013 johnmcloud,And this is my final word on the subject: expandcollapse popup#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 ;==>_OnAutoItExitM23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
johnmcloud Posted November 15, 2013 Share Posted November 15, 2013 Very good work Melba, BAUE! ( Best Autoit User Ever ) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now