Jump to content

Ctrl-handle of loop-created controls (TrayItems)?


davior
 Share

Recommended Posts

Hi all,

although I'm not too new in AutoIt I got along quite ok the last years by crawling the forums, thus learning the stuff I needed step by step. But today there's come the point were active help of all of you in the community is needed, and I would greatly appreciate that.

My code excerpt:

For $i = 1 To $TrayArray1[0]
        $langselect[$i] = _TrayCreateItem($TrayArray1[$i], $M41)
        Next

        GUICtrlSetOnEvent($langselect[$i], "langselect")

Problem, story and program intention:

The above code is just a part from a TrayMenu created with UDFs. For each $TrayArray1[$i] an item control shall be created, and this is working out fine. But unfortunately I've no idea how to invoke an action in OnEventMode when one of the controls is clicked. Reason: I don't know how to determine which Control invoked the action. Simply hardcoding

GUICtrlSetOnEvent($langselect[1], "langselect1")
GUICtrlSetOnEvent($langselect[2], "langselect2")

won't help cause the array reads an unknown amount of files from disk, and these file namens shall make up the different TrayItems. I know that it doesn't work out the way I have it in the snippet above. Any idea?

Have been looking around the whole sunday, but couldn't make it out unfortunately. To give another example: I would like to call functions on events of buttons created like in the AutoIt Help file for the _GUICtrlButton_SetImageList Function (Only that here the controls are buttons, not TrayItems).

