Jump to content

GuiCtrlOnEvent()


Recommended Posts

ok so i have read my file in stored it in an array and created my menu with some error checking but now im stuck on how i would create an event for each of these

Dim $pgeLuaFunctionsArrayMenus[1]
_FileReadToArray(@ScriptDir & "\PGE Lua Function List.txt", $pgeLuaFunctionsArrayMenus)

$pgeMenu = GUICtrlCreateMenu("PGE Functions")
$level_1 = ""
$level_2 = ""
$level_3 = ""

For $i = 1 To UBound($pgeLuaFunctionsArrayMenus) - 1
    If StringLeft($pgeLuaFunctionsArrayMenus[$i], 4) = "----" Then
        MsgBox(0, "Error", "Failed to read function line: " & $i & ", incorrect format = " & $pgeLuaFunctionsArrayMenus[$i])
    ElseIf StringLeft($pgeLuaFunctionsArrayMenus[$i], 3) = "---" Then
        If StringRight($pgeLuaFunctionsArrayMenus[$i], 2) = "()" Then
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenuItem(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 3), $level_2)
        Else
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenu(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 3), $level_2)
        EndIf
        $level_3 = $pgeLuaFunctionsArrayMenus[$i]
    ElseIf StringLeft($pgeLuaFunctionsArrayMenus[$i], 2) = "--" Then
        If StringRight($pgeLuaFunctionsArrayMenus[$i], 2) = "()" Then
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenuItem(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 2), $level_1)
        Else
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenu(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 2), $level_1)
        EndIf
        $level_2 = $pgeLuaFunctionsArrayMenus[$i]
        $level_3 = ""
    ElseIf StringLeft($pgeLuaFunctionsArrayMenus[$i], 1) = "-" Then
        If StringRight($pgeLuaFunctionsArrayMenus[$i], 2) = "()" Then
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenuItem(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 1), $pgeMenu)
        Else
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenu(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 1), $pgeMenu)
        EndIf
        $level_1 = $pgeLuaFunctionsArrayMenus[$i]
        $level_2 = ""
        $level_3 = ""
    Else
        MsgBox(0, "Error", "Failed to read function line: " & $i & ", incorrect format = " & $pgeLuaFunctionsArrayMenus[$i])
    EndIf
Next

i tried using GuiCtrlOnEvent(-1, "function") but that didnt work so i tried looping through them but that didnt work either, is there some way i can use a function and pass an argument because GuiCtrlOnEvent doesnt allow you to pass arguments.

thanks in advance.

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Link to comment
Share on other sites

ok so i have read my file in stored it in an array and created my menu with some error checking but now im stuck on how i would create an event for each of these

<snip>

i tried using GuiCtrlOnEvent(-1, "function") but that didnt work so i tried looping through them but that didnt work either, is there some way i can use a function and pass an argument because GuiCtrlOnEvent doesnt allow you to pass arguments.

thanks in advance.

How about posting a short demo of your issue that runs and doesn't require psychic powers to determine things like the contents of "PGE Lua Function List.txt"?

:D

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

example of the txt file

-pge

--pge.running()

--pge.delay()

--pge.version()

--pge.exit()

test script

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#include <GUIConstants.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <GuiEdit.au3>
#include <File.au3>

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Opt("GUIOnEventMode", 1)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$frmMain = GUICreate("PGE Lua Game Creator", 793, 567, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GUISetOnEvent($GUI_EVENT_CLOSE, "frmMainClose")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "frmMainMinimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "frmMainMaximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "frmMainRestore")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Dim $pgeLuaFunctionsArrayMenus[1]
Dim $pgeLuaFunctionsArray[1]
_FileReadToArray(@ScriptDir & "\PGE Lua Function List.txt", $pgeLuaFunctionsArrayMenus)
_FileReadToArray(@ScriptDir & "\PGE Lua Function List.txt", $pgeLuaFunctionsArray)

