Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WindowsConstants.au3>
- #include <GUIConstants.au3>
- #include <GUIMenu.au3>
- ; -Author: name22 (www.autoit.de)
- Global $iButtons = 10 ;Number of Buttons
- Global $aButtons[$iButtons + 1][2] = [[$iButtons]]
- Global Enum $iTestItem1 = 1000, $iTestItem2, $iTestItem3 ;IDs that will be passed to WM_COMMAND when one of the MenuItems is clicked.
- Global $iCurContext ;Variable containing the ArrayIndex of the Button that the current ContextMenu was assigned to.
- $hWnd_GUI = GUICreate("ContextMenu Example by name22", 400, 100)
- For $i = 1 To $aButtons[0][0]
- $aButtons[$i][0] = GUICtrlCreateButton($i, 5 + ($i - 1) * 40, 40, 30, 30)
- $aButtons[$i][1] = GUICtrlGetHandle($aButtons[$i][0])
- Next
- GUISetState()
- GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
- GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
- $hMenu_Context = _GUICtrlMenu_CreatePopup() ;Creation of the actual menu
- _GUICtrlMenu_InsertMenuItem($hMenu_Context, 0, "Test-Item 1", $iTestItem1)
- _GUICtrlMenu_InsertMenuItem($hMenu_Context, 1, "Test-Item 2", $iTestItem2)
- _GUICtrlMenu_InsertMenuItem($hMenu_Context, 2, "Test-Item 3", $iTestItem3)
- While True
- $nMsg = GUIGetMsg()
- Switch $nMsg
- Case $GUI_EVENT_CLOSE
- Exit
- Case $aButtons[1][0] To $aButtons[$aButtons[0][0]][0]
- MsgBox(64, "Button", "Button Clicked: " & $nMsg - $aButtons[1][0] + 1)
- EndSwitch
- WEnd
- Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
- #forceref $hWnd, $iMsg, $ilParam
- Switch $iwParam
- Case $iTestItem1
- ConsoleWrite("Button: " & $iCurContext & @TAB & "Context: Test-Item 1" & @CRLF)
- Case $iTestItem2
- ConsoleWrite("Button: " & $iCurContext & @TAB & "Context: Test-Item 2" & @CRLF)
- Case $iTestItem3
- ConsoleWrite("Button: " & $iCurContext & @TAB & "Context: Test-Item 3" & @CRLF)
- EndSwitch
- EndFunc ;==>WM_COMMAND
- Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
- #forceref $hWnd, $iMsg, $ilParam
- For $i = 1 To $aButtons[0][0]
- If $aButtons[$i][1] = $iwParam Then
- $iCurContext = $i
- _GUICtrlMenu_TrackPopupMenu($hMenu_Context, $hWnd_GUI) ;The ContextMenu will be assigned to the MainGUI if one of the buttons is rightclicked. If you assign the menu directly to the button, then WM_COMMAND won't be called when an Item is clicked.
- ExitLoop
- EndIf
- Next
- Return True
- EndFunc ;==>WM_CONTEXTMENU
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement