AndyS01 Posted July 2, 2013 Posted July 2, 2013 I have a system menu button that contains the name of certain files that can be selected, and I want to show the file size next to each name, using the updateAllMenuItems() function. This must be done when the "Files" menu button is pressed. I cannot figure out how to trap this event. There is another, related issue: How can I dynamically change the text of my menu items? Here is my test code: expandcollapse popup#include <GUIConstantsEx.au3> Opt("GUICloseOnESC", 1) ; ESC closes GUI Opt("GUIOnEventMode", 1) ; Change to OnEvent mode Opt('MustDeclareVars', 1) OnAutoItExitRegister("Event_GUIClose") Global $aIDs[4], $aFNs[4] _Main() Exit (1) Func _Main() Local $txt, $iID_Menu, $x, $fn, $txt GUICreate("test", 200, 200) $iID_Menu = GUICtrlCreateMenu("Files") For $ndx = 0 To UBound($aFNs) - 1 $fn = "testfile" & $ndx & ".txt" $aFNs[$ndx] = $fn FileDelete($fn) for $x = 0 to (($ndx + 1) * 10) filewriteline($fn, "Line " & $x ) Next $aIDs[$ndx] = GUICtrlCreateMenuItem($fn, $iID_Menu) Next updateAllMenuItems() ; Set the text of each menu item GUISetOnEvent($GUI_EVENT_CLOSE, 'Event_GUIClose') ; CLOSE events GUISetState() ; Make everything visible While (1) Sleep(250) WEnd EndFunc ;==>_Main Func Event_GUIClose() Exit (1) EndFunc ;==>Event_GUIClose ; Add the file size to each menu item Func updateAllMenuItems() Local $ndx, $id, $fn, $size, $str For $ndx = 0 To UBound($aFNs) - 1 $id = $aIDs[$ndx] $fn = $aFNs[$ndx] $size = FileGetSize($fn) $str = StringFormat("%-10s - %d bytes", $aFNs[$ndx], $size) setMenuItemText($aIDs, $str) Next EndFunc ;==>updateMenuText ; Change the text of a specified menu item Func setMenuItemText($id, $txt) ConsoleWrite("+++: ID " & $id & " = " & $txt & @CRLF) GUICtrlSetData($id, $txt) EndFunc ;==>setMenuText Note that adding text to my menu items in the setMenuItemText() function does not work, and I have no code to trap the 'Files" menu button press so I can call updateAllMenuItems() before the menu items are displayed.
Moderators Melba23 Posted July 3, 2013 Moderators Posted July 3, 2013 AndyS01, You do not update the menu item text because you are passing the array and not the specific ControlID to the setMenuItemText function. Change the calling line to read: setMenuItemText($id, $str) ; And not setMenuItemText($aIDs, $str) and it will work. As to registering the click on the menu item itself, you can do it this way: expandcollapse popup#include <GUIConstantsEx.au3> #Include <GuiMenu.au3> Opt("GUIOnEventMode", 1) ; Change to OnEvent mode OnAutoItExitRegister("Event_GUIClose") Global $aIDs[4], $aFNs[4] _Main() Func _Main() Local $txt, $iID_Menu, $x, $fn, $txt GUICreate("test", 200, 200) GUISetOnEvent($GUI_EVENT_CLOSE, 'Event_GUIClose') ; CLOSE events $iID_Menu = GUICtrlCreateMenu("Files") For $ndx = 0 To UBound($aFNs) - 1 $fn = "testfile" & $ndx & ".txt" $aFNs[$ndx] = $fn FileDelete($fn) For $x = 0 To (($ndx + 1) * 10) FileWriteLine($fn, "Line " & $x) Next $aIDs[$ndx] = GUICtrlCreateMenuItem($fn, $iID_Menu) Next updateAllMenuItems() ; Set the text of each menu item GUISetState() ; Make everything visible GUIRegisterMsg(0x0211,"_WM_ENTERMENULOOP") ; WM_ENTERMENULOOP While (1) Sleep(250) WEnd EndFunc ;==>_Main Func Event_GUIClose() Exit (1) EndFunc ;==>Event_GUIClose ; Add the file size to each menu item Func updateAllMenuItems() Local $ndx, $id, $fn, $size, $str For $ndx = 0 To UBound($aFNs) - 1 $id = $aIDs[$ndx] $fn = $aFNs[$ndx] $size = FileGetSize($fn) $str = StringFormat("%-10s - %d bytes", $aFNs[$ndx], $size) setMenuItemText($id, $str) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Use the index and not the array here Next EndFunc ;==>updateAllMenuItems ; Change the text of a specified menu item Func setMenuItemText($id, $txt) ConsoleWrite("+++: ID " & $id & " = " & $txt & @CRLF) GUICtrlSetData($id, $txt) EndFunc ;==>setMenuItemText Func _WM_ENTERMENULOOP($hWnd, $iMsg, $wParam, $lParam) $hMenu = _GUICtrlMenu_GetMenu($hWnd) $iCount = _GUICtrlMenu_GetItemCount($hMenu) - 1 For $i = 0 To $iCount Local $tRect = _GUICtrlMenu_GetItemRectEx($hWnd, $hMenu, $i) Local $aMousePos = MouseGetPos() Local $aRes = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $aMousePos[0], "int", $aMousePos[1]) If Not @error And $aRes[0] Then If _GUICtrlMenu_GetItemText($hMenu, $i) = "Files" Then ConsoleWrite("File menu selected" & @CRLF) ExitLoop EndIf EndIf Next EndFunc A bit of a roundabout way to do it, but I do not know of any other. Perhaps someone else can provide a more elegant solution. 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
AndyS01 Posted July 3, 2013 Author Posted July 3, 2013 The 'item text' issue was a rookie mistake . Your _WM_ENTERMENULOOP solution worked for me. Thank you.
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