$pgeMenu = GUICtrlCreateMenu("PGE Functions")
$level_1 = ""
$level_2 = ""
$level_3 = ""

For $i = 1 To UBound($pgeLuaFunctionsArrayMenus) - 1
    If StringLeft($pgeLuaFunctionsArrayMenus[$i], 4) = "----" Then
        MsgBox(0, "Error", "Failed to read function line: " & $i & ", incorrect format = " & $pgeLuaFunctionsArrayMenus[$i])
    ElseIf StringLeft($pgeLuaFunctionsArrayMenus[$i], 3) = "---" Then
        If StringRight($pgeLuaFunctionsArrayMenus[$i], 2) = "()" Then
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenuItem(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 3), $level_2)
        Else
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenu(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 3), $level_2)
        EndIf
        $level_3 = $pgeLuaFunctionsArrayMenus[$i]
    ElseIf StringLeft($pgeLuaFunctionsArrayMenus[$i], 2) = "--" Then
        If StringRight($pgeLuaFunctionsArrayMenus[$i], 2) = "()" Then
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenuItem(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 2), $level_1)
        Else
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenu(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 2), $level_1)
        EndIf
        $level_2 = $pgeLuaFunctionsArrayMenus[$i]
        $level_3 = ""
    ElseIf StringLeft($pgeLuaFunctionsArrayMenus[$i], 1) = "-" Then
        If StringRight($pgeLuaFunctionsArrayMenus[$i], 2) = "()" Then
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenuItem(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 1), $pgeMenu)
        Else
            $pgeLuaFunctionsArrayMenus[$i] = GUICtrlCreateMenu(StringTrimLeft($pgeLuaFunctionsArrayMenus[$i], 1), $pgeMenu)
        EndIf
        $level_1 = $pgeLuaFunctionsArrayMenus[$i]
        $level_2 = ""
        $level_3 = ""
    Else
        MsgBox(0, "Error", "Failed to read function line: " & $i & ", incorrect format = " & $pgeLuaFunctionsArrayMenus[$i])
    EndIf
Next

$mainMenu = _GUICtrlMenu_GetMenu($frmMain)
$itemPGE = _GUICtrlMenu_GetItemSubMenu($mainMenu, 0)
For $i = 0 To 15
    $ItemPGESub = _GUICtrlMenu_GetItemSubMenu($itemPGE, $i)
    _GUICtrlMenu_SetMenuHeight($ItemPGESub, 220)
Next

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$pgeCode = GUICtrlCreateEdit("", 144, 32, 641, 504)
GUICtrlSetOnEvent(-1, "pgeCodeChange")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GUISetState(@SW_SHOW)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
While 1
    Sleep(100)
WEnd

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func frmMainClose()
    Exit
EndFunc;==>frmMainClose
Func frmMainMaximize()
    WinSetState("PGE Lua Game Creator", "", @SW_MAXIMIZE)
EndFunc;==>frmMainMaximize
Func frmMainMinimize()
    WinSetState("PGE Lua Game Creator", "", @SW_MINIMIZE)
EndFunc;==>frmMainMinimize
Func frmMainRestore()
    WinSetState("PGE Lua Game Creator", "", @SW_RESTORE)
EndFunc;==>frmMainRestore

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func pgeCodeChange()

EndFunc ;==>pgeCodeChange

and i need to be able to set a seperate event for each of the menu items but use the actual name of the item.

Edited by FaT3oYCG

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Link to comment
Share on other sites

example of the txt file

-pge

--pge.running()

--pge.delay()

--pge.version()

--pge.exit()

<snip>

and i need to be able to set a seperate event for each of the menu items but use the actual name of the item.

That helped a lot in understanding your problem. This method keeps everything in arrays, so you can access all the parts my numeric indexes. Notice how much less repetitive the code is. I did trim out the irrelevant parts and unused stuff.

The array $pgeLuaFunctionsArrayMenus is the _FileReadToArray() data.

