Sign in to follow this  
Followers 0
AndyS01

How can I dynamically change the text of a System menu

3 posts in this topic

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:

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

Share this post


Link to post
Share on other sites



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:

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

 

Share this post


Link to post
Share on other sites

The 'item text' issue was a rookie mistake  :doh: .  Your _WM_ENTERMENULOOP solution worked for me.  Thank you.

Share this post


Link to post
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
Sign in to follow this  
Followers 0