What to use it for: I`d like to parse a given directory for *.ini-files containing language libraries to be able to change the gui language of my traymenu.

Thanks alot says

davior

Edited by davior
Link to comment
Share on other sites

  • Moderators

davior,

Welcome to the Autoit forums. ;)

You need to use the correct syntax for a start - TrayItemSetOnEvent and not GUICtrlSetOnEvent! :evil: Then you can use a loop and concatenation to create your items, like this:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

HotKeySet("{ESC}", "On_Exit")

Global $TrayArray1[3] = [2, "One", "Two"], $langselect[3]


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

For $i = 1 To $TrayArray1[0]
    $langselect[$i] = TrayCreateItem($TrayArray1[$i])
    TrayItemSetOnEvent(-1, "langselect" & $i)
Next

$hGUI = GUICreate("Test", 500, 500)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Exit")

GUISetState()

While 1
    Sleep(10)
WEnd

Func On_Exit()
    Exit
EndFunc

Func langselect1()
    MsgBox(0, "", "You pressed One")
EndFunc

Func langselect2()
    MsgBox(0, "", "You pressed Two")
EndFunc

Please ask if anything is unclear.

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

Plenty of misunderstanding here... But were are getting closer.

He wants to use the same function both times, and find the control ID from within the function.

Within the called user function the item identifier can be retrieved with @TRAY_ID.

That should be the final answer!

Mat

Edit:

Func langselect ()
MsgBox (0, "test", @TRAY_ID)
EndFunc ; ==> langSelect
Edited by Mat
Link to comment
Share on other sites

  • Moderators

Mat,

Quite right - my mistake! ;)

davior,

Try this instead:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

HotKeySet("{ESC}", "On_Exit")

Global $TrayArray1[3] = [2, "One", "Two"], $langselect[3]


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

For $i = 1 To $TrayArray1[0]
    $langselect[$i] = TrayCreateItem($TrayArray1[$i])
    TrayItemSetOnEvent(-1, "langselect")
Next

$hGUI = GUICreate("Test", 500, 500)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Exit")

GUISetState()

While 1
    Sleep(10)
WEnd

Func On_Exit()
    Exit
EndFunc

Func langselect()
    MsgBox(0, "", "You pressed " & TrayItemGetText(@TRAY_ID))
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

At first thanks for the lightspeed replies, at all.

@melba: Thank you for producing a running sample, my laziness pushed me just to cut out the relevant pieces of code, but yours gives a better overview. What you can't know: My traymenu is created by an UDF which uses GuiCtrlCreateItem also for TrayItems, but this is not important.

Finally Mat solved the issue somehow, he's absolutely right with @TRAY_ID. I could probably use the return value to read out the CtrlText and thus run the action I want.

Unfortunately, the GuiCtrlCreateItem Function in combination with Holger Kotschs colored TrayMenu UDF does apparently not return anything for @TRAY_ID. Bad function ;) (kidding)

So what works:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

HotKeySet("{ESC}", "On_Exit")

Global $TrayArray1[3] = [2, "One", "Two"], $langselect[3]


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

For $i = 1 To $TrayArray1[0]
    $langselect[$i] = TrayCreateItem($TrayArray1[$i])
    TrayItemSetOnEvent(-1, "langselect")
Next

$hGUI = GUICreate("Test", 500, 500)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Exit")

GUISetState()

While 1
    Sleep(10)
WEnd

Func On_Exit()
    Exit
EndFunc

Func langselect()
MsgBox (0, "test", @TRAY_ID)
EndFunc ; ==> langSelect

what not works:

#Include <Array.au3>
#Include <GUIConstantsEx.au3>
#Include <String.au3>
#Include "ModernMenuRaw.au3"
#NoTrayIcon

Opt("TrayOnEventMode", 1)

Global $lstringta
Global $lstringt0
Global $lstringt1
Global $lstringt2
Global $lstringt3
Global $lstringt4
Global $lstringt5
Global $lstringt6
Global $lstringt7
Global $lstringt8
Global $lstringt9
Global $lstringt10
Global $lstringt11
Global $lstringt12
Global $lstringt13
Global $lstringt14
Global $lstringt15
Global $lstringt16

$USBdrive = StringLeft(@ScriptDir,2)
$Icons = "MyDiricons.dll"
$langfile = @ScriptDir & "\english.ini"

;$TrayArray1 = _FileListToArray("MyDirLanguages", "*.ini", 1)
Global $TrayArray1[3] = [2, "Two", "One"]
Global $langselect[$TrayArray1[0]+1]

_ReadLanguage()

_ArraySort($TrayArray1)

#Region -- TrayTip --
$var = DriveSpaceFree($USBdrive & "")
If $var > 999 Then
    $var = Round($var/1024, 2)
    $unit ="GB"
Else
    $var = Round($var)
    $unit ="MB"
EndIf

$var2 = DriveSpaceTotal($USBdrive & "")
If $var2 > 999 Then
    $var2 = Round($var2/1024, 2)
    $unit2 ="GB"
Else
    $var2 = Round($var2)
    $unit2 ="MB"
EndIf

$free = $var & " " & $unit & " / "
$total = $var2 & " " & $unit2
#EndRegion -- TrayTip --

#Region -- TrayMenu --
$nTrayIcon1 = _TrayIconCreate("Stickware Control" & @CR & _StringProper(DriveGetLabel($USBdrive & "")) & " (" & $USBdrive & ")" & @CR & $free & $total, $Icons, -1)
$nTrayMenu1     = _TrayCreateContextMenu($nTrayIcon1)
$nSideItem1     = _CreateSideMenu($nTrayMenu1)

_SetSideMenuText($nSideItem1, "Stickware Control")
_SetSideMenuColor($nSideItem1, 0xFFFFFF); default color - white
_SetSideMenuBkColor($nSideItem1, 0x921801); bottom start color
_SetSideMenuBkGradColor($nSideItem1, 0xFBCE92); top end color

$TrayExplore = _TrayCreateItem($lstringt0)
_TrayCreateItem("")
_TrayItemSetIcon(-1, "", 0)
$M1 = _TrayCreateMenu($lstringt1)
$nSideItem2     = _CreateSideMenu($M1)
_SetSideMenuText($nSideItem2, $lstringt1)
_SetSideMenuColor($nSideItem2, 0xFFFFFF)
_SetSideMenuBkColor($nSideItem2, 0x77002A)
_SetSideMenuBkGradColor($nSideItem2, 0x934884)
    $TrayMountTrueCryptStick = _TrayCreateItem($lstringt2, $M1)
    $TrayMountTrueCryptBackup = _TrayCreateItem($lstringt3, $M1)
    _TrayCreateItem("", $M1)
    _TrayItemSetIcon(-1, "", 0)
    $TrayDismountTrueCryptStick = _TrayCreateItem($lstringt4, $M1)
    $TrayDismountTrueCryptBackup = _TrayCreateItem($lstringt5, $M1)
    _TrayCreateItem("", $M1)
    _TrayItemSetIcon(-1, "", 0)
    $TrayDismountTrueCryptAll = _TrayCreateItem($lstringt6, $M1)
$M2 = _TrayCreateMenu($lstringt7)
    $M21 = _TrayCreateMenu($lstringt8 & ' "' & $USBdrive & '"', $M2)
    $M22 = _TrayCreateMenu($lstringt9 & ' "X:"', $M2)
        $TrayRoboFullUSB = _TrayCreateItem($lstringt10, $M21)
        $TrayRoboIncUSB = _TrayCreateItem($lstringt11, $M21)
        $TrayRoboFullTCC = _TrayCreateItem($lstringt10, $M22)
        $TrayRoboIncTCC = _TrayCreateItem($lstringt11, $M22)
$M3 = _TrayCreateMenu($lstringt12)
    $M31 = _TrayCreateMenu($lstringt8 & ' "' & $USBdrive & '"', $M3)
    $M32 = _TrayCreateMenu($lstringt9 & ' "X:"', $M3)
        $TrayRoboSyncUSB2HD = _TrayCreateItem($lstringt8 & ' "' & $USBdrive & '" --> HD', $M31)
        $TrayRoboSyncHD2USB = _TrayCreateItem('HD --> ' & $lstringt8 & ' "' & $USBdrive & '"', $M31)
        $TrayRoboSyncTCC2HD = _TrayCreateItem('"X:" --> HD', $M32)
        $TrayRoboSyncHD2TCC = _TrayCreateItem('HD --> "X:"', $M32)
_TrayCreateItem("")
_TrayItemSetIcon(-1, "", 0)
$M4 = _TrayCreateMenu($lstringt13)
    $TrayEditINI1 = _TrayCreateItem("myini.ini", $M4)
    $M41 = _TrayCreateMenu($lstringt16, $M4)
        For $i = 1 To $TrayArray1[0]
        $langselect[$i] = _TrayCreateItem($TrayArray1[$i], $M41)
        GUICtrlSetOnEvent(-1, "langselect")
        Next
    _TrayCreateItem("", $M4)
    _TrayItemSetIcon(-1, "", 0)
    $TrayReboot = _TrayCreateItem($lstringt14, $M4)
_TrayCreateItem("")
_TrayItemSetIcon(-1, "", 0)
$TrayExit = _TrayCreateItem($lstringt15)
GUICtrlSetOnEvent(-1, "TrayExit")

Func SetTrayColors()
    _SetTrayBkColor(0xFFFFFF)
    _SetTrayIconBkColor(0xDBD8D8)
    _SetTrayIconBkGrdColor(0xDBD8D8)
    _SetTraySelectBkColor(0xD2BDB6)
    _SetTraySelectRectColor(0x854240)
    _SetTraySelectTextColor(0x000000)
    _SetTrayTextColor(0x000000)
EndFunc

_TrayIconSetClick($nTrayIcon1, 16)
_TrayIconSetState()

While 1
    Sleep(1000)
WEnd

Func langselect()
    MsgBox (0, "test", @TRAY_ID)
EndFunc

Func TrayExit()
    Exit
EndFunc

Func _ReadLanguage()
    ;[Tray]
    $lstringta = IniRead($langfile, "Tray", "lstringta", "")
    $lstringt0 = IniRead($langfile, "Tray", "lstringt0", "")
    $lstringt1 = IniRead($langfile, "Tray", "lstringt1", "")
    $lstringt2 = IniRead($langfile, "Tray", "lstringt2", "")
    $lstringt3 = IniRead($langfile, "Tray", "lstringt3", "")
    $lstringt4 = IniRead($langfile, "Tray", "lstringt4", "")
    $lstringt5 = IniRead($langfile, "Tray", "lstringt5", "")
    $lstringt6 = IniRead($langfile, "Tray", "lstringt6", "")
    $lstringt7 = IniRead($langfile, "Tray", "lstringt7", "")
    $lstringt8 = IniRead($langfile, "Tray", "lstringt8", "")
    $lstringt9 = IniRead($langfile, "Tray", "lstringt9", "")
    $lstringt10 = IniRead($langfile, "Tray", "lstringt10", "")
    $lstringt11 = IniRead($langfile, "Tray", "lstringt11", "")
    $lstringt12 = IniRead($langfile, "Tray", "lstringt12", "")
    $lstringt13 = IniRead($langfile, "Tray", "lstringt13", "")
    $lstringt14 = IniRead($langfile, "Tray", "lstringt14", "")
    $lstringt15 = IniRead($langfile, "Tray", "lstringt15", "")
    $lstringt16 = IniRead($langfile, "Tray", "lstringt16", "")
EndFunc

where "ModernMenuRaw.au3" is below attached

and english.ini is

[Tray]
lstringta=Use me...
lstringt0=Explore drive...
lstringt1=TrueCrypt
lstringt2=Mount portable volume...
lstringt3=Mount backup volume...
lstringt4=Dismount portable volume
lstringt5=Dismount backup volume
lstringt6=Dismount all volumes
lstringt7=Backup
lstringt8=Drive
lstringt9=Container
lstringt10=Full
lstringt11=Update
lstringt12=Sync
lstringt13=Settings
lstringt14=Apply and restart stickware
lstringt15=Exit
lstringt16=Language

@GUI_CTRLID or @GUI_CTRLHANDLE also don't return anything on this udf, as well :evil:

If you're still in the mood for help I'd appreciate it, but perhaps you simply want to enjoy your sunday evening and not mess around with that bunch of code...

ModernMenuRaw.zip

Edited by davior
Link to comment
Share on other sites

So does @GUI_CTRLID Work then? Just put it instead of @TRAY_ID.

Mat

I tried @GUI_CTRLID before and it didn't work out, but now it does, and here's why:

I extracted the menu from the whole code for not having to deal with all the function stuff while typing, and forgot:

Opt("GUIOnEventMode", 1)

which is obviously used by the UDF (might have thought about it on my own when it uses GUICtrlSetOnEvent for TrayItems) for TrayItemEvents, too.

Thanks for leading me on the trace ;)

Link to comment
Share on other sites

  • Moderators

davior,

Ah, it is always the small things...... ;)

Glad you got it working.

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