The 2D array $avMenuItems holds all the menu item information required.

The 1D $avMenuLevels array handles the menu parent levels.

Demo:

#include <GuiConstantsEx.au3>
#include <GuiMenu.au3>
#include <File.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)

; Simulate reading from file:
;_FileReadToArray(@ScriptDir & "\PGE Lua Function List.txt", $pgeLuaFunctionsArrayMenus)
Global $pgeLuaFunctionsArrayMenus[9] = [8, _
        "-pge", "--pge.running()", "--pge.delay()", "--pge.version()", "--pge.exit()", _
        "-Psalty", "--PsaltyDS", "---PsaltyDS.Rocks()"]
_ArrayDisplay($pgeLuaFunctionsArrayMenus, "Debug: File data")

; 2D array for menu info:
;   [n][0] = file text
;   [n][1] = trimmed item text
;   [n][2] = menu level
;   [n][3] = controlID
Global $avMenuItems[UBound($pgeLuaFunctionsArrayMenus)][4] = [[$pgeLuaFunctionsArrayMenus[0]]]
For $i = 1 To UBound($avMenuItems) - 1
    $avMenuItems[$i][0] = $pgeLuaFunctionsArrayMenus[$i]
    $avMenuItems[$i][1] = $avMenuItems[$i][0]
    $avMenuItems[$i][2] = 0
    While (StringLeft($avMenuItems[$i][1], 1) = "-")
        $avMenuItems[$i][2] += 1
        $avMenuItems[$i][1] = StringTrimLeft($avMenuItems[$i][1], 1)
    WEnd
    If ($avMenuItems[$i][2] < 1) Or ($avMenuItems[$i][2] > 3) Then
        MsgBox(16, "Error", "Malformed entry:  " & $avMenuItems[$i][0])
        ContinueLoop
    EndIf
Next

Global $frmMain = GUICreate("PGE Lua Game Creator", 793, 567)
GUISetOnEvent($GUI_EVENT_CLOSE, "frmMainClose")

; Array of menu levels: [0] = menu, [1] thru [3] = parent levels
Global $avMenuLevels[4] = [GUICtrlCreateMenu("PGE Functions")]

For $i = 1 To UBound($avMenuItems) - 1
    If StringRight($avMenuItems[$i][1], 2) = "()" Then
        $avMenuItems[$i][3] = GUICtrlCreateMenuItem($avMenuItems[$i][1], $avMenuLevels[$avMenuItems[$i][2] - 1])
        GUICtrlSetOnEvent(-1, "_MenuItemHit")
    Else
        $avMenuItems[$i][3] = GUICtrlCreateMenu($avMenuItems[$i][1], $avMenuLevels[$avMenuItems[$i][2] - 1])
    EndIf
    $avMenuLevels[$avMenuItems[$i][2]] = $avMenuItems[$i][3]
    For $n = $avMenuItems[$i][2] + 1 To 3
        $avMenuLevels[$n] = ""
    Next
Next
_ArrayDisplay($avMenuItems, "Debug: $avMenuItems")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func _MenuItemHit()
    Local $hWnd = @GUI_WinHandle
    Local $ctrlMenuItem = @GUI_CtrlId
    Local $iIndex = _ArraySearch($avMenuItems, $ctrlMenuItem, 1, 0, 0, 0, 1, 3)
    If @error Then
        MsgBox(16, "Error", "Unhandled case, CtrlID = " & $ctrlMenuItem)
        Return 0
    Else
        MsgBox(64, "Menu Item Hit", "$hWnd = " & $hWnd & @CRLF & _
                "$ctrlMenuItem = " & $ctrlMenuItem & @CRLF & _
                "Item Text = " & $avMenuItems[$iIndex][1])
    EndIf
EndFunc  ;==>_MenuItemHit

Func frmMainClose()
    Exit
EndFunc  ;==>frmMainClose